Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge fetched data with manually managed patch data. #116

Merged
merged 3 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions codicefiscale/data/countries-patch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[
]
44 changes: 44 additions & 0 deletions codicefiscale/data/municipalities-patch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[
{
"active": false,
"code": "F383",
"date_created": "1928-05-12T00:00:00",
"date_deleted": "1991-12-09T00:00:00",
"name": "Monserrato",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"monserrato"
],
"name_trans": "Monserrato",
"province": "CA"
},
{
"active": false,
"code": "G133",
"date_created": "1928-11-28T00:00:00",
"date_deleted": "1948-04-07T00:00:00",
"name": "Ortacesus",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"ortacesus"
],
"name_trans": "Ortacesus",
"province": "CA"
},
{
"active": false,
"code": "L513",
"date_created": "1927-11-04T00:00:00",
"date_deleted": "1946-02-27T00:00:00",
"name": "Ussaramanna",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"ussaramanna"
],
"name_trans": "Ussaramanna",
"province": "CA"
}
]
42 changes: 42 additions & 0 deletions codicefiscale/data/municipalities.json
Original file line number Diff line number Diff line change
Expand Up @@ -139084,6 +139084,20 @@
"name_trans": "Monserrato",
"province": "CA"
},
{
"active": false,
"code": "F383",
"date_created": "1928-05-12T00:00:00",
"date_deleted": "1991-12-09T00:00:00",
"name": "Monserrato",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"monserrato"
],
"name_trans": "Monserrato",
"province": "CA"
},
{
"active": false,
"code": "F384",
Expand Down Expand Up @@ -158555,6 +158569,20 @@
"name_trans": "Ortacesus",
"province": "SU"
},
{
"active": false,
"code": "G133",
"date_created": "1928-11-28T00:00:00",
"date_deleted": "1948-04-07T00:00:00",
"name": "Ortacesus",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"ortacesus"
],
"name_trans": "Ortacesus",
"province": "CA"
},
{
"active": false,
"code": "G135",
Expand Down Expand Up @@ -251715,6 +251743,20 @@
"name_trans": "Ussaramanna",
"province": "SU"
},
{
"active": false,
"code": "L513",
"date_created": "1927-11-04T00:00:00",
"date_deleted": "1946-02-27T00:00:00",
"name": "Ussaramanna",
"name_alt": "",
"name_alt_trans": "",
"name_slugs": [
"ussaramanna"
],
"name_trans": "Ussaramanna",
"province": "CA"
},
{
"active": false,
"code": "L514",
Expand Down
31 changes: 25 additions & 6 deletions scripts/updatedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from benedict import benedict
from slugify import slugify

DATA_DIR = fsutil.join_path(__file__, "../codicefiscale/data/")


def _expect_keys(d, keys):
missing_keys = list(set(keys) - set(d.keys()))
Expand Down Expand Up @@ -71,9 +73,12 @@ def map_item(item):
"province": province,
}

items_data = [map_item(benedict(item)) for item in data["values"]]
items_data_patch = _read_data_json("countries-patch.json")

_write_data_json(
filepath="../codicefiscale/data/countries.json",
data=[map_item(benedict(item)) for item in data["values"]],
filepath="countries.json",
data=items_data + items_data_patch,
)


Expand Down Expand Up @@ -142,17 +147,31 @@ def map_item(item):
"province": province,
}

items_data = [map_item(benedict(item)) for item in data["values"]]
items_data_patch = _read_data_json("municipalities-patch.json")

_write_data_json(
filepath="../codicefiscale/data/municipalities.json",
data=[map_item(benedict(item)) for item in data["values"]],
filepath="municipalities.json",
data=items_data + items_data_patch,
)


def _read_data_json(filepath):
data = fsutil.read_file_json(
fsutil.join_filepath(DATA_DIR, filepath),
)
return data


def _write_data_json(filepath, data):
data = list(filter(bool, data))
data = sorted(data, key=lambda item: item["name"])
data_filepath = fsutil.join_path(__file__, filepath)
fsutil.write_file_json(data_filepath, data, indent=4, sort_keys=True)
fsutil.write_file_json(
fsutil.join_filepath(DATA_DIR, filepath),
data,
indent=4,
sort_keys=True,
)


def main():
Expand Down
32 changes: 32 additions & 0 deletions tests/test_issue_0113.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import datetime
import unittest

from codicefiscale import codicefiscale


class Issue0113TestCase(unittest.TestCase):
def test_issue_0113(self):
"""
Wrong birthplace code error (missing date-range in the data-source).
"""
codes = [
"XXXXXX39D44G133O",
"XXXXXX46D11F383M",
"XXXXXX50H18F383N",
"XXXXXX38C71L513V",
"XXXXXX43C14G133F",
]
expected_birthdates = [
datetime.datetime(1939, 4, 4, 0, 0),
datetime.datetime(1946, 4, 11, 0, 0),
datetime.datetime(1950, 6, 18, 0, 0),
datetime.datetime(1938, 3, 31, 0, 0),
datetime.datetime(1943, 3, 14, 0, 0),
]
for index, code in enumerate(codes):
with self.subTest(f"code: {code}"):
code_data = codicefiscale.decode(code)
code_data.pop("omocodes")
code_data.pop("raw")
# print(code_data)
self.assertEqual(code_data["birthdate"], expected_birthdates[index])