Commit f3cbf0dd authored by Marc Yap's avatar Marc Yap

Replace family_tree.py

parent b6b4a537
......@@ -333,6 +333,52 @@ class Person:
"""Return the name."""
return self.name
def openfile(filename): #Open Method
atree = FamilyTree()
fname = filename + ".ft"
readF = open(fname, "r")
familyname = readF.readline().split()[1]
count = int(readF.readline().split()[1])
for i in range(count):
name = readF.readline().split()[1]
age = int(readF.readline().split()[1])
sex = bool(readF.readline().split()[1])
person = Person(name, age, sex)
atree.insert_person(person)
links = int(readF.readline().split()[1])
for i in range(links):
line = readF.readline().split()
p1 = atree.get_person(line[1])
p2 = atree.get_person(line[2])
p2.set_parent(p1)
readF.close()
return atree
def savefile(filename, tree): #Save Method
fname = filename + ".ft"
saveF = open(fname, "w+")
saveF.write("family " + filename + "\n")
count = len(tree.family_tree)
saveF.write("count " + str(count) + "\n")
for i,x in tree.family_tree.items():
name = x.get_name()
age = str(x.get_age())
sex = str(x.get_sex())
saveF.write("name " + name + "\n")
saveF.write("age " + age + "\n")
saveF.write("sex " + sex + "\n")
links = count - 1
saveF.write("links " + str(links) + "\n")
for i,x in tree.family_tree.items():
if x is not tree.root:
parent = x.get_parent()
saveF.write("child " + parent.get_name() + " " + x.get_name() + "\n")
def main():
r"""For testing.
......@@ -381,6 +427,18 @@ def main():
print("relationship {} is {}'s: {}".format(D, C, ftree.relationship(C, D)))
print("relationship {} is {}'s: {}".format(C, D, ftree.relationship(D, C)))
print("")
#test for opening and saving files
ntree = openfile("chandelier")
ntree.find_root()
ntree.precompute()
print("The root of the tree is {}.".format(ntree.root))
for key, node in ntree.family_tree.items():
print("{}: {}".format(node, node.get_generation()))
savefile("test", ntree)
if __name__ == "__main__":
main()
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