| Conditions | 7 |
| Total Lines | 65 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| 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:
| 1 | import pandas as pd |
||
| 52 | def get_config(): |
||
| 53 | """ |
||
| 54 | |||
| 55 | :return: |
||
| 56 | :rtype: |
||
| 57 | """ |
||
| 58 | pd.set_option('mode.chained_assignment', None) |
||
| 59 | print("Loading data") |
||
| 60 | values_input = import_from_sheets() |
||
| 61 | df = pd.DataFrame(values_input[1:], columns=values_input[0]) |
||
| 62 | |||
| 63 | print("Transforming data") |
||
| 64 | monsters_df = df[["name", "type"]] |
||
| 65 | monsters_df["type"] = pd.to_numeric(df["type"]) |
||
| 66 | |||
| 67 | triggers = df.drop(['name', 'role', 'type', 'id'], axis=1) |
||
| 68 | triggers = triggers.applymap(lambda s: s.lower() if type(s) == str else s) |
||
| 69 | # triggers = triggers.applymap(lambda s: unidecode.unidecode(s) if type(s) == str else s) |
||
| 70 | |||
| 71 | triggers_list = [] |
||
| 72 | for row in triggers.itertuples(index=False): |
||
| 73 | helpt = pd.Series(row) |
||
| 74 | helpt = helpt[~helpt.isna()] |
||
| 75 | # Drop empty strings |
||
| 76 | helpt = pd.Series(filter(None, helpt)) |
||
| 77 | # Copy strings with spaces without keeping them |
||
| 78 | for trigger in helpt: |
||
| 79 | trigger_nospace = trigger.replace(' ', '') |
||
| 80 | helpt = helpt.append(pd.Series(trigger_nospace)) |
||
| 81 | helpt = helpt.drop_duplicates() |
||
| 82 | triggers_list.append(helpt) |
||
| 83 | |||
| 84 | print("Creating trigger structure") |
||
| 85 | triggers_def = [] |
||
| 86 | for i in triggers_list: |
||
| 87 | triggers_def.append(list(i)) |
||
| 88 | triggers_def_series = pd.Series(triggers_def) |
||
| 89 | monsters_df.insert(loc=0, column='triggers', value=triggers_def_series) |
||
| 90 | |||
| 91 | print("Creating output") |
||
| 92 | |||
| 93 | types = {'id': [4, 3, 2, 1, 0], 'label': ["Common", "Event0", "Event1", "Legendary", "Rare"]} |
||
| 94 | types_df = pd.DataFrame(data=types) |
||
| 95 | milestones = {'total': [150, 1000, 2000, 3000, 4000, 5000], |
||
| 96 | 'name': ["Rare Spotter", "Pepega Spotter", "Pog Spotter", "Pogmare Spotter", "Legendary Spotter", |
||
| 97 | "Mythic Spotter"]} |
||
| 98 | milestones_df = pd.DataFrame(data=milestones) |
||
| 99 | json_final = {'milestones': milestones_df, 'types': types_df, 'commands': monsters_df} |
||
| 100 | |||
| 101 | # convert dataframes into dictionaries |
||
| 102 | data_dict = { |
||
| 103 | key: json_final[key].to_dict(orient='records') |
||
| 104 | for key in json_final |
||
| 105 | } |
||
| 106 | |||
| 107 | # write to disk |
||
| 108 | with open('server_files/config.json', 'w', encoding='utf8') as f: |
||
| 109 | json.dump( |
||
| 110 | data_dict, |
||
| 111 | f, |
||
| 112 | indent=4, |
||
| 113 | ensure_ascii=False, |
||
| 114 | sort_keys=False |
||
| 115 | ) |
||
| 116 | print(".json saved") |
||
| 117 | |||
| 121 |