Add a to_dict method

This commit is contained in:
Gabriel Augendre 2019-07-10 23:45:18 +02:00
parent 82687980d3
commit 510bec8964

35
main.py
View file

@ -1,5 +1,6 @@
import calendar
import datetime
import pprint
from departments import DEPARTMENTS
from cities import CITIES
@ -53,6 +54,12 @@ class InseeData:
return "Male"
return "Female"
@property
def gender_short(self):
if self._gender == "1":
return "M"
return "F"
@property
def city(self):
if self.foreign:
@ -113,12 +120,40 @@ class InseeData:
return "\n".join(message)
def to_dict(self):
data = {
"is_valid": self.is_valid,
"gender": self.gender_short,
"month": self.month,
"year": self.year,
"order_of_birth": self.order_of_birth,
}
if self.foreign:
data["foreigner"] = {
"countries_names": COUNTRIES.get("99" + self.country, []),
"country_code": self.country,
"continent": CONTINENTS.get(self.country[0]),
}
else:
city = CITIES.get(self.department + self.city, dict())
data["french"] = {
"department_name": DEPARTMENTS[self.department],
"city_insee_code": self.city,
"department_code": self.department,
"city_name": city.get("name"),
"zip_code": city.get("zip_code"),
}
return data
def main():
# insee_number = "168127982980507"
insee_number = "269059913116714"
data = InseeData(insee_number)
print(data)
pprint.pprint(data.to_dict())
main()