class Person:
#Class for Representing a person in the family.
#Attributes:
# name (str): Name of the person.
# birth_year (int): Year in which Person was born.
# death_year (int or None): Year in which person died, or None if that
person is still alive.
#parents (list of Parents): List containing parents of the person.
#spouses (list of Spouses): List containing spouses of the person.
#children (list of Children): A List containing children of the person.
def __init__(self, name: str, birth_year: int, death_year: int = None):
#Made a constructor
#Arguments:
# name (str): Name of person.
# birth_year (int): Year the person was born.
# death_year (int, optional): year the person died, or None if he
is still alive.
[Link] = name
self.birth_year = birth_year
self.death_year = death_year
self._parents = [] #made list for holding parents
self._spouses = [] #made list for holding spouses
self._children = [] #made list for holding Children
@property
def parents(self):
return self._parents
@property
def spouses(self):
return self._spouses
@property
def children(self):
return self._children
def add_parent(self, parent: 'Person'):
#Adding parents and ensuring the reverse relationship between them.
if parent not in [Link]:
[Link](parent)
parent.add_child(self)
def add_spouse(self, spouse: 'Person'):
#Adding a spouse and ensuring its reverse relationship.
if spouse not in [Link]:
[Link](spouse)
spouse.add_spouse(self)
def add_child(self, child: 'Person'):
# Adding children and ensuring its reverse relationship.
if child not in [Link]:
[Link](child)
child.add_parent(self)
def get_siblings(self):
#will Return a list of siblings.
siblings = []
if [Link]:
for parent in [Link]:
for sibling in [Link]:
if sibling is not self and sibling not in siblings:
[Link](sibling)
return siblings
def get_immediate_family(self):
#It will Return immediate family members including
parents,siblings,spouse,and children.
immediate_family = {
'parents': [Link],
'siblings': self.get_siblings(),
'spouses': [Link],
'children': [Link],
}
return immediate_family
def display_immediate_family(self):
#Displaying names of person's immediate family.
immediate_family = self.get_immediate_family()
print(f"Immediate family of {[Link]}:")
for relation, members in immediate_family.items():
member_names = ', '.join([Link] for member in members)
print(f"{[Link]()}: {member_names if member_names
else 'None'}")
def get_extended_family(self):
#will return the extended family members.
extended_family = set()
# for Adding immediate family
extended_family.update([Link], [Link],
self.get_siblings())
# Adding aunts,uncles and cousins
for parent in [Link]:
for aunt_uncle in parent.get_siblings():
if aunt_uncle.death_year is None: # Only displays the living
relatives
extended_family.add(aunt_uncle)
extended_family.update(child for child in
aunt_uncle.children if child.death_year is None)
# getting out self and only living blood relatives
extended_family.discard(self)
return [relative for relative in extended_family if
relative.death_year is None]
def display_extended_family(self):
#Displaying names of the person's extended family.
extended_family = self.get_extended_family()
print(f"Extended family of {[Link]} (living blood relatives):")
if extended_family:
for relative in extended_family:
print(f"- {[Link]}")
else:
print("No extended family members found.")
def __str__(self):
status = f"{[Link]} (b. {self.birth_year}"
if self.death_year:
status += f", d. {self.death_year}"
status += ")"
return status
# Using the Person Class with Immediate and Extended Family
# Creating persons in the Emmersohn family
#I will give name to each person and its brith year
cornelia = Person("Cornelia Emmersohn", 1980)
otto = Person("Otto Emmersohn", 1978)
child1 = Person("Emily Emmersohn", 2005)
child2 = Person("David Emmersohn", 2007)
grandchild1 = Person("Sophia Emmersohn", 2028)
grandchild2 = Person("Liam Emmersohn", 2030)
# Now i will Add relationships
# Assume that Cornelia and Otto are married and they have two children
cornelia.add_spouse(otto)
cornelia.add_child(child1)
cornelia.add_child(child2)
# Emily has two children, making Cornelia and Otto grandparents
child1.add_child(grandchild1)
child1.add_child(grandchild2)
# Displaying immediate family of Emily
print("Displaying Emily's immediate family:")
child1.display_immediate_family()
# Displaying extended family of Cornelia
print("\nDisplaying Cornelia's extended family:")
cornelia.display_extended_family()