Conditions | 3 |
Total Lines | 65 |
Code Lines | 51 |
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 |
||
77 | def _update_municipalities_data(): |
||
78 | # https://www.anagrafenazionale.interno.it/area-tecnica/tabelle-di-decodifica/ |
||
79 | data_url = "https://www.anagrafenazionale.interno.it/wp-content/uploads/2022/12/ANPR_archivio_comuni.csv" |
||
80 | data = benedict.from_csv(data_url) |
||
81 | data.standardize() |
||
82 | |||
83 | def map_item(item): |
||
84 | if not item: |
||
85 | return None |
||
86 | |||
87 | _expect_keys( |
||
88 | item, |
||
89 | [ |
||
90 | "stato", |
||
91 | "codcatastale", |
||
92 | "denominazione_it", |
||
93 | "denomtraslitterata", |
||
94 | "altradenominazione", |
||
95 | "altradenomtraslitterata", |
||
96 | "siglaprovincia", |
||
97 | "dataistituzione", |
||
98 | "datacessazione", |
||
99 | ], |
||
100 | ) |
||
101 | |||
102 | status = item.get("stato", "").upper() |
||
103 | assert len(status) == 1 and status in ["A", "C"], f"Invalid status: '{status}'" |
||
104 | active = status == "A" |
||
105 | |||
106 | code = item.get_str("codcatastale").upper() |
||
107 | assert code == "ND" or len(code) == 4, f"Invalid code: '{code}'" |
||
108 | |||
109 | name = item.get_str("denominazione_it").title() |
||
110 | assert name != "", f"Invalid name: {name}" |
||
111 | |||
112 | name_trans = item.get_str("denomtraslitterata").title() |
||
113 | name_alt = item.get_str("altradenominazione").title() |
||
114 | name_alt_trans = item.get_str("altradenomtraslitterata").title() |
||
115 | name_slugs = _slugify_names(name, name_trans, name_alt, name_alt_trans) |
||
116 | |||
117 | province = item.get("siglaprovincia", "").upper() |
||
118 | assert len(province) == 2, f"Invalid province: '{province}'" |
||
119 | |||
120 | date_created = item.get_datetime("dataistituzione") |
||
121 | date_deleted = item.get_datetime("datacessazione") |
||
122 | date_deleted_raw = item.get_str("datacessazione") |
||
123 | if "9999" in date_deleted_raw: |
||
124 | date_deleted = "" |
||
125 | |||
126 | return { |
||
127 | "active": active, |
||
128 | "code": code, |
||
129 | "date_created": date_created, |
||
130 | "date_deleted": date_deleted, |
||
131 | "name": name, |
||
132 | "name_trans": name_trans, |
||
133 | "name_alt": name_alt, |
||
134 | "name_alt_trans": name_alt_trans, |
||
135 | "name_slugs": name_slugs, |
||
136 | "province": province, |
||
137 | } |
||
138 | |||
139 | _write_data_json( |
||
140 | filepath="../codicefiscale/data/municipalities.json", |
||
141 | data=[map_item(benedict(item)) for item in data["values"]], |
||
142 | ) |
||
159 |