Commit 8ffc046b authored by Temujin's avatar Temujin
parent 0ea8ba34
family Chandelier
count 11
name Kyle
age 91
sex true
name Fe
age 73
sex false
name Daniel
age 77
sex true
name Alma
age 52
sex false
name Aaron
age 49
sex true
name Dane
age 33
sex true
name Juan
age 32
sex true
name Charles
age 17
sex true
name Joyce
age 11
sex false
name Floyd
age 4
sex true
name Patricia
age 2
sex false
links 10
child Kyle Fe
child Kyle Daniel
child Daniel Alma
child Daniel Aaron
child Alma Dane
child Alma Juan
child Juan Charles
child Juan Joyce
child Charles Floyd
child Charles Patricia
......@@ -328,6 +328,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.
......@@ -376,6 +422,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