| Conditions | 3 |
| Total Lines | 62 |
| Code Lines | 47 |
| 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 -*- |
||
| 85 | def _update_municipalities_data(): |
||
| 86 | # https://www.anagrafenazionale.interno.it/il-progetto/strumenti-di-lavoro/tabelle-decodifica/ |
||
| 87 | data_url = "https://www.anagrafenazionale.interno.it/wp-content/uploads/ANPR_archivio_comuni.csv" |
||
| 88 | data = benedict.from_csv(data_url) |
||
| 89 | data.standardize() |
||
| 90 | |||
| 91 | def map_item(item): |
||
| 92 | |||
| 93 | status = item.get("stato", "").upper() |
||
| 94 | assert len(status) == 1 and status in ["A", "C"], f"Invalid status: '{status}'" |
||
| 95 | active = status == "A" |
||
| 96 | |||
| 97 | code = item.get_str("codcatastale").upper() |
||
| 98 | assert code == "ND" or len(code) == 4, f"Invalid code: '{code}'" |
||
| 99 | |||
| 100 | name = item.get_str("denominazione_it").title() |
||
| 101 | assert name != "", f"Invalid name: {name}" |
||
| 102 | |||
| 103 | name_trans = item.get_str("denomtraslitterata").title() |
||
| 104 | name_alt = item.get_str("altradenominazione").title() |
||
| 105 | name_alt_trans = item.get_str("altradenomtraslitterata").title() |
||
| 106 | name_slugs = sorted( |
||
| 107 | set( |
||
| 108 | filter( |
||
| 109 | bool, |
||
| 110 | [ |
||
| 111 | slugify(name), |
||
| 112 | slugify(name_trans), |
||
| 113 | slugify(name_alt), |
||
| 114 | slugify(name_alt_trans), |
||
| 115 | ], |
||
| 116 | ) |
||
| 117 | ) |
||
| 118 | ) |
||
| 119 | province = item.get("siglaprovincia", "").upper() |
||
| 120 | assert len(province) == 2, f"Invalid province: '{province}'" |
||
| 121 | |||
| 122 | date_created = item.get_str("dataistituzione") |
||
| 123 | date_deleted = item.get_str("datacessazione") |
||
| 124 | if "9999" in date_deleted: |
||
| 125 | date_deleted = "" |
||
| 126 | |||
| 127 | return { |
||
| 128 | "active": active, |
||
| 129 | "code": code, |
||
| 130 | "date_created": date_created, |
||
| 131 | "date_deleted": date_deleted, |
||
| 132 | "name": name, |
||
| 133 | "name_trans": name_trans, |
||
| 134 | "name_alt": name_alt, |
||
| 135 | "name_alt_trans": name_alt_trans, |
||
| 136 | "name_slugs": name_slugs, |
||
| 137 | "province": province, |
||
| 138 | } |
||
| 139 | |||
| 140 | output_data = list( |
||
| 141 | filter(bool, [map_item(benedict(item)) for item in data["values"]]) |
||
| 142 | ) |
||
| 143 | output_data = sorted(output_data, key=lambda item: item["name"]) |
||
| 144 | output_path = "../codicefiscale/data/municipalities.json" |
||
| 145 | output_abspath = fsutil.join_path(__file__, output_path) |
||
| 146 | fsutil.write_file_json(output_abspath, output_data, indent=4, sort_keys=True) |
||
| 147 | |||
| 156 |