-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfamily-tree.py
385 lines (308 loc) · 11.5 KB
/
family-tree.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import subprocess
from dbscripts import * # all functions relating to the sqlite3 database
from person_init import * # all functions relating to the Person object
excel_path = r"C:\Program Files\Microsoft Office\root\Office16\EXCEL.EXE"
file_path = r"C:\Users\ASHil\PycharmProjects\Family-Tree\family-tree-data.xlsx"
people = []
person_name_dict = {}
person_id_dict = {}
def show_kids(person):
for child in person.children:
print(child)
def print_table():
for person in people:
print((f"{'ID:'} {person.parse_id} \t {'Name:'} {person.first_name} {person.last_name} \t "
f"{'Class of '}{person.class_year}").expandtabs(15))
def get_person_by_id(person_id):
if person_id == '':
pass
else:
person_id = int(person_id)
for person in people:
if person.parse_id == person_id:
print(person.first_name, person.last_name, 'class of', person.class_year)
return person
print("No person found with ID:", person_id)
print()
return None
def get_person_by_last():
print()
print('*****************************************')
print('Enter 9 to go back')
print('NOTE: Input is case sensitive')
people_list = []
while True:
print()
get = input('Enter last name: ')
if get == '9':
print('*****************************************')
break
else:
for person in people:
if person.last_name == get:
print(f"Parse ID for {person.first_name} {person.last_name}: {person.parse_id}")
people_list.append(person)
if len(people_list) == 0:
print("No people found with last name:", get)
def get_person_by_year(person_year):
people_list = []
person_year = int(person_year)
for person in people:
if person.class_year == person_year:
print(f"{person.first_name} {person.last_name}")
people_list.append(person)
if not people_list:
print(f"No people found with class of {person_year}")
return people_list
def path_selection():
global file_path, excel_path
while True:
print()
print('Enter the path to either your EXCEL.EXE or the family-tree-data.xlsx')
print('You will select which path it is in the next step')
new_path = input('Enter path: ')
raw_path = r"{}".format(new_path)
print()
print(f"Path: {raw_path}")
selected = input('[1] Set path for EXCEL.EXE \n[2] Set path for family-tree-data.xlsx '
'\n[3] Change input \n[9] Go back \nInput choice: ')
match selected:
case'1':
excel_path = raw_path
print()
break
case '2':
file_path = raw_path
print()
break
case '3':
print()
case '9':
print()
break
def data_menu():
print('***************** Data ******************')
while True:
global people
print('Select action:')
print('[1] Print table')
print('[2] Open Excel')
print('[3] Re-parse data')
print('[4] Edit paths')
print('[5] Display paths')
print('[9] Go back')
selected = input('Input choice: ')
match selected:
case '1':
print()
print_table()
print()
case '2':
try:
subprocess.Popen([excel_path, file_path])
print('For changes to take effect you MUST close Excel AND then re-parse the data')
print()
except Exception:
print('Error, verify correct paths')
print()
case '3':
clear_db_people()
people, person_name_dict, person_id_dict = initialize_people()
print()
case '4':
path_selection()
case '5':
print()
print(f'Path to EXCEL.EXE: {excel_path}')
print(f'Path to family-tree-data.xlsx: {file_path:}')
print()
case '9':
print('*****************************************')
print()
break
case '':
print()
def search_menu():
print('**************** Search *****************')
while True:
print('Select search criterion:')
print('[1] ID')
print('[2] Last name')
print('[3] Class year')
print('[9] Go back')
selected = input('Input choice: ')
match selected:
case '1':
print()
get_person_by_id(input('Enter ID: '))
case '2':
get_person_by_last()
print()
case '3':
print()
print('*****************************************')
get_person_by_year(input('Enter class year: '))
print('*****************************************')
case '9':
print('*****************************************')
print()
break
case '':
print()
print()
def relationships_menu():
print('************* Relationships *************')
while True:
print('Select action:')
print('[1] Immediate family')
print('[2] Show all descendants')
print('[3] Show all ancestors')
print('[9] Go back')
selected = input('Input choice: ')
match selected:
case '1':
show_relationships()
case '2':
show_descendants()
case '3':
show_ancestors()
case '9':
print('*****************************************')
print()
break
print()
def credits_menu():
print('**************** Credits ****************')
print('Historians: \t Tanner Hansard \'23 and Chris Huser \'22'.expandtabs(10))
print('Traditions Chairs: \t Owen Dunston \'23, Aidan Hill \'23, and Liam Stevens \'23'.expandtabs(10))
print('Vice President: \t Miles Baker \'23'.expandtabs(10))
print('Comp Sci Major: \t Joshua Wood \'23'.expandtabs(10))
print('*****************************************')
print()
def print_nested_list(nested_list, output_length):
ancestors = []
def is_last_in_list(place, parent_list_length):
return place[len(place)-1] >= parent_list_length - 1
def getEl (place):
ancestors.clear()
ancestors.append(False)
element = nested_list
parent_list_length = len(nested_list)
for i, index in enumerate(place):
if index >= len(element): raise StopIteration
if isinstance(element, list): parent_list_length = len(element)
element = element[index]
ancestors.append(False if is_last_in_list(place[0:i+1], parent_list_length) else True)
return(element, parent_list_length)
PIPE = " │" + ' ' * (output_length - 2)
ELBOW = " └─" + '─' * (output_length - 2)
TEE = " ├─" + '─' * (output_length - 2)
SPACE = ' ' * (output_length)
place = []
keepGoing = True
try:
while keepGoing:
element, parent_list_length = getEl(place)
while isinstance(element, list):
element = element[0]
place.append(0)
if isinstance(element, list): parent_list_length = len(element)
# print element
last_in_list = is_last_in_list(place, parent_list_length)
pipes = ""
for ancestor in ancestors[0:len(ancestors)-1]:
if ancestor:
pipes += PIPE
else:
pipes += SPACE
# isCorner = last_in_list
isCorner = True # always print elbows
thisLine = pipes + (ELBOW if isCorner else TEE) + str(element)
print(pipes + PIPE)
print(thisLine)
while last_in_list:
# if at the end of the parent list, then remove the last element
place.pop(len(place)-1)
if not place:
keepGoing = False
break
element, parent_list_length = getEl(place)
last_in_list = is_last_in_list(place, parent_list_length)
# increment the new last element by one
if keepGoing and not last_in_list: place[len(place)-1] += 1
except StopIteration:
print("StopIteration exception raised.", place)
pass
def create_rec_list(this_person, direction, depth, maxDepth):
nodes = this_person.children if direction == "DOWN" else this_person.parents if direction == "UP" else []
if nodes and depth < maxDepth:
node_list = [person_name_dict[this_person.parse_id]]
for node in nodes:
node_list.append(create_rec_list(person_id_dict[node], direction, depth + 1, maxDepth))
return node_list
else:
return person_name_dict[this_person.parse_id]
def show_ancestors():
print()
subject = int(input('Enter person ID: '))
maxDepth = input('How many generations of ancestors? ')
print()
subject = person_id_dict[subject]
print("Showing ancestors for", subject.first_name + ' ' + subject.last_name)
rl = create_rec_list(subject, "UP", 0, (int(maxDepth) if maxDepth else 1000))
print_nested_list(rl, 5)
print('*****************************************')
print()
def show_descendants():
print()
subject = int(input('Enter person ID: '))
maxDepth = input('How many generations of descendants? ')
print()
print('*****************************************')
subject = person_id_dict[subject]
print("Showing descendants for", subject.first_name + ' ' + subject.last_name)
rl = create_rec_list(subject, "DOWN", 0, (int(maxDepth) if maxDepth else 1000))
print_nested_list(rl, 5)
print('*****************************************')
print()
def show_relationships():
print()
subject = int(input('Enter person ID: '))
print()
subject = person_id_dict[subject]
tense = 'old man' if len(subject.parents) == 1 else 'old men'
print(f'{subject.first_name} {subject.last_name}\'s {tense}:')
for parent in subject.parents:
if parent:
print(person_name_dict[parent])
print(f'\n{subject.first_name} {subject.last_name}\'s buffo:')
for child in subject.children:
if child:
print(person_name_dict[child])
print()
def main():
print('Welcome to the Singing Cadet family tree project! Here you can lookup any recorded member to see their '
'family tree!')
print('Github Repo: https://github.com/ASHill11/Family-Tree')
print()
while True:
print('*************** Main Menu ***************')
print('[1] Search')
print('[2] Relationships')
print('[7] Data')
print('[8] Credits')
print('[9] Exit')
selected = input('Input choice: ')
print()
match selected:
case '1':
search_menu()
case '2':
relationships_menu()
case '7':
data_menu()
case '8':
credits_menu()
case '9':
exit()
main()