Conditions | 6 |
Total Lines | 58 |
Code Lines | 45 |
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 |
||
6 | def _update_countries_data(): |
||
7 | # https://www.anagrafenazionale.interno.it/il-progetto/strumenti-di-lavoro/tabelle-decodifica/ |
||
8 | data_url = "https://www.anagrafenazionale.interno.it/wp-content/uploads/2021/03/tabella_2_statiesteri.xlsx" |
||
9 | data = benedict.from_xls(data_url) |
||
10 | data.standardize() |
||
11 | # print(data.dump()) |
||
12 | |||
13 | def map_item(item): |
||
14 | if not item: |
||
15 | return None |
||
16 | code = item.get_str("codat").upper() |
||
17 | if not code: |
||
18 | return None |
||
19 | assert len(code) == 4, f"Invalid code: '{code}'" |
||
20 | |||
21 | name = item.get_str("denominazione").title() |
||
22 | assert name != "", f"Invalid name: '{name}'" |
||
23 | name_alt = item.get_str("denominazioneistat").title() |
||
24 | name_alt_en = item.get_str("denominazioneistat_en").title() |
||
25 | name_slugs = sorted( |
||
26 | set( |
||
27 | filter( |
||
28 | bool, |
||
29 | [ |
||
30 | slugify(name), |
||
31 | slugify(name_alt), |
||
32 | slugify(name_alt_en), |
||
33 | ], |
||
34 | ) |
||
35 | ) |
||
36 | ) |
||
37 | province = "EE" |
||
38 | |||
39 | date_created = item.get_datetime("datainiziovalidita") |
||
40 | date_deleted = item.get_datetime("datafinevalidita") |
||
41 | date_deleted_raw = item.get_str("datafinevalidita") |
||
42 | if "9999" in date_deleted_raw: |
||
43 | date_deleted = "" |
||
44 | |||
45 | return { |
||
46 | "active": False if date_deleted else True, |
||
47 | "code": code, |
||
48 | "date_created": date_created, |
||
49 | "date_deleted": date_deleted, |
||
50 | "name": name, |
||
51 | "name_alt": name_alt, |
||
52 | "name_alt_en": name_alt_en, |
||
53 | "name_slugs": name_slugs, |
||
54 | "province": province, |
||
55 | } |
||
56 | |||
57 | output_data = list( |
||
58 | filter(bool, [map_item(benedict(item)) for item in data["values"]]) |
||
59 | ) |
||
60 | output_data = sorted(output_data, key=lambda item: item["name"]) |
||
61 | output_path = "../codicefiscale/data/countries.json" |
||
62 | output_abspath = fsutil.join_path(__file__, output_path) |
||
63 | fsutil.write_file_json(output_abspath, output_data, indent=4, sort_keys=True) |
||
64 | |||
138 |