-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtree_methods.py
More file actions
102 lines (76 loc) · 3.23 KB
/
tree_methods.py
File metadata and controls
102 lines (76 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import tree_class
import tree_drawer
def input_validator(message, options, error_message="Yikes! Command was not found...", type=int):
response = type(input(message))
if response not in options:
print(error_message)
return input_validator(message, options, error_message)
return response
def create_tree():
print("You selected: Create a new tree")
label = input("Label: ")
tree_class.Tree(label, [])
def edit_tree():
if len(tree_class.Tree.all_trees) == 0:
print("You haven't created any trees yet")
return
print("You selected: Edit an existing tree")
message = "ID of the tree you would like to edit: "
options = list(tree_class.Tree.all_trees.keys())
error_message = "Sorry! It doesn't look like this tree exists"
tree_id = input_validator(message, options, error_message)
tree_to_edit = tree_class.Tree.all_trees[tree_id]
message = "Please select the change you would like to make to this tree:\n" \
"1) Change a label\n" \
"2) Add a branch\n" \
"3) Delete a branch\n" \
"4) Delete the tree\n" \
"Option Number: "
options = [1, 2, 3, 4]
option = input_validator(message, options)
if option == 1:
print("You selected: Change a label")
old = input("Old label: ")
new = input("New label: ")
tree_to_edit.replace_label(old, new)
elif option == 2:
print("You selected: Add a branch")
branch_label = input("Label of new branch: ")
tree_to_edit.add_branch(branch_label)
elif option == 3:
branch_id = int(input("ID of branch to delete: "))
if branch_id not in [b.tree_id for b in tree_to_edit.branches]:
print("This branch is not in the tree")
else:
tree_to_edit.delete(branch_id)
elif option == 4:
tree_to_edit.delete(tree_id)
def visualize_tree():
if len(tree_class.Tree.all_trees) == 0:
print("You haven't created any trees yet")
return
print("You selected: Visualize a tree")
message = "ID of the tree you would like to visualize: "
options = list(tree_class.Tree.all_trees.keys())
error_message = "Sorry! It doesn't look like this tree exists"
tree_id = input_validator(message, options, error_message)
tree_to_visualize = tree_class.Tree.all_trees[tree_id]
tree_drawer.draw_tree(tree_to_visualize)
def write_code():
print("You selected: Write code manually")
entered_code = input("Please type your code below:\n")
tree_class.run_code(entered_code)
def print_trees():
print("You selected: Print existing trees")
[print("Tree ID: {0}\n{1}".format(tree_id, tree)) for tree_id, tree in tree_class.Tree.all_trees.items()]
def print_code():
if len(tree_class.Tree.all_trees) == 0:
print("You haven't created any trees yet")
return
print("You selected: Print a tree's code")
message = "ID of the tree who's code you would like to print: "
options = list(tree_class.Tree.all_trees.keys())
error_message = "Sorry! It doesn't look like this tree exists"
tree_id = input_validator(message, options, error_message)
tree_to_print = tree_class.Tree.all_trees[tree_id]
print(tree_to_print)