| 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 build.modules.pull_config.pull_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 sys |
||
| 57 | View Code Duplication | def get_config(): |
|
| 58 | """ |
||
| 59 | |||
| 60 | :return: |
||
| 61 | :rtype: |
||
| 62 | """ |
||
| 63 | pd.set_option('mode.chained_assignment', None) |
||
| 64 | print("Loading data") |
||
| 65 | values_input = import_from_sheets() |
||
| 66 | df = pd.DataFrame(values_input[1:], columns=values_input[0]) |
||
| 67 | |||
| 68 | print("Transforming data") |
||
| 69 | monsters_df = df[["name", "type"]] |
||
| 70 | monsters_df["type"] = pd.to_numeric(df["type"]) |
||
| 71 | |||
| 72 | triggers = df.drop(['name', 'role', 'type', 'id'], axis=1) |
||
| 73 | triggers = triggers.applymap(lambda s: s.lower() if type(s) == str else s) |
||
| 74 | # triggers = triggers.applymap(lambda s: unidecode.unidecode(s) if type(s) == str else s) |
||
| 75 | |||
| 76 | triggers_list = [] |
||
| 77 | with tqdm(total=len(triggers), file=sys.stdout) as pbar: |
||
| 78 | for row in triggers.itertuples(index=False): |
||
| 79 | helpt = pd.Series(row) |
||
| 80 | helpt = helpt[~helpt.isna()] |
||
| 81 | # Drop empty strings |
||
| 82 | helpt = pd.Series(filter(None, helpt)) |
||
| 83 | # Copy strings with spaces without keeping them |
||
| 84 | for trigger in helpt: |
||
| 85 | trigger_nospace = trigger.replace(' ', '') |
||
| 86 | if trigger_nospace != trigger: |
||
| 87 | helpt = helpt.append(pd.Series(trigger_nospace)) |
||
| 88 | helpt = helpt.drop_duplicates() |
||
| 89 | triggers_list.append(helpt) |
||
| 90 | pbar.update(1) |
||
| 91 | |||
| 92 | print("Creating trigger structure") |
||
| 93 | triggers_def = [] |
||
| 94 | with tqdm(total=len(triggers_list), file=sys.stdout) as pbar: |
||
| 95 | for i in triggers_list: |
||
| 96 | triggers_def.append(list(i)) |
||
| 97 | pbar.update(1) |
||
| 98 | triggers_def_series = pd.Series(triggers_def) |
||
| 99 | monsters_df.insert(loc=0, column='triggers', value=triggers_def_series) |
||
| 100 | |||
| 101 | print("Creating output") |
||
| 102 | |||
| 103 | types = {'id': [4, 3, 2, 1, 0], 'label': ["Common", "Event_Likan", "Event_Ulf", "Legendary", "Rare"]} |
||
| 104 | types_df = pd.DataFrame(data=types) |
||
| 105 | milestones = {'total': [150, 1000, 5000], 'name': ["Rare Spotter", "Legendary Spotter", "Mythic Spotter"]} |
||
| 106 | milestones_df = pd.DataFrame(data=milestones) |
||
| 107 | json_final = {'milestones': milestones_df, 'types': types_df, 'commands': monsters_df} |
||
| 108 | |||
| 109 | # convert dataframes into dictionaries |
||
| 110 | data_dict = { |
||
| 111 | key: json_final[key].to_dict(orient='records') |
||
| 112 | for key in json_final |
||
| 113 | } |
||
| 114 | |||
| 115 | # write to disk |
||
| 116 | with open('json_files/config.json', 'w', encoding='utf8') as f: |
||
| 117 | json.dump( |
||
| 118 | data_dict, |
||
| 119 | f, |
||
| 120 | indent=4, |
||
| 121 | ensure_ascii=False, |
||
| 122 | sort_keys=False |
||
| 123 | ) |
||
| 124 | with open('modules/pull_config/output/config.txt', 'w', encoding='utf8') as f: |
||
| 125 | json.dump( |
||
| 126 | data_dict, |
||
| 127 | f, |
||
| 128 | indent=4, |
||
| 129 | ensure_ascii=False |
||
| 130 | ) |
||
| 131 | |||
| 132 | print(".json saved") |
||
| 133 | |||
| 137 |