0% found this document useful (0 votes)
3 views

Python Code F1b[1]

The document defines a Python class 'Person' for representing individuals in a family tree, including attributes for name, birth year, death year, parents, spouses, and children. It includes methods for adding family members, retrieving immediate and extended family, and displaying family relationships. The document also provides an example of using the class to create a family structure and display family members.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Python Code F1b[1]

The document defines a Python class 'Person' for representing individuals in a family tree, including attributes for name, birth year, death year, parents, spouses, and children. It includes methods for adding family members, retrieving immediate and extended family, and displaying family relationships. The document also provides an example of using the class to create a family structure and display family members.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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.
self.name = 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 self.parents:
self.parents.append(parent)
parent.add_child(self)

def add_spouse(self, spouse: 'Person'):


#Adding a spouse and ensuring its reverse relationship.
if spouse not in self.spouses:
self.spouses.append(spouse)
spouse.add_spouse(self)

def add_child(self, child: 'Person'):


# Adding children and ensuring its reverse relationship.
if child not in self.children:
self.children.append(child)
child.add_parent(self)
def get_siblings(self):
#will Return a list of siblings.
siblings = []
if self.parents:
for parent in self.parents:
for sibling in parent.children:
if sibling is not self and sibling not in siblings:
siblings.append(sibling)
return siblings

def get_immediate_family(self):
#It will Return immediate family members including
parents,siblings,spouse,and children.
immediate_family = {
'parents': self.parents,
'siblings': self.get_siblings(),
'spouses': self.spouses,
'children': self.children,
}
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 {self.name}:")
for relation, members in immediate_family.items():
member_names = ', '.join(member.name for member in members)
print(f"{relation.capitalize()}: {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(self.parents, self.children,
self.get_siblings())

# Adding aunts,uncles and cousins


for parent in self.parents:
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 {self.name} (living blood relatives):")
if extended_family:
for relative in extended_family:
print(f"- {relative.name}")
else:
print("No extended family members found.")

def __str__(self):
status = f"{self.name} (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()

You might also like