-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (120 loc) · 5.08 KB
/
main.py
File metadata and controls
144 lines (120 loc) · 5.08 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
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
#!/usr/bin/env python3
"""
Helper file for Growing Code.
This file helps you test your exercises easily.
Just run: python3 main.py
How it works:
1. It tries to import your exercise files (like ft_plot_area.py)
2. It calls your functions to test them
3. If there's an error, it tells you what went wrong
Make sure your exercise files are in the same folder as this main.py file!
"""
import importlib
def import_from_exercises(exercise_file_name):
for i in range(8): # ex0 hasta ex7
try:
module_path = f"ex{i}.{exercise_file_name}"
return importlib.import_module(module_path)
except ModuleNotFoundError:
continue
raise ImportError(f"No se encontró {exercise_file_name} en ninguna carpeta ex")
def test_ft_exercise(exercise_file_name):
"""
This function tries to run one of your exercises.
For example: test_ft_exercise("ft_plot_area") will:
- Look for a file called ft_plot_area.py
- Import it
- Call the function ft_plot_area() inside it
"""
print(f"\n=== Testing {exercise_file_name} ===")
try:
# Import your exercise file desde ex0-ex7
ft_module = import_from_exercises(exercise_file_name)
# Get the function from your file
ft_function = getattr(ft_module, exercise_file_name)
# Special handling for ft_seed_inventory (Exercise 7)
if exercise_file_name == "ft_seed_inventory":
print("Testing with different seed types and units:\n")
ft_function("tomato", 15, "packets")
ft_function("carrot", 8, "grams")
ft_function("lettuce", 12, "area")
print("\nTesting with unknown unit:")
ft_function("basil", 5, "unknown")
else:
# Run your function normally (no parameters)
ft_function()
except ImportError:
print(f"❌ Could not find {exercise_file_name}.py")
print(
""" Make sure your file exists inside ex0-ex7
and each folder has __init__.py"""
)
except AttributeError:
print(f"❌ Could not find function {exercise_file_name}() in your file")
print(f" Make sure you have: def {exercise_file_name}():")
except TypeError as error:
msg = str(error)
print(f"❌ Type error: {error}")
if exercise_file_name == "ft_seed_inventory":
if "missing" in msg and "required positional argument" in msg:
print(
""" For exercise 7, make sure your
function takes parameters:"""
)
print(
f" def {exercise_file_name}"
"(seed_type: str, quantity: int, unit: str) -> None:"
)
else:
print(" Your function should not take any parameters")
except Exception as error:
print(f"❌ Error running your function: {error}")
print(" Check your code for syntax errors")
def main():
"""Run main function - this runs when you execute: python3 main.py ."""
print("🌱 Welcome to Growing Code! 🌱")
print("This helper will test your exercises for you.")
print("\nWhich exercise would you like to test?")
print()
print("0 - ft_hello_garden (Say hello to the garden community)")
print("1 - ft_garden_name (Display garden name)")
print("2 - ft_plot_area (Calculate garden plot area)")
print("3 - ft_harvest_total (Add up harvest weights)")
print("4 - ft_plant_age (Check if plant is ready)")
print("5 - ft_water_reminder (Check if plants need water)")
print("6 - ft_count_harvest (Count days to harvest)")
print("7 - ft_seed_inventory (Seed inventory with type hints)")
print("a - test all exercises")
print()
choice = input("Enter your choice: ")
if choice == "0":
test_ft_exercise("ft_hello_garden")
elif choice == "1":
test_ft_exercise("ft_garden_name")
elif choice == "2":
test_ft_exercise("ft_plot_area")
elif choice == "3":
test_ft_exercise("ft_harvest_total")
elif choice == "4":
test_ft_exercise("ft_plant_age")
elif choice == "5":
test_ft_exercise("ft_water_reminder")
elif choice == "6":
test_ft_exercise("ft_count_harvest_iterative")
test_ft_exercise("ft_count_harvest_recursive")
elif choice == "7":
test_ft_exercise("ft_seed_inventory")
elif choice == "a":
test_ft_exercise("ft_hello_garden")
test_ft_exercise("ft_garden_name")
test_ft_exercise("ft_plot_area")
test_ft_exercise("ft_harvest_total")
test_ft_exercise("ft_plant_age")
test_ft_exercise("ft_water_reminder")
test_ft_exercise("ft_count_harvest_iterative")
test_ft_exercise("ft_count_harvest_recursive")
test_ft_exercise("ft_seed_inventory")
else:
print("❌ Invalid choice! Please enter 0, 1, 2, 3, 4, 5, 6, 7, or a")
if __name__ == "__main__":
main()