| Conditions | 8 |
| Total Lines | 73 |
| Code Lines | 56 |
| 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 -*- |
||
| 10 | def _update_countries_data(): |
||
| 11 | # https://www.anagrafenazionale.interno.it/il-progetto/strumenti-di-lavoro/tabelle-decodifica/ |
||
| 12 | data_url = "https://www.anagrafenazionale.interno.it/wp-content/uploads/2021/03/tabella_2_statiesteri.xlsx" |
||
| 13 | data_path = fsutil.download_file(data_url, __file__, filename="countries.xlsx") |
||
| 14 | |||
| 15 | workbook = load_workbook(filename=data_path, read_only=True) |
||
| 16 | sheet = workbook.active |
||
| 17 | |||
| 18 | items = [] |
||
| 19 | keys = [] |
||
| 20 | for row in sheet.iter_rows(min_row=1, max_row=1): |
||
| 21 | keys = [cell.value for cell in row] |
||
| 22 | for row in sheet.iter_rows(min_row=2): |
||
| 23 | values = list([cell.value for cell in row]) |
||
| 24 | items.append(dict(zip(keys, values))) |
||
| 25 | |||
| 26 | workbook.close() |
||
| 27 | fsutil.remove_file(data_path) |
||
| 28 | |||
| 29 | data = benedict({"values": items}) |
||
| 30 | data.standardize() |
||
| 31 | # print(data.dump()) |
||
| 32 | |||
| 33 | def map_item(item): |
||
| 34 | if not item: |
||
| 35 | return None |
||
| 36 | code = item.get_str("codat").upper() |
||
| 37 | if not code: |
||
| 38 | return None |
||
| 39 | assert len(code) == 4, f"Invalid code: '{code}'" |
||
| 40 | |||
| 41 | name = item.get_str("denominazione").title() |
||
| 42 | assert name != "", f"Invalid name: '{name}'" |
||
| 43 | name_alt = item.get_str("denominazioneistat").title() |
||
| 44 | name_alt_en = item.get_str("denominazioneistat_en").title() |
||
| 45 | name_slugs = sorted( |
||
| 46 | set( |
||
| 47 | filter( |
||
| 48 | bool, |
||
| 49 | [ |
||
| 50 | slugify(name), |
||
| 51 | slugify(name_alt), |
||
| 52 | slugify(name_alt_en), |
||
| 53 | ], |
||
| 54 | ) |
||
| 55 | ) |
||
| 56 | ) |
||
| 57 | province = "EE" |
||
| 58 | |||
| 59 | date_created = item.get_str("datainiziovalidita") |
||
| 60 | date_deleted = item.get_str("datafinevalidita") |
||
| 61 | if "9999" in date_deleted: |
||
| 62 | date_deleted = "" |
||
| 63 | |||
| 64 | return { |
||
| 65 | "active": False if date_deleted else True, |
||
| 66 | "code": code, |
||
| 67 | "date_created": date_created, |
||
| 68 | "date_deleted": date_deleted, |
||
| 69 | "name": name, |
||
| 70 | "name_alt": name_alt, |
||
| 71 | "name_alt_en": name_alt_en, |
||
| 72 | "name_slugs": name_slugs, |
||
| 73 | "province": province, |
||
| 74 | } |
||
| 75 | |||
| 76 | output_data = list( |
||
| 77 | filter(bool, [map_item(benedict(item)) for item in data["values"]]) |
||
| 78 | ) |
||
| 79 | output_data = sorted(output_data, key=lambda item: item["name"]) |
||
| 80 | output_path = "../codicefiscale/data/countries.json" |
||
| 81 | output_abspath = fsutil.join_path(__file__, output_path) |
||
| 82 | fsutil.write_file_json(output_abspath, output_data, indent=4, sort_keys=True) |
||
| 83 | |||
| 156 |