Conditions | 5 |
Total Lines | 57 |
Code Lines | 44 |
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 |
||
17 | def _update_countries_data(): |
||
18 | # https://www.anagrafenazionale.interno.it/area-tecnica/tabelle-di-decodifica/ |
||
19 | data_url = "https://www.anagrafenazionale.interno.it/wp-content/uploads/2022/10/tabella_2_statiesteri.xlsx" |
||
20 | data = benedict.from_xls(data_url) |
||
21 | data.standardize() |
||
22 | # print(data.dump()) |
||
23 | |||
24 | def map_item(item): |
||
25 | if not item: |
||
26 | return None |
||
27 | |||
28 | _expect_keys( |
||
29 | item, |
||
30 | [ |
||
31 | "codat", |
||
32 | "denominazione", |
||
33 | "denominazioneistat", |
||
34 | "denominazioneistat_en", |
||
35 | "datainiziovalidita", |
||
36 | "datafinevalidita", |
||
37 | ], |
||
38 | ) |
||
39 | |||
40 | code = item.get_str("codat").upper() |
||
41 | if not code: |
||
42 | return None |
||
43 | assert len(code) == 4, f"Invalid code: '{code}'" |
||
44 | |||
45 | name = item.get_str("denominazione").title() |
||
46 | assert name != "", f"Invalid name: '{name}'" |
||
47 | name_alt = item.get_str("denominazioneistat").title() |
||
48 | name_alt_en = item.get_str("denominazioneistat_en").title() |
||
49 | name_slugs = _slugify_names(name, name_alt, name_alt_en) |
||
50 | |||
51 | province = "EE" |
||
52 | |||
53 | date_created = item.get_datetime("datainiziovalidita") |
||
54 | date_deleted = item.get_datetime("datafinevalidita") |
||
55 | date_deleted_raw = item.get_str("datafinevalidita") |
||
56 | if "9999" in date_deleted_raw: |
||
57 | date_deleted = "" |
||
58 | |||
59 | return { |
||
60 | "active": False if date_deleted else True, |
||
61 | "code": code, |
||
62 | "date_created": date_created, |
||
63 | "date_deleted": date_deleted, |
||
64 | "name": name, |
||
65 | "name_alt": name_alt, |
||
66 | "name_alt_en": name_alt_en, |
||
67 | "name_slugs": name_slugs, |
||
68 | "province": province, |
||
69 | } |
||
70 | |||
71 | _write_data_json( |
||
72 | filepath="../codicefiscale/data/countries.json", |
||
73 | data=[map_item(benedict(item)) for item in data["values"]], |
||
74 | ) |
||
159 |