-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
106 lines (83 loc) · 2.7 KB
/
Copy pathmain.py
File metadata and controls
106 lines (83 loc) · 2.7 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
#!/usr/bin/env python3
"""
Quantum Computing Exercise Runner
Usage: python3 main.py <task_number>
Example: python3 main.py 1
"""
import sys
import os
import importlib.util
def load_task(task_number):
"""Dynamically load and execute a task module"""
task_file = f"tasks/task{task_number}.py"
if not os.path.exists(task_file):
print(f"Error: Task {task_number} not found. File {task_file} does not exist.")
print("Available tasks:")
list_available_tasks()
return False
try:
# Load the task module dynamically
spec = importlib.util.spec_from_file_location(f"task{task_number}", task_file)
task_module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(task_module)
# Execute the task
if hasattr(task_module, "run_task"):
task_module.run_task()
else:
print(f"Error: Task {task_number} does not have a run_task() function.")
return False
return True
except Exception as e:
print(f"Error executing task {task_number}: {e}")
return False
def list_available_tasks():
"""List all available tasks in the tasks directory"""
tasks_dir = "tasks"
if not os.path.exists(tasks_dir):
print("No tasks directory found.")
return
task_files = [
f for f in os.listdir(tasks_dir) if f.startswith("task") and f.endswith(".py")
]
task_numbers = []
for task_file in task_files:
try:
# Extract task number from filename
task_num = task_file.replace("task", "").replace(".py", "")
if task_num.isdigit():
task_numbers.append(int(task_num))
except:
continue
task_numbers.sort()
if task_numbers:
for num in task_numbers:
print(f" Task {num}: python3 main.py {num}")
else:
print(" No valid tasks found.")
def main():
"""Main entry point"""
if len(sys.argv) != 2:
print("Usage: python3 main.py <task_number>")
print("\nExample:")
print(" python3 main.py 1")
print(" python3 main.py 2")
print("\nAvailable tasks:")
list_available_tasks()
sys.exit(1)
try:
task_number = int(sys.argv[1])
except ValueError:
print(f"Error: '{sys.argv[1]}' is not a valid task number.")
print("Task number must be an integer.")
sys.exit(1)
print(f"=" * 50)
print(f"Quantum Computing Exercise Runner")
print(f"=" * 50)
success = load_task(task_number)
if not success:
sys.exit(1)
print(f"=" * 50)
print(f"Task {task_number} completed!")
print(f"=" * 50)
if __name__ == "__main__":
main()