|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import importlib.util |
| 4 | +import sys |
| 5 | +import traceback |
| 6 | +from collections.abc import Callable |
| 7 | +from pathlib import Path |
| 8 | + |
| 9 | +student_code_path: str = sys.argv[2] |
| 10 | +username: str = sys.argv[3] |
| 11 | +log_file = Path(sys.argv[4]) |
| 12 | + |
| 13 | +test_cases = ( |
| 14 | + (1, 2), |
| 15 | + (3, 4), |
| 16 | + (1000, 20345), |
| 17 | + (54, 78), |
| 18 | +) |
| 19 | + |
| 20 | +secret_test_cases = ( |
| 21 | + (127, 856.7), |
| 22 | + (789.101, 101112), |
| 23 | +) |
| 24 | + |
| 25 | + |
| 26 | +def import_module(modname: str = "student_submission", func_name="add_num") -> Callable: |
| 27 | + """Imports the student submission and returns the function with the given name. |
| 28 | +
|
| 29 | + It accomplishes this by utilizing a lot of the machinery provided by the python module |
| 30 | + ``importlib``. If you don't understand how it works, feel free to just copy paste this |
| 31 | + function and pass a different value for the ``func_name`` parameter. |
| 32 | + """ |
| 33 | + |
| 34 | + spec = importlib.util.spec_from_file_location(modname, student_code_path) |
| 35 | + |
| 36 | + # these are probably grader errors and not student errors, so we raise an |
| 37 | + # exception instead of printing |
| 38 | + if spec is None: |
| 39 | + raise ImportError(f"Could not load spec for module {student_code_path!r}") |
| 40 | + if spec.loader is None: |
| 41 | + raise ImportError(f"No loader found for module {student_code_path!r} with {spec=!r}") |
| 42 | + |
| 43 | + submission = importlib.util.module_from_spec(spec) |
| 44 | + |
| 45 | + if submission is None: |
| 46 | + raise ImportError("Module spec is None") |
| 47 | + |
| 48 | + sys.modules[modname] = submission |
| 49 | + |
| 50 | + try: |
| 51 | + spec.loader.exec_module(submission) |
| 52 | + except Exception: |
| 53 | + # this traceback could provide sensitive information, so we don't provide it to students |
| 54 | + print("Could not test submission, an exception was raised while initializing.") |
| 55 | + log_file.write_text(f"Student {username} import error:\n\n" + traceback.format_exc()) |
| 56 | + # it's not our fault so we exit 0 |
| 57 | + sys.exit(0) |
| 58 | + |
| 59 | + try: |
| 60 | + func = getattr(submission, func_name) |
| 61 | + except AttributeError: |
| 62 | + print(f"Could not find function {func_name!r}") |
| 63 | + sys.exit(0) |
| 64 | + |
| 65 | + return func |
| 66 | + |
| 67 | + |
| 68 | +def run_submission(func: Callable) -> None: |
| 69 | + # grade submissions |
| 70 | + failing_cases = 0 |
| 71 | + tol = 1e-8 |
| 72 | + for x, y in test_cases: |
| 73 | + try: |
| 74 | + # take into account floating point error |
| 75 | + if func(x, y) - (x + y) > tol: |
| 76 | + print(f"Failed on test case {x=},{y=}") |
| 77 | + failing_cases += 1 |
| 78 | + except Exception: |
| 79 | + print(f"Code errored on test case {x=},{y=}") |
| 80 | + failing_cases += 1 |
| 81 | + |
| 82 | + for idx, (x, y) in enumerate(secret_test_cases): |
| 83 | + try: |
| 84 | + if func(x, y) - (x + y) > tol: |
| 85 | + print(f"Failed on secret test case {idx}") |
| 86 | + failing_cases += 1 |
| 87 | + except Exception: |
| 88 | + print(f"Code errored on secret test case {idx}") |
| 89 | + failing_cases += 1 |
| 90 | + |
| 91 | + raw = 1 - failing_cases / (len(test_cases) + len(secret_test_cases)) |
| 92 | + # print score, rounding to two decimal places |
| 93 | + print(f"Score: {raw * 100:.2f}%") |
| 94 | + |
| 95 | + |
| 96 | +def main() -> None: |
| 97 | + submission = import_module() |
| 98 | + run_submission(submission) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + main() |
0 commit comments