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,12 +378,14 @@ def savefile(filename, tree): #Save Method ...@@ -374,12 +378,14 @@ 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():
"""Open command line."""
tree = FamilyTree() tree = FamilyTree()
while True: while True:
cmd = input().split() cmd = input().split()
...@@ -418,7 +424,7 @@ def commandLine(): ...@@ -418,7 +424,7 @@ def commandLine():
print("Error: no such person " + cmd[1]) print("Error: no such person " + cmd[1])
elif cmd[0] == "edit-name": elif cmd[0] == "edit-name":
try: try:
if tree.family_tree.get(cmd[2]) == None: if tree.family_tree.get(cmd[2]) is None:
tree.get_person(cmd[1]).set_name(cmd[2]) tree.get_person(cmd[1]).set_name(cmd[2])
tree.family_tree[cmd[2]] = tree.family_tree[cmd[1]] tree.family_tree[cmd[2]] = tree.family_tree[cmd[1]]
del tree.family_tree[cmd[1]] del tree.family_tree[cmd[1]]
...@@ -464,9 +470,9 @@ def commandLine(): ...@@ -464,9 +470,9 @@ def commandLine():
else: else:
print("Error: no such command") print("Error: no such command")
def main(): def main():
r"""For testing. r"""For testing.
A A
/ \ / \
B C B C
...@@ -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