-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
executable file
·88 lines (67 loc) · 2.08 KB
/
example.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
#!/usr/bin/python3
"""
Module to test output from Holberton test templates located at ./templates
"""
choices = []
class Choice:
"""
Choice class that represent a template choice
name: Choice name
callback: callable to use import
"""
def __init__(self, name: str, template_name: str) -> None:
"""
Initialize the choice with a name and a callback
"""
self.name = name
self.callback = lambda: __import__(template_name)
choices.append(self)
def select(self) -> None:
"""
Select the choice and execute the callback
"""
try:
self.callback()
except Exception as err:
print(f"\033[91mDo you have complete the task ?\033[0m")
print(f"\033[91m[Stack: {err}\033[0m")
def on_error() -> None:
"""
Error message when the user input is invalid
"""
print("\033[91mInvalid choice, please retry\033[0m\n")
app()
def app() -> None:
"""
Program entry that is triggered while all Choice are setup and ready.
Can be recursive on fail.
"""
try:
selection = input("Enter test index to select it: ")
if (selection.isspace() or selection.__len__() == 0):
print("Operation aborted.")
exit()
if (not selection.isdigit()):
on_error()
return
choice = int(selection) - 1
if (type(choices[choice]) == Choice):
choices[choice].select()
else:
on_error()
except KeyboardInterrupt:
print()
exit()
Choice("BaseModel (Task 3)", "examples.test_base_model")
Choice("BaseModel (Task 4)", "examples.test_base_model_dict")
Choice("BaseModel (Task 5)", "examples.test_save_reload_base_model")
Choice("User (Task 8)", "examples.test_user")
# Create your choices here:
# Choice("My Choice", my_choice_location)
print("Choose a template to test\n")
print("Choices:")
for i in range(len(choices)):
print(f"\t\033[96m{i + 1}\033[0m - {choices[i].name}")
print("\n")
# Start the program
app()