-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
144 lines (103 loc) · 3.36 KB
/
main.py
File metadata and controls
144 lines (103 loc) · 3.36 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
import sys
import importlib
from pathlib import Path
import pandas as pd
import os
CEFR_LEVELS = ["A1", "A2", "B1", "B2", "C1", "C2"]
# ---------------------------------------
# Unit Test Loading
# ---------------------------------------
def load_unit_tests(path="unit_tests.csv"):
"""
Loads public unit tests.
Required columns:
- sentence
- source_level
- target_level
Optional:
- expected_output
"""
file_path = Path(path)
if not file_path.exists():
raise FileNotFoundError(f"Missing file: {path}")
df = pd.read_csv(file_path)
required_columns = {"sentence", "source_level", "target_level"}
if not required_columns.issubset(df.columns):
raise ValueError(
"unit_tests.csv must contain columns: "
"sentence, source_level, target_level"
)
return df
# ---------------------------------------
# Validation
# ---------------------------------------
def validate_levels(source_level, target_level):
if source_level not in CEFR_LEVELS:
raise ValueError(f"Invalid source CEFR level: {source_level}")
if target_level not in CEFR_LEVELS:
raise ValueError(f"Invalid target CEFR level: {target_level}")
# ---------------------------------------
# Student Import
# ---------------------------------------
def load_student_module(zid_name):
"""
Loads student module (e.g. z1234567.py).
The module must define:
transform_sentence(sentence, source_level, target_level)
"""
try:
module = importlib.import_module(zid_name)
except Exception as e:
raise ImportError(
f"Could not import {zid_name}.py. "
f"Ensure it is in the same directory as main.py. "
f"Original error: {e}"
)
if not hasattr(module, "transform_sentence"):
raise AttributeError(
f"{zid_name}.py must define "
f"transform_sentence(sentence, source_level, target_level)"
)
return module
# ---------------------------------------
# Runner
# ---------------------------------------
def run_tests(student_module, tests_df):
has_expected = "expected_output" in tests_df.columns
for i, row in enumerate(tests_df.itertuples(index=False), 1):
sentence = row.sentence
source = row.source_level
target = row.target_level
print(f"\nTest {i}")
print("Input sentence:", sentence)
print("Source level :", source)
print("Target level :", target)
try:
validate_levels(source, target)
output = student_module.transform_sentence(sentence, source, target)
except Exception as e:
print("ERROR:", e)
continue
print("Output :", output)
if has_expected:
print("Expected :", row.expected_output)
# ---------------------------------------
# Main
# ---------------------------------------
def main():
"""
Usage:
python main.py <zid>
Example:
python main.py z1234567
"""
if len(sys.argv) != 2:
print("Usage: python main.py <zid>")
print("Example: python main.py z1234567")
sys.exit(1)
zid_name = sys.argv[1]
tests = load_unit_tests("unit_tests.csv")
student_module = load_student_module(zid_name)
run_tests(student_module, tests)
if __name__ == "__main__":
main()