Update event.py, mariage.py, and person.py

This commit is contained in:
cyril 2021-08-10 08:40:53 +02:00
commit 92c9fabbc4
3 changed files with 74 additions and 63 deletions

View file

@ -3,7 +3,7 @@ from person import Person
from mariage import Mariage
event_types = ['N', 'M', 'D']
event_types = ['N', 'M', 'A', 'D']
class Event:
@ -37,6 +37,16 @@ class Event:
y,
's' if y > 1 else ''
)
if self._type == 'A':
return '{} s\'est uni{} à {} il y a {} an{}'.format(
self._p_a.formatted_name(),
'' if self._p_a.is_male() else 'e',
self._p_b.formatted_name(),
y,
's' if y > 1 else ''
)
return '{}{} décédé{} il y a {} an{}'.format(
self._p_a.formatted_name(),
' (a épousé {})'.format(self._p_b.formatted_name()) if self._p_b else '',
@ -60,77 +70,85 @@ class Event:
def load_persons_to_dict(members_file: str) -> dict:
id_to_person = {}
first_line = True
# CSV structure from Heredis
# Gender,Name,First Names,N : Date of Birth,D : Date of Death,Name of father (to detect in laws)
with open(members_file, 'r', encoding='utf-8') as members:
for line in members:
if first_line:
first_line = False
continue
line.strip()
infos = line.split(';')
birth = infos[5].split('-')
death = None if infos[6] == 'NULL' else infos[6].split('-')
p = Person(int(infos[0]),
'M' if infos[3] == '1' else 'F',
infos[2],
infos[1],
date(int(birth[0]), int(birth[1]), int(birth[2])),
None if not death else date(int(death[0]), int(death[1]), int(death[2])),
infos[4] == '1',
infos[7] == '1')
id_to_person[int(infos[0])] = p
infos = line.split(',')
birth = infos[3][4:].split('/')
death = None if infos[4] == '' else infos[4][4:].split('/')
p = Person(infos[0], # Gender
infos[2], # First names
infos[1], # Last name
date(int(birth[2]), int(birth[1]), int(birth[0])), # Date of birth YYYY MM DD
None if not death else date(int(death[2]), int(death[1]), int(death[0])), # Date of death YYYY MM DD
infos[5] == '') # If empty, this is an in law
id_to_person[p.get_hash_id()] = p
return id_to_person
def load_mariage_to_dict(mariages_file: str) -> dict:
id_to_mariage = {}
def load_mariage_to_list(mariages_file: str) -> list:
mariage_lst = []
first_line = True
# CSV format from Heredis
# T : Date of Event,Name1 | Name2, FirstName1 | FirstName 2, Event long name, DoB1 | DoB2
with open(mariages_file, 'r', encoding='utf-8') as mariages:
for line in mariages:
if first_line:
first_line = False
continue
line.strip()
infos = line.split(';')
if infos[3] == "NULL": # Union libre, not an event
continue
m_type = 'M'
if infos[6] == '0':
m_type = 'D'
if infos[6] == '2':
m_type = 'L'
m_date = infos[3].split('-')
infos = line.split(',')
union_type = infos[0][0]
doe = infos[0][4:].split('/')
name1 = infos[1].split('|')[0].strip()
name2 = infos[1].split('|')[1].strip()
first_name1 = infos[2].split('|')[0].strip()
first_name2 = infos[2].split('|')[1].strip()
dob1 = infos[4].split('|')[0].strip()[4:].split('/')
dob2 = infos[4].split('|')[1].strip()[4:].split('/')
id1 = Person.get_id(first_name1, name1, date(int(dob1[2]), int(dob1[1]), int(dob1[0])))
id2 = Person.get_id(first_name2, name2, date(int(dob2[2]), int(dob2[1]), int(dob2[0])))
m = Mariage(
int(infos[0]),
int(infos[1]),
int(infos[2]),
date(int(m_date[0]), int(m_date[1]), int(m_date[2])),
infos[4] == '1',
infos[5] == '1',
m_type
id1,
id2,
date(int(doe[2]), int(doe[1]), int(doe[0])),
union_type
)
if m.show_mariage():
id_to_mariage[int(infos[0])] = m
return id_to_mariage
mariage_lst.append(m)
return mariage_lst
def find_significant_other(id_to_mariage: dict, uid: int) -> int:
for m in id_to_mariage.values():
if m.in_this_union(uid) and m.show_mariage():
def find_significant_other(mariages: list, uid: int) -> int:
for m in mariages:
if m.in_this_union(uid):
return m.get_so(uid)
return -1
def create_events_from_span(start: date, end: date, members_file: str, mariages_file: str) -> list:
id_to_person = load_persons_to_dict(members_file)
id_to_mariage = load_mariage_to_dict(mariages_file)
mariages = load_mariage_to_list(mariages_file)
cur_year = start.year
events = []
for p in id_to_person.values():
so = find_significant_other(id_to_mariage, p.uid())
if so == p.uid():
so = find_significant_other(mariages, p.get_hash_id())
if so == p.get_hash_id():
print(f"What ??? {so}")
if not p.is_dead():
birth = p.birth()
@ -159,19 +177,17 @@ def create_events_from_span(start: date, end: date, members_file: str, mariages_
p, None if so == -1 else id_to_person[so])
events.append(e)
for m in id_to_mariage.values():
if not m.show_mariage():
continue
for m in mariages:
m_date = m.event_date()
date_in_year = date(cur_year, m_date.month, m_date.day)
if start <= date_in_year <= end:
e = Event('M', m_date, date_in_year,
e = Event(m.union(), m_date, date_in_year,
id_to_person[m.id_a()], id_to_person[m.id_b()])
events.append(e)
date_in_year = date(cur_year + 1, m_date.month, m_date.day)
if start <= date_in_year <= end:
e = Event('M', m_date, date_in_year,
e = Event(m.union(), m_date, date_in_year,
id_to_person[m.id_a()], id_to_person[m.id_b()])
events.append(e)

View file

@ -1,24 +1,17 @@
from datetime import date
unions = ['D', 'M', 'L'] # Divorce (0), Mariage ou Pacs (1), Union Libre (2)
unions = ['M', 'A'] # Mariage (0), Autre union officielle (1)
class Mariage:
def __init__(self, uid: int, id_a: int, id_b: int, event_date: date, last_mariage: bool,
hide: bool, union: str):
self._uid = uid
def __init__(self, id_a: int, id_b: int, event_date: date, union: str):
self._id_a = id_a
self._id_b = id_b
self._date = event_date
self._last_mariage = last_mariage
self._hide = hide
if union not in unions:
raise ValueError('{} is not in {}'.format(union, unions))
self._union = union
def show_mariage(self):
return not self._hide and self._union != 'D' and self._last_mariage
def event_date(self):
return self._date
@ -35,3 +28,6 @@ class Mariage:
def id_b(self):
return self._id_b
def union(self):
return self._union

View file

@ -4,8 +4,8 @@ genders = ['M', 'F']
class Person:
def __init__(self, uid: int, gender: str, first_name: str, last_name: str,
birth: date, death: date, in_law: bool, hide: bool):
def __init__(self, gender: str, first_name: str, last_name: str,
birth: date, death: date, in_law: bool):
if gender not in genders:
raise ValueError('{} is not in {}'.format(gender, genders))
self._gender = gender
@ -14,18 +14,17 @@ class Person:
self._birth = birth
self._death = death
self._in_law = in_law
self._uid = uid
self._hide = hide
def uid(self):
return self._uid
@staticmethod
def get_id(first_name: str, name: str, dob: date):
return hash(first_name + name + dob.strftime('%Y%m%d'))
def get_hash_id(self):
return hash(self._first + self._last + self._birth.strftime('%Y%m%d'))
def is_dead(self):
return self._death is not None
def hide(self):
return self._hide
def death(self, death: date = None) -> date:
if death is not None:
self._death = death