Commit 1d86bec5 authored by Temujin's avatar Temujin

added some functions to the classes

parent f213035d
"""Family Tree project for CSCI 30."""
from collections import defaultdict
class FamilyTree:
"""A family tree."""
"""A family tree.
Family Tree dict format --
key = person
value = [name of child1, name of child2, name of parent]
"""
"""
TODO:
Don't initialize with root.
Find a way to get root afterwards.
Find a way to get isolated nodes.
Problems to consider --
What to do if not rooted?
Multiple roots?
What if the file describes two separate trees at once?
Handling value errors.
Incest and cycles:
> Adding someone already in the tree as a child
Problems taken care of(?) --
Adding child to someone who already has two children.
Questions:
How does a dict compare object keys?
How to deal with multiple trees?
Is there only one root?
"""
def __init__(self, root):
self.family_tree = defaultdict([None, None])
self.family_tree[root] = [None, None]
pass
"""Initialize the family tree.
root: the name of the first generation person.
"""
self.family_tree = dict()
self.root = root
self.family_tree[root] = [None, None, None]
def insert_person(self, person, parent):
"""Add a person as a child to some parent."""
try:
parent_list = self.family_tree[parent]
except KeyError:
raise KeyError("Error: The parent is not in the tree.")
if parent_list[1] is None:
parent_list[1] = person
parent_list[person] = [None, None, parent]
elif parent_list[2] is None:
parent_list[2] = person
parent_list[person] = [None, None, parent]
else:
raise ValueError("Error: That parent already has two children.")
def get_parent(self, name):
def get_ancestors(self, name):
"""Return a list of all the ancestors of a person."""
ancestors = []
parent = self.family_tree[name][2]
while parent is not None:
ancestors += parent
parent = self.family_tree[parent][2]
ancestors.sort()
return ancestors
def get_descendants(self, name):
"""Return a list of a person's descendants."""
class Person:
"""A person."""
"""A person.
Attributes --
Name: a string indicating the name
Age: a integer indicating the age
Sex: a boolean, male = True, female = False
"""
def __init__(self, name, age, sex):
"""Initialize the Person class."""
self.name = name
self.age = age
self.sex = sex
def set_name(self, name):
"""Set name."""
self.name = name
def set_age(self, age):
"""Set age."""
self.age = age
def set_sex(self, sex):
"""Set sex (bool)."""
self.sex = sex
def get_name(self):
"""Get name."""
return self.name
def get_age(self):
"""Get age."""
return self.age
def get_sex(self):
"""Get sex (bool)."""
return self.sex
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment