Reformat union string

This commit is contained in:
Cyril 2021-08-23 07:43:14 +02:00
commit ccb11e2be4

View file

@ -8,7 +8,7 @@ event_types = ['N', 'M', 'A', 'D']
class Event:
def __init__(self, event_type: str, event_date: date, current_date: date,
person_a: Person, person_b: Person = None):
person_a: Person, person_b: Person = None, union_type=None):
if event_type not in event_types:
raise ValueError('{} is not in {}'.format(event_type, event_types))
self._type = event_type
@ -16,16 +16,25 @@ class Event:
self._current_date = current_date
self._p_a = person_a
self._p_b = person_b
self._union_type = union_type
def get_year_span(self) -> int:
return self._current_date.year - self._date.year
def event_description(self) -> str:
y = self.get_year_span()
union_descr = ''
if self._union_type == 'A':
union_descr = 's\'est uni{} à'.format('' if self._p_a.is_male() else 'e')
elif self._union_type == 'M':
union_descr = 'a épousé'
union_str = ' ({} {})'.format(union_descr, self._p_b.formatted_name()) if self._p_b else ''
if self._type == 'N':
return '{}{}{} il y a {} an{}'.format(
self._p_a.formatted_name(),
' (a épousé {})'.format(self._p_b.formatted_name()) if self._p_b else '',
union_str,
'' if self._p_a.is_male() else 'e',
y,
's' if y > 1 else ''
@ -49,7 +58,7 @@ class Event:
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 '',
union_str,
'' if self._p_a.is_male() else 'e',
y,
's' if y > 1 else ''
@ -134,11 +143,11 @@ def load_mariage_to_list(mariages_file: str) -> list:
return mariage_lst
def find_significant_other(mariages: list, uid: int) -> int:
def find_significant_other(mariages: list, uid: int) -> (int, str):
for m in mariages:
if m.in_this_union(uid):
return m.get_so(uid)
return -1
return m.get_so(uid), m.union()
return -1, ''
def create_events_from_span(start: date, end: date, members_file: str, mariages_file: str) -> list:
@ -149,7 +158,7 @@ def create_events_from_span(start: date, end: date, members_file: str, mariages_
events = []
for p in id_to_person.values():
so = find_significant_other(mariages, p.get_hash_id())
so, union = find_significant_other(mariages, p.get_hash_id())
if so == p.get_hash_id():
print(f"What ??? {so}")
if not p.is_dead():
@ -157,26 +166,26 @@ def create_events_from_span(start: date, end: date, members_file: str, mariages_
birth_in_year = date(cur_year, birth.month, birth.day)
if start <= birth_in_year <= end:
e = Event('N', birth, birth_in_year,
p, None if so == -1 else id_to_person[so])
p, None if so == -1 else id_to_person[so], None if so == -1 else union)
events.append(e)
birth_in_year = date(cur_year + 1, birth.month, birth.day)
if start <= birth_in_year <= end:
e = Event('N', birth, birth_in_year,
p, None if so == -1 else id_to_person[so])
p, None if so == -1 else id_to_person[so], None if so == -1 else union)
events.append(e)
else:
death = p.death()
death_in_year = date(cur_year, death.month, death.day)
if start <= death_in_year <= end:
e = Event('D', death, death_in_year,
p, None if so == -1 else id_to_person[so])
p, None if so == -1 else id_to_person[so], None if so == -1 else union)
events.append(e)
death_in_year = date(cur_year + 1, death.month, death.day)
if start <= death_in_year <= end:
e = Event('D', death, death_in_year,
p, None if so == -1 else id_to_person[so])
p, None if so == -1 else id_to_person[so], None if so == -1 else union)
events.append(e)
for m in mariages: