Commit fc4afe66 authored by Temujin's avatar Temujin

fixed relationship func. commandline added

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