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