| Conditions | 2 |
| Total Lines | 59 |
| 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 | # -*- coding: utf-8 -*- |
||
| 9 | def _update_municipalities_data(): |
||
| 10 | |||
| 11 | data_url = "https://www.anagrafenazionale.interno.it/wp-content/uploads/ANPR_archivio_comuni.csv" |
||
| 12 | data = benedict.from_csv(data_url) |
||
| 13 | data.standardize() |
||
| 14 | |||
| 15 | def map_item(item): |
||
| 16 | |||
| 17 | status = item.get("stato", "").upper() |
||
| 18 | assert len(status) == 1 and status in ["A", "C"] |
||
| 19 | active = status == "A" |
||
| 20 | |||
| 21 | code = item.get_str("codcatastale").upper() |
||
| 22 | assert code == "ND" or len(code) == 4, f"Invalid code: {code}" |
||
| 23 | |||
| 24 | name = item.get_str("denominazione_it").title() |
||
| 25 | assert name != "", f"Invalid name: {name}" |
||
| 26 | |||
| 27 | name_trans = item.get_str("denomtraslitterata").title() |
||
| 28 | name_alt = item.get_str("altradenominazione").title() |
||
| 29 | name_alt_trans = item.get_str("altradenomtraslitterata").title() |
||
| 30 | name_slugs = list( |
||
| 31 | set( |
||
| 32 | filter( |
||
| 33 | bool, |
||
| 34 | [ |
||
| 35 | slugify(name), |
||
| 36 | slugify(name_trans), |
||
| 37 | slugify(name_alt), |
||
| 38 | slugify(name_alt_trans), |
||
| 39 | ], |
||
| 40 | ) |
||
| 41 | ) |
||
| 42 | ) |
||
| 43 | province = item.get("siglaprovincia", "").upper() |
||
| 44 | assert len(province) == 2, f"Invalid province: {province}" |
||
| 45 | |||
| 46 | date_created = item.get_str("dataistituzione") |
||
| 47 | date_deleted = item.get_str("datacessazione") |
||
| 48 | if date_deleted.startswith("9999"): |
||
| 49 | date_deleted = "" |
||
| 50 | |||
| 51 | return { |
||
| 52 | "active": active, |
||
| 53 | "code": code, |
||
| 54 | "date_created": date_created, |
||
| 55 | "date_deleted": date_deleted, |
||
| 56 | "name": name, |
||
| 57 | "name_trans": name_trans, |
||
| 58 | "name_alt": name_alt, |
||
| 59 | "name_alt_trans": name_alt_trans, |
||
| 60 | "name_slugs": name_slugs, |
||
| 61 | "province": province, |
||
| 62 | } |
||
| 63 | |||
| 64 | output_data = [map_item(benedict(item)) for item in data["values"]] |
||
| 65 | output_path = "../codicefiscale/data/municipalities.json" |
||
| 66 | output_abspath = fsutil.join_path(__file__, output_path) |
||
| 67 | fsutil.write_file_json(output_abspath, output_data, indent=4) |
||
| 68 | |||
| 76 |