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)