SHOW:
|
|
- or go back to the newest paste.
| 1 | import re | |
| 2 | ||
| 3 | ||
| 4 | def generate_entry(item, special=False): | |
| 5 | if not special: | |
| 6 | return f"""str_in_list(field('tags'), ",", '{item}', '{item}',"""
| |
| 7 | return f"""test({item},{item},"""
| |
| 8 | ||
| 9 | ||
| 10 | def generate_joint_entry(item, special=False): | |
| 11 | if not special: | |
| 12 | return f"""strcat(str_in_list(field('tags'),",", '{item}', strcat('{item}/',"""
| |
| 13 | return f"""strcat(test({item}, strcat({item},'/',"""
| |
| 14 | ||
| 15 | ||
| 16 | hierarchy = {
| |
| 17 | "Magazine": {
| |
| 18 | "%series%": [] | |
| 19 | }, | |
| 20 | "Chess": {
| |
| 21 | "%series%": [] | |
| 22 | }, | |
| 23 | "IT": {
| |
| 24 | "Programming": {
| |
| 25 | "Application Design": [], | |
| 26 | "Python": [], | |
| 27 | "JavaScript": [], | |
| 28 | "Java": [], | |
| 29 | "C": [], | |
| 30 | "C++": [], | |
| 31 | "Algorithms": [] | |
| 32 | }, | |
| 33 | "Operating Systems": [], | |
| 34 | "Algorithms": [], | |
| 35 | "Networking": [], | |
| 36 | "Project Management": [], | |
| 37 | "Data Science": [], | |
| 38 | "Machine Learning": [], | |
| 39 | "InfoSec": [], | |
| 40 | "Databases": [], | |
| 41 | "VCS": [] | |
| 42 | }, | |
| 43 | "LightNovel": {
| |
| 44 | "%series%": [], | |
| 45 | }, | |
| 46 | "WebNovel": {
| |
| 47 | "%series%": [], | |
| 48 | }, | |
| 49 | "Fiction": {
| |
| 50 | "%series%": [], | |
| 51 | }, | |
| 52 | "Science": [], | |
| 53 | "Psychology": [], | |
| 54 | "Philosophy": [], | |
| 55 | "Business": [], | |
| 56 | "Life Style": [], | |
| 57 | "Mathematics": [], | |
| 58 | "Classics": [], | |
| 59 | } | |
| 60 | ||
| 61 | ||
| 62 | def gen(item_hierarchy): | |
| 63 | def t(item_hierarchy): | |
| 64 | s = "" | |
| 65 | missing_brackets = 0 | |
| 66 | for k, v in item_hierarchy.items(): | |
| 67 | special = False | |
| 68 | if re.search(r'^\%(\D+)\%$', k): | |
| 69 | k = f"field('{k[1:-1]}')"
| |
| 70 | special = True | |
| 71 | ||
| 72 | if type(v) is dict: | |
| 73 | s += generate_joint_entry(k, special=special) | |
| 74 | special = False | |
| 75 | missing_brackets += 2 | |
| 76 | s += t(v) | |
| 77 | s += ")," | |
| 78 | else: | |
| 79 | s += generate_entry(k, special=special) | |
| 80 | missing_brackets += 1 | |
| 81 | special = False | |
| 82 | ||
| 83 | s += "'Other'" | |
| 84 | s += ')' * missing_brackets | |
| 85 | return s | |
| 86 | ||
| 87 | return "program: strcat(" + t(item_hierarchy) + ", '/', field('title'))"
| |
| 88 | ||
| 89 | ||
| 90 | print(gen(hierarchy)) | |
| 91 |