Conditions | 3 |
Total Lines | 63 |
Code Lines | 48 |
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 fsutil |
||
66 | def _update_municipalities_data(): |
||
67 | # https://www.anagrafenazionale.interno.it/il-progetto/strumenti-di-lavoro/tabelle-decodifica/ |
||
68 | data_url = "https://www.anagrafenazionale.interno.it/wp-content/uploads/ANPR_archivio_comuni.csv" |
||
69 | data = benedict.from_csv(data_url) |
||
70 | data.standardize() |
||
71 | |||
72 | def map_item(item): |
||
73 | |||
74 | status = item.get("stato", "").upper() |
||
75 | assert len(status) == 1 and status in ["A", "C"], f"Invalid status: '{status}'" |
||
76 | active = status == "A" |
||
77 | |||
78 | code = item.get_str("codcatastale").upper() |
||
79 | assert code == "ND" or len(code) == 4, f"Invalid code: '{code}'" |
||
80 | |||
81 | name = item.get_str("denominazione_it").title() |
||
82 | assert name != "", f"Invalid name: {name}" |
||
83 | |||
84 | name_trans = item.get_str("denomtraslitterata").title() |
||
85 | name_alt = item.get_str("altradenominazione").title() |
||
86 | name_alt_trans = item.get_str("altradenomtraslitterata").title() |
||
87 | name_slugs = sorted( |
||
88 | set( |
||
89 | filter( |
||
90 | bool, |
||
91 | [ |
||
92 | slugify(name), |
||
93 | slugify(name_trans), |
||
94 | slugify(name_alt), |
||
95 | slugify(name_alt_trans), |
||
96 | ], |
||
97 | ) |
||
98 | ) |
||
99 | ) |
||
100 | province = item.get("siglaprovincia", "").upper() |
||
101 | assert len(province) == 2, f"Invalid province: '{province}'" |
||
102 | |||
103 | date_created = item.get_datetime("dataistituzione") |
||
104 | date_deleted = item.get_datetime("datacessazione") |
||
105 | date_deleted_raw = item.get_str("datacessazione") |
||
106 | if "9999" in date_deleted_raw: |
||
107 | date_deleted = "" |
||
108 | |||
109 | return { |
||
110 | "active": active, |
||
111 | "code": code, |
||
112 | "date_created": date_created, |
||
113 | "date_deleted": date_deleted, |
||
114 | "name": name, |
||
115 | "name_trans": name_trans, |
||
116 | "name_alt": name_alt, |
||
117 | "name_alt_trans": name_alt_trans, |
||
118 | "name_slugs": name_slugs, |
||
119 | "province": province, |
||
120 | } |
||
121 | |||
122 | output_data = list( |
||
123 | filter(bool, [map_item(benedict(item)) for item in data["values"]]) |
||
124 | ) |
||
125 | output_data = sorted(output_data, key=lambda item: item["name"]) |
||
126 | output_path = "../codicefiscale/data/municipalities.json" |
||
127 | output_abspath = fsutil.join_path(__file__, output_path) |
||
128 | fsutil.write_file_json(output_abspath, output_data, indent=4, sort_keys=True) |
||
129 | |||
138 |