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 from mariage import Mariage
event_types = ['N', 'M', 'D'] event_types = ['N', 'M', 'A', 'D']
class Event: class Event:
@ -37,6 +37,16 @@ class Event:
y, y,
's' if y > 1 else '' '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( return '{}{} décédé{} il y a {} an{}'.format(
self._p_a.formatted_name(), self._p_a.formatted_name(),
' (a épousé {})'.format(self._p_b.formatted_name()) if self._p_b else '', ' (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: def load_persons_to_dict(members_file: str) -> dict:
id_to_person = {} id_to_person = {}
first_line = True 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: with open(members_file, 'r', encoding='utf-8') as members:
for line in members: for line in members:
if first_line: if first_line:
first_line = False first_line = False
continue continue
line.strip() line.strip()
infos = line.split(';') infos = line.split(',')
birth = infos[5].split('-')
death = None if infos[6] == 'NULL' else infos[6].split('-') birth = infos[3][4:].split('/')
p = Person(int(infos[0]), death = None if infos[4] == '' else infos[4][4:].split('/')
'M' if infos[3] == '1' else 'F',
infos[2], p = Person(infos[0], # Gender
infos[1], infos[2], # First names
date(int(birth[0]), int(birth[1]), int(birth[2])), infos[1], # Last name
None if not death else date(int(death[0]), int(death[1]), int(death[2])), date(int(birth[2]), int(birth[1]), int(birth[0])), # Date of birth YYYY MM DD
infos[4] == '1', None if not death else date(int(death[2]), int(death[1]), int(death[0])), # Date of death YYYY MM DD
infos[7] == '1') infos[5] == '') # If empty, this is an in law
id_to_person[int(infos[0])] = p id_to_person[p.get_hash_id()] = p
return id_to_person return id_to_person
def load_mariage_to_dict(mariages_file: str) -> dict: def load_mariage_to_list(mariages_file: str) -> list:
id_to_mariage = {} mariage_lst = []
first_line = True 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: with open(mariages_file, 'r', encoding='utf-8') as mariages:
for line in mariages: for line in mariages:
if first_line: if first_line:
first_line = False first_line = False
continue continue
line.strip() line.strip()
infos = line.split(';') infos = line.split(',')
if infos[3] == "NULL": # Union libre, not an event
continue union_type = infos[0][0]
m_type = 'M' doe = infos[0][4:].split('/')
if infos[6] == '0':
m_type = 'D' name1 = infos[1].split('|')[0].strip()
if infos[6] == '2': name2 = infos[1].split('|')[1].strip()
m_type = 'L'
m_date = infos[3].split('-') 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( m = Mariage(
int(infos[0]), id1,
int(infos[1]), id2,
int(infos[2]), date(int(doe[2]), int(doe[1]), int(doe[0])),
date(int(m_date[0]), int(m_date[1]), int(m_date[2])), union_type
infos[4] == '1',
infos[5] == '1',
m_type
) )
if m.show_mariage(): mariage_lst.append(m)
id_to_mariage[int(infos[0])] = m return mariage_lst
return id_to_mariage
def find_significant_other(id_to_mariage: dict, uid: int) -> int: def find_significant_other(mariages: list, uid: int) -> int:
for m in id_to_mariage.values(): for m in mariages:
if m.in_this_union(uid) and m.show_mariage(): if m.in_this_union(uid):
return m.get_so(uid) return m.get_so(uid)
return -1 return -1
def create_events_from_span(start: date, end: date, members_file: str, mariages_file: str) -> list: 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_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 cur_year = start.year
events = [] events = []
for p in id_to_person.values(): for p in id_to_person.values():
so = find_significant_other(id_to_mariage, p.uid()) so = find_significant_other(mariages, p.get_hash_id())
if so == p.uid(): if so == p.get_hash_id():
print(f"What ??? {so}") print(f"What ??? {so}")
if not p.is_dead(): if not p.is_dead():
birth = p.birth() 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]) p, None if so == -1 else id_to_person[so])
events.append(e) events.append(e)
for m in id_to_mariage.values(): for m in mariages:
if not m.show_mariage():
continue
m_date = m.event_date() m_date = m.event_date()
date_in_year = date(cur_year, m_date.month, m_date.day) date_in_year = date(cur_year, m_date.month, m_date.day)
if start <= date_in_year <= end: 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()]) id_to_person[m.id_a()], id_to_person[m.id_b()])
events.append(e) events.append(e)
date_in_year = date(cur_year + 1, m_date.month, m_date.day) date_in_year = date(cur_year + 1, m_date.month, m_date.day)
if start <= date_in_year <= end: 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()]) id_to_person[m.id_a()], id_to_person[m.id_b()])
events.append(e) events.append(e)

View file

@ -1,24 +1,17 @@
from datetime import date 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: class Mariage:
def __init__(self, uid: int, id_a: int, id_b: int, event_date: date, last_mariage: bool, def __init__(self, id_a: int, id_b: int, event_date: date, union: str):
hide: bool, union: str):
self._uid = uid
self._id_a = id_a self._id_a = id_a
self._id_b = id_b self._id_b = id_b
self._date = event_date self._date = event_date
self._last_mariage = last_mariage
self._hide = hide
if union not in unions: if union not in unions:
raise ValueError('{} is not in {}'.format(union, unions)) raise ValueError('{} is not in {}'.format(union, unions))
self._union = union self._union = union
def show_mariage(self):
return not self._hide and self._union != 'D' and self._last_mariage
def event_date(self): def event_date(self):
return self._date return self._date
@ -35,3 +28,6 @@ class Mariage:
def id_b(self): def id_b(self):
return self._id_b return self._id_b
def union(self):
return self._union

View file

@ -4,8 +4,8 @@ genders = ['M', 'F']
class Person: class Person:
def __init__(self, uid: int, gender: str, first_name: str, last_name: str, def __init__(self, gender: str, first_name: str, last_name: str,
birth: date, death: date, in_law: bool, hide: bool): birth: date, death: date, in_law: bool):
if gender not in genders: if gender not in genders:
raise ValueError('{} is not in {}'.format(gender, genders)) raise ValueError('{} is not in {}'.format(gender, genders))
self._gender = gender self._gender = gender
@ -14,18 +14,17 @@ class Person:
self._birth = birth self._birth = birth
self._death = death self._death = death
self._in_law = in_law self._in_law = in_law
self._uid = uid
self._hide = hide
def uid(self): @staticmethod
return self._uid 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): def is_dead(self):
return self._death is not None return self._death is not None
def hide(self):
return self._hide
def death(self, death: date = None) -> date: def death(self, death: date = None) -> date:
if death is not None: if death is not None:
self._death = death self._death = death