Conditions | 11 |
Total Lines | 76 |
Code Lines | 54 |
Lines | 76 |
Ratio | 100 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
Complex classes like modules.pull_config.get_config.get_config() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | import pandas as pd |
||
58 | View Code Duplication | def get_config(): |
|
1 ignored issue
–
show
|
|||
59 | """ |
||
60 | |||
61 | :return: |
||
62 | :rtype: |
||
63 | """ |
||
64 | pd.set_option('mode.chained_assignment', None) |
||
65 | print("Loading data") |
||
66 | values_input = import_from_sheets() |
||
67 | df = pd.DataFrame(values_input[1:], columns=values_input[0]) |
||
68 | |||
69 | print("Transforming data") |
||
70 | monsters_df = df[["name", "type"]] |
||
71 | monsters_df["type"] = pd.to_numeric(df["type"]) |
||
72 | |||
73 | triggers = df.drop(['name', 'role', 'type', 'id'], axis=1) |
||
74 | triggers = triggers.applymap(lambda s: s.lower() if isinstance(s) == str else s) |
||
75 | # triggers = triggers.applymap(lambda s: unidecode.unidecode(s) if type(s) == str else s) |
||
76 | |||
77 | triggers_list = [] |
||
78 | with tqdm(total=len(triggers), file=sys.stdout) as pbar: |
||
79 | for row in triggers.itertuples(index=False): |
||
80 | helpt = pd.Series(row) |
||
81 | helpt = helpt[~helpt.isna()] |
||
82 | # Drop empty strings |
||
83 | helpt = pd.Series(filter(None, helpt)) |
||
84 | # Copy strings with spaces without keeping them |
||
85 | for trigger in helpt: |
||
86 | trigger_nospace = trigger.replace(' ', '') |
||
87 | if trigger_nospace != trigger: |
||
88 | helpt = helpt.append(pd.Series(trigger_nospace)) |
||
89 | helpt = helpt.drop_duplicates() |
||
90 | triggers_list.append(helpt) |
||
91 | pbar.update(1) |
||
92 | |||
93 | print("Creating trigger structure") |
||
94 | triggers_def = [] |
||
95 | with tqdm(total=len(triggers_list), file=sys.stdout) as pbar: |
||
96 | for i in triggers_list: |
||
97 | triggers_def.append(list(i)) |
||
98 | pbar.update(1) |
||
99 | triggers_def_series = pd.Series(triggers_def) |
||
100 | monsters_df.insert(loc=0, column='triggers', value=triggers_def_series) |
||
101 | |||
102 | print("Creating output") |
||
103 | |||
104 | types = {'id': [4, 3, 2, 1, 0], 'label': ["Common", "Event_Likan", "Event_Ulf", "Legendary", "Rare"]} |
||
105 | types_df = pd.DataFrame(data=types) |
||
106 | milestones = {'total': [150, 1000, 5000], 'name': ["Rare Spotter", "Legendary Spotter", "Mythic Spotter"]} |
||
107 | milestones_df = pd.DataFrame(data=milestones) |
||
108 | json_final = {'milestones': milestones_df, 'types': types_df, 'commands': monsters_df} |
||
109 | |||
110 | # convert dataframes into dictionaries |
||
111 | data_dict = { |
||
112 | key: json_final[key].to_dict(orient='records') |
||
113 | for key in json_final |
||
114 | } |
||
115 | |||
116 | # write to disk |
||
117 | with open('json_files/config.json', 'w', encoding='utf8') as f: |
||
118 | json.dump( |
||
119 | data_dict, |
||
120 | f, |
||
121 | indent=4, |
||
122 | ensure_ascii=False, |
||
123 | sort_keys=False |
||
124 | ) |
||
125 | with open('modules/pull_config/output/config.txt', 'w', encoding='utf8') as f: |
||
126 | json.dump( |
||
127 | data_dict, |
||
128 | f, |
||
129 | indent=4, |
||
130 | ensure_ascii=False |
||
131 | ) |
||
132 | |||
133 | print(".json saved") |
||
134 | |||
138 |