Commit fc4afe66 authored by Temujin's avatar Temujin

fixed relationship func. commandline added

parent 9b822ba9
...@@ -197,12 +197,12 @@ class FamilyTree: ...@@ -197,12 +197,12 @@ class FamilyTree:
elif furthest == 2: elif furthest == 2:
return self.TERM("nibling", d1, d2, p2.sex) return self.TERM("nibling", d1, d2, p2.sex)
else: else:
return self.GREAT(furthest) + "grand" + self.TERM("nibling", d1, d2, p2.sex) return self.GREAT(furthest-2) + "grand" + self.TERM("nibling", d1, d2, p2.sex)
elif nearest == 0: elif nearest == 0:
if furthest == 1: if furthest == 1:
return self.TERM("child", d1, d2, p2.sex) return self.TERM("child", d1, d2, p2.sex)
else: else:
return self.GREAT(furthest) + "grand" + self.TERM("child", d1, d2, p2.sex) return self.GREAT(furthest-2) + "grand" + self.TERM("child", d1, d2, p2.sex)
def ORDINAL(self, n): def ORDINAL(self, n):
"""Return the index in the ordinal table.""" """Return the index in the ordinal table."""
...@@ -328,7 +328,9 @@ class Person: ...@@ -328,7 +328,9 @@ class Person:
"""Return the name.""" """Return the name."""
return self.name return self.name
def openfile(filename): #Open Method
def openfile(filename):
"""Open a .ft file."""
atree = FamilyTree() atree = FamilyTree()
fname = filename + ".ft" fname = filename + ".ft"
readF = open(fname, "r") readF = open(fname, "r")
...@@ -356,14 +358,16 @@ def openfile(filename): #Open Method ...@@ -356,14 +358,16 @@ def openfile(filename): #Open Method
atree.precompute() atree.precompute()
return atree return atree
def savefile(filename, tree): #Save Method
def savefile(filename, tree):
"""Save the tree to a file."""
fname = filename + ".ft" fname = filename + ".ft"
saveF = open(fname, "w+") saveF = open(fname, "w+")
saveF.write("family " + filename + "\n") saveF.write("family " + filename + "\n")
count = len(tree.family_tree) count = len(tree.family_tree)
saveF.write("count " + str(count) + "\n") saveF.write("count " + str(count) + "\n")
for i,x in tree.family_tree.items(): for i, x in tree.family_tree.items():
name = x.get_name() name = x.get_name()
age = str(x.get_age()) age = str(x.get_age())
sex = str(x.get_sex()) sex = str(x.get_sex())
...@@ -374,104 +378,106 @@ def savefile(filename, tree): #Save Method ...@@ -374,104 +378,106 @@ def savefile(filename, tree): #Save Method
links = count - 1 links = count - 1
saveF.write("links " + str(links) + "\n") saveF.write("links " + str(links) + "\n")
for i,x in tree.family_tree.items(): for i, x in tree.family_tree.items():
if x is not tree.root: if x is not tree.root:
parent = x.get_parent() parent = x.get_parent()
saveF.write("child " + parent.get_name() + " " + x.get_name() + "\n") saveF.write("child " + parent.get_name() + " " + x.get_name() + "\n")
def commandLine(): def commandLine():
tree = FamilyTree() """Open command line."""
while True: tree = FamilyTree()
cmd = input().split() while True:
if cmd[0] == "count": cmd = input().split()
print(len(tree.family_tree.items())) if cmd[0] == "count":
elif cmd[0] == "age": print(len(tree.family_tree.items()))
try: elif cmd[0] == "age":
print(tree.get_person(cmd[1]).get_age()) try:
except KeyError: print(tree.get_person(cmd[1]).get_age())
print("Error: no such person " + cmd[1]) except KeyError:
elif cmd[0] == "sex": print("Error: no such person " + cmd[1])
try: elif cmd[0] == "sex":
if tree.get_person(cmd[1]).get_sex(): try:
print("male") if tree.get_person(cmd[1]).get_sex():
else: print("male")
print("female") else:
except KeyError: print("female")
print("Error: no such person " + cmd[1]) except KeyError:
elif cmd[0] == "ancestors": print("Error: no such person " + cmd[1])
try: elif cmd[0] == "ancestors":
if len(tree.get_ancestors(cmd[1])) == 0: try:
print("(no ancestors)") if len(tree.get_ancestors(cmd[1])) == 0:
else: print("(no ancestors)")
for person in tree.get_ancestors(cmd[1]): else:
print(person) for person in tree.get_ancestors(cmd[1]):
except KeyError: print(person)
print("Error: no such person " + cmd[1]) except KeyError:
elif cmd[0] == "descendants": print("Error: no such person " + cmd[1])
try: elif cmd[0] == "descendants":
if len(tree.get_descendants(cmd[1])) == 0: try:
print("(no descendants)") if len(tree.get_descendants(cmd[1])) == 0:
else: print("(no descendants)")
for person in tree.get_descendants(cmd[1]): else:
print(person) for person in tree.get_descendants(cmd[1]):
except KeyError: print(person)
print("Error: no such person " + cmd[1]) except KeyError:
elif cmd[0] == "edit-name": print("Error: no such person " + cmd[1])
try: elif cmd[0] == "edit-name":
if tree.family_tree.get(cmd[2]) == None: try:
tree.get_person(cmd[1]).set_name(cmd[2]) if tree.family_tree.get(cmd[2]) is None:
tree.family_tree[cmd[2]] = tree.family_tree[cmd[1]] tree.get_person(cmd[1]).set_name(cmd[2])
del tree.family_tree[cmd[1]] tree.family_tree[cmd[2]] = tree.family_tree[cmd[1]]
else: del tree.family_tree[cmd[1]]
print("Error: "+cmd[2]+" exists already") else:
except KeyError: print("Error: "+cmd[2]+" exists already")
print("Error: no such person " + cmd[1]) except KeyError:
elif cmd[0] == "edit-age": print("Error: no such person " + cmd[1])
try: elif cmd[0] == "edit-age":
tree.get_person(cmd[1]).set_age(cmd[2]) try:
except KeyError: tree.get_person(cmd[1]).set_age(cmd[2])
print("Error: no such person " + cmd[1]) except KeyError:
elif cmd[0] == "edit-sex": print("Error: no such person " + cmd[1])
try: elif cmd[0] == "edit-sex":
if cmd[2] == "true": try:
tree.get_person(cmd[1]).set_sex(True) if cmd[2] == "true":
else: tree.get_person(cmd[1]).set_sex(True)
tree.get_person(cmd[1]).set_sex(False) else:
except KeyError: tree.get_person(cmd[1]).set_sex(False)
print("Error: no such person " + cmd[1]) except KeyError:
elif cmd[0] == "relationship": print("Error: no such person " + cmd[1])
try: elif cmd[0] == "relationship":
if cmd[1] == cmd[2]: try:
print("Error: inputs must be distinct") if cmd[1] == cmd[2]:
else: print("Error: inputs must be distinct")
print(tree.relationship(tree.get_person(cmd[1]), tree.get_person(cmd[2]))) else:
except KeyError: print(tree.relationship(tree.get_person(cmd[1]), tree.get_person(cmd[2])))
print("Error: one or both persons do not exist") except KeyError:
elif cmd[0] == "load": print("Error: one or both persons do not exist")
try: elif cmd[0] == "load":
tree = openfile(cmd[1]) try:
print(cmd[1] + ".ft has been loaded") tree = openfile(cmd[1])
except FileNotFoundError: print(cmd[1] + ".ft has been loaded")
print("Error: File not found") except FileNotFoundError:
elif cmd[0] == "save": print("Error: File not found")
tree.find_root() elif cmd[0] == "save":
tree.precompute() tree.find_root()
savefile(cmd[1], tree) tree.precompute()
print("file saved") savefile(cmd[1], tree)
elif cmd[0] == "bye": print("file saved")
print("Goodbye!") elif cmd[0] == "bye":
break print("Goodbye!")
else: break
print("Error: no such command") else:
print("Error: no such command")
def main(): def main():
r"""For testing. r"""For testing.
A
A / \
/ \ B C
B C / \
/ \ D E
D E
""" """
ftree = FamilyTree() ftree = FamilyTree()
A = Person("A", 69, True) A = Person("A", 69, True)
...@@ -502,18 +508,21 @@ def main(): ...@@ -502,18 +508,21 @@ def main():
print("The lca of {} and {} is {}".format(A, B, ftree.find_lca("A", "B"))) print("The lca of {} and {} is {}".format(A, B, ftree.find_lca("A", "B")))
# parent/child # parent/child
print("relationship {} is {}'s: {}".format(B, A, ftree.relationship(A, B))) print("{} is {}'s: {}".format(B, A, ftree.relationship(A, B)))
print("relationship {} is {}'s: {}".format(A, B, ftree.relationship(B, A))) print("{} is {}'s: {}".format(A, B, ftree.relationship(B, A)))
# grandparent/granchild
print("")
# sibling # sibling
print("relationship {} is {}'s: {}".format(B, C, ftree.relationship(C, B))) print("{} is {}'s: {}".format(B, C, ftree.relationship(C, B)))
print("relationship {} is {}'s: {}".format(C, B, ftree.relationship(B, C))) print("{} is {}'s: {}".format(C, B, ftree.relationship(B, C)))
# nibling # nibling
print("relationship {} is {}'s: {}".format(D, C, ftree.relationship(C, D))) print("{} is {}'s: {}".format(D, C, ftree.relationship(C, D)))
print("relationship {} is {}'s: {}".format(C, D, ftree.relationship(D, C))) print("{} is {}'s: {}".format(C, D, ftree.relationship(D, C)))
print("") print("{} is {}'s: {}".format(A, D, ftree.relationship(D, A)))
print("{} is {}'s: {}".format(D, A, ftree.relationship(A, D)))
#test for opening and saving files # test for opening and saving files
ntree = openfile("chandelier") ntree = openfile("chandelier")
ntree.find_root() ntree.find_root()
...@@ -524,5 +533,6 @@ def main(): ...@@ -524,5 +533,6 @@ def main():
savefile("test", ntree) savefile("test", ntree)
if __name__ == "__main__": if __name__ == "__main__":
main() main()
family test
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
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