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