mogs 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mogs/__init__.py +17 -0
- mogs/_assertrewrite.py +44 -0
- mogs/_env.py +26 -0
- mogs/_run.py +111 -0
- mogs/_typst/preview/cmarker/0.1.10/LICENSE +7 -0
- mogs/_typst/preview/cmarker/0.1.10/lib.typ +462 -0
- mogs/_typst/preview/cmarker/0.1.10/plugin.wasm +0 -0
- mogs/_typst/preview/cmarker/0.1.10/typst.toml +11 -0
- mogs/_typst/preview/mitex/0.2.7/LICENSE +176 -0
- mogs/_typst/preview/mitex/0.2.7/lib.typ +1 -0
- mogs/_typst/preview/mitex/0.2.7/mitex.typ +45 -0
- mogs/_typst/preview/mitex/0.2.7/mitex.wasm +0 -0
- mogs/_typst/preview/mitex/0.2.7/specs/latex/standard.typ +1187 -0
- mogs/_typst/preview/mitex/0.2.7/specs/mod.typ +12 -0
- mogs/_typst/preview/mitex/0.2.7/specs/prelude.typ +230 -0
- mogs/_typst/preview/mitex/0.2.7/typst.toml +12 -0
- mogs/assignment.py +172 -0
- mogs/build.py +605 -0
- mogs/check.py +410 -0
- mogs/cli.py +272 -0
- mogs/config.py +619 -0
- mogs/environment.py +118 -0
- mogs/export.py +556 -0
- mogs/grader/__init__.py +1 -0
- mogs/grader/_process.py +383 -0
- mogs/grader/_user.py +7 -0
- mogs/grader/bundle.py +320 -0
- mogs/grader/child.py +317 -0
- mogs/grader/datafiles.py +54 -0
- mogs/grader/main.py +132 -0
- mogs/grader/protocol.py +175 -0
- mogs/grader/results.py +337 -0
- mogs/grader/sandbox.py +462 -0
- mogs/grader/templates/__init__.py +38 -0
- mogs/grader/templates/run_autograder +3 -0
- mogs/grader/templates/setup.sh +28 -0
- mogs/loader.py +260 -0
- mogs/pdf.py +676 -0
- mogs/regrade.py +223 -0
- mogs/requirements.py +232 -0
- mogs/runner.py +281 -0
- mogs/scaffold.py +155 -0
- mogs/selfcheck.py +173 -0
- mogs/source.py +1438 -0
- mogs/transform.py +769 -0
- mogs-0.7.0.dist-info/METADATA +17 -0
- mogs-0.7.0.dist-info/RECORD +49 -0
- mogs-0.7.0.dist-info/WHEEL +4 -0
- mogs-0.7.0.dist-info/entry_points.txt +3 -0
mogs/loader.py
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"""Load a student's marimo notebook and bind the names the tests use.
|
|
2
|
+
|
|
3
|
+
Functions written with `@app.function` are plain module attributes, so importing the notebook
|
|
4
|
+
runs only its setup cell and those definitions. The build makes sure every name the tests read
|
|
5
|
+
is one of these (see `source.validate`). The only way a test then reads a name that is not a
|
|
6
|
+
module attribute is a student edit, such as moving a function into a plain cell.
|
|
7
|
+
|
|
8
|
+
Such a name comes from a single `app.run()`, which executes the whole notebook. This fallback
|
|
9
|
+
is designed behavior. It is all-or-nothing: if any cell raises, every name that needed the
|
|
10
|
+
fallback becomes `Missing`, including names from healthy cells. The reason names the line
|
|
11
|
+
that raised and the exception. When the run killed or hung an earlier grading child, the
|
|
12
|
+
grader binds again with the run turned off (`run_failed`), so names that need no run still
|
|
13
|
+
work. marimo has no public API to run only the cells some names depend on, so the fallback
|
|
14
|
+
runs them all.
|
|
15
|
+
|
|
16
|
+
Loading never raises. Names that cannot be bound come back as `Missing` placeholders. Using
|
|
17
|
+
one in a test, by calling it, comparing it, reading an attribute and so on, raises a readable
|
|
18
|
+
error with the reason, so one broken name costs only the tests that use it.
|
|
19
|
+
|
|
20
|
+
`app.run()` behaves the same whether or not `PYTEST_CURRENT_TEST` is set (checked on marimo
|
|
21
|
+
0.24.2 and 0.25.2), so binding inside a pytest test needs no environment changes.
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
import hashlib
|
|
25
|
+
import importlib.util
|
|
26
|
+
import sys
|
|
27
|
+
import traceback
|
|
28
|
+
from collections.abc import Iterable, Mapping
|
|
29
|
+
from pathlib import Path
|
|
30
|
+
from types import ModuleType
|
|
31
|
+
from typing import NoReturn, cast
|
|
32
|
+
|
|
33
|
+
import marimo
|
|
34
|
+
|
|
35
|
+
from mogs.runner import cap, exception_summary
|
|
36
|
+
|
|
37
|
+
NOT_DEFINED = "it is not defined in your notebook"
|
|
38
|
+
|
|
39
|
+
type Loaded = tuple[ModuleType | None, str | None]
|
|
40
|
+
type Defs = tuple[Mapping[str, object], str | None]
|
|
41
|
+
|
|
42
|
+
_modules: dict[Path, Loaded] = {}
|
|
43
|
+
_defs: dict[Path, Defs] = {}
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class Missing:
|
|
47
|
+
"""Stand-in for a name the notebook does not provide.
|
|
48
|
+
|
|
49
|
+
Any use a test is likely to make raises `RuntimeError` with the reason: calling it, reading
|
|
50
|
+
a public attribute, comparing it, arithmetic, `bool`, `len`, iteration, indexing, `in`,
|
|
51
|
+
`str`, `format`, number conversions and NumPy's `__array__`. So `assert n == 3` fails with
|
|
52
|
+
the reason rather than a bare `AssertionError`. Private attributes raise `AttributeError`,
|
|
53
|
+
and `repr`, `hash` and pickling work, so introspection, pickling and pytest leave it alone.
|
|
54
|
+
|
|
55
|
+
Args:
|
|
56
|
+
name: The name the tests asked for.
|
|
57
|
+
reason: Why it is unavailable, shown to the student.
|
|
58
|
+
"""
|
|
59
|
+
|
|
60
|
+
__slots__ = ("_name", "_reason")
|
|
61
|
+
|
|
62
|
+
def __init__(self, name: str, reason: str) -> None:
|
|
63
|
+
self._name = name
|
|
64
|
+
self._reason = reason
|
|
65
|
+
|
|
66
|
+
def _unavailable(self, *args: object, **kwargs: object) -> NoReturn:
|
|
67
|
+
"""Raise the readable error."""
|
|
68
|
+
|
|
69
|
+
__tracebackhide__ = True
|
|
70
|
+
|
|
71
|
+
raise RuntimeError(f"`{self._name}` is unavailable: {self._reason}")
|
|
72
|
+
|
|
73
|
+
# Assigned, not defined, so each use raises from the one method above. Defining `__eq__`
|
|
74
|
+
# would make the class unhashable, hence the explicit `__hash__`.
|
|
75
|
+
__call__ = _unavailable
|
|
76
|
+
__eq__ = __ne__ = __lt__ = __le__ = __gt__ = __ge__ = _unavailable
|
|
77
|
+
__hash__ = object.__hash__
|
|
78
|
+
__bool__ = __len__ = __iter__ = __contains__ = _unavailable
|
|
79
|
+
__getitem__ = __setitem__ = __delitem__ = _unavailable
|
|
80
|
+
__add__ = __radd__ = __sub__ = __rsub__ = __mul__ = __rmul__ = _unavailable
|
|
81
|
+
__truediv__ = __rtruediv__ = __floordiv__ = __rfloordiv__ = _unavailable
|
|
82
|
+
__mod__ = __rmod__ = __pow__ = __rpow__ = __matmul__ = __rmatmul__ = _unavailable
|
|
83
|
+
__divmod__ = __rdivmod__ = __lshift__ = __rlshift__ = __rshift__ = __rrshift__ = _unavailable
|
|
84
|
+
__and__ = __rand__ = __or__ = __ror__ = __xor__ = __rxor__ = _unavailable
|
|
85
|
+
__neg__ = __pos__ = __abs__ = __invert__ = _unavailable
|
|
86
|
+
__int__ = __float__ = __complex__ = __index__ = __round__ = _unavailable
|
|
87
|
+
__str__ = __format__ = __array__ = _unavailable
|
|
88
|
+
|
|
89
|
+
def __getattr__(self, attr: str) -> object:
|
|
90
|
+
__tracebackhide__ = True
|
|
91
|
+
|
|
92
|
+
if attr.startswith("_"):
|
|
93
|
+
raise AttributeError(attr)
|
|
94
|
+
|
|
95
|
+
self._unavailable()
|
|
96
|
+
|
|
97
|
+
def __getstate__(self) -> tuple[str, str]:
|
|
98
|
+
return self._name, self._reason
|
|
99
|
+
|
|
100
|
+
def __setstate__(self, state: tuple[str, str]) -> None:
|
|
101
|
+
self._name, self._reason = state
|
|
102
|
+
|
|
103
|
+
def __repr__(self) -> str:
|
|
104
|
+
return f"Missing({self._name!r}, {self._reason!r})"
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def unavailable(value: object) -> str | None:
|
|
108
|
+
"""Why `value` is unavailable when it is a `Missing`, else `None`."""
|
|
109
|
+
|
|
110
|
+
# `type()` cannot be fooled by a `__class__` property, unlike `isinstance`.
|
|
111
|
+
if issubclass(type(value), Missing):
|
|
112
|
+
return cast(Missing, value)._reason
|
|
113
|
+
|
|
114
|
+
return None
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def load(path: Path) -> Loaded:
|
|
118
|
+
"""Import a notebook as a module, once per path in this process.
|
|
119
|
+
|
|
120
|
+
The module gets a name unique to the path and stays in `sys.modules`, which dataclasses and
|
|
121
|
+
pickling need. Never raises: any `BaseException` from the notebook becomes the error string.
|
|
122
|
+
|
|
123
|
+
Args:
|
|
124
|
+
path: The notebook file.
|
|
125
|
+
|
|
126
|
+
Returns:
|
|
127
|
+
`(module, None)` on success, `(None, reason)` on failure.
|
|
128
|
+
"""
|
|
129
|
+
|
|
130
|
+
path = path.resolve()
|
|
131
|
+
|
|
132
|
+
if path in _modules:
|
|
133
|
+
return _modules[path]
|
|
134
|
+
|
|
135
|
+
digest = hashlib.sha256(str(path).encode()).hexdigest()[:16]
|
|
136
|
+
name = f"_mogs_submission_{digest}"
|
|
137
|
+
|
|
138
|
+
try:
|
|
139
|
+
spec = importlib.util.spec_from_file_location(name, path)
|
|
140
|
+
if spec is None or spec.loader is None:
|
|
141
|
+
raise ImportError(f"cannot load {path.name}")
|
|
142
|
+
|
|
143
|
+
module = importlib.util.module_from_spec(spec)
|
|
144
|
+
sys.modules[name] = module
|
|
145
|
+
spec.loader.exec_module(module)
|
|
146
|
+
loaded: Loaded = (module, None)
|
|
147
|
+
except BaseException as exc:
|
|
148
|
+
sys.modules.pop(name, None)
|
|
149
|
+
loaded = (None, cap(f"your notebook failed to import ({exception_summary(exc)})"))
|
|
150
|
+
|
|
151
|
+
_modules[path] = loaded
|
|
152
|
+
return loaded
|
|
153
|
+
|
|
154
|
+
|
|
155
|
+
def bind(path: Path, names: Iterable[str], *, run_failed: str | None = None) -> dict[str, object]:
|
|
156
|
+
"""Look up names in a notebook, falling back to `app.run()` once for the rest.
|
|
157
|
+
|
|
158
|
+
Names come from module attributes, read through the module's `__dict__` so a module-level
|
|
159
|
+
`__getattr__` never runs. Named marimo cells also appear as module attributes but are cell
|
|
160
|
+
objects, not the values the tests want, so they are skipped.
|
|
161
|
+
|
|
162
|
+
If any name needs the fallback, or the notebook already ran in this process, every name the
|
|
163
|
+
run defined comes from the run. The run re-executes the setup cell and every top-level
|
|
164
|
+
definition, so mixing its values with module attributes would split classes and module
|
|
165
|
+
state in two. Module attributes cover only names the run did not define, or every name when
|
|
166
|
+
the run failed. Names still unbound become `Missing`. Never raises.
|
|
167
|
+
|
|
168
|
+
Args:
|
|
169
|
+
path: The notebook file.
|
|
170
|
+
names: Names the tests use.
|
|
171
|
+
run_failed: Why an earlier run of this notebook failed, in another process. When set,
|
|
172
|
+
the notebook does not run, and names that need the run become `Missing` with
|
|
173
|
+
this reason.
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
Each name mapped to its value or a `Missing`, in the order given.
|
|
177
|
+
"""
|
|
178
|
+
|
|
179
|
+
names = list(names)
|
|
180
|
+
module, error = load(path)
|
|
181
|
+
|
|
182
|
+
if module is None:
|
|
183
|
+
return {name: Missing(name, error or NOT_DEFINED) for name in names}
|
|
184
|
+
|
|
185
|
+
try:
|
|
186
|
+
return _bind(path.resolve(), module, names, run_failed)
|
|
187
|
+
except BaseException as exc:
|
|
188
|
+
reason = cap(f"reading your notebook failed ({exception_summary(exc)})")
|
|
189
|
+
return {name: Missing(name, reason) for name in names}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def _bind(
|
|
193
|
+
path: Path, module: ModuleType, names: list[str], run_failed: str | None
|
|
194
|
+
) -> dict[str, object]:
|
|
195
|
+
"""Bind names from the module and, when needed, from one run of the notebook."""
|
|
196
|
+
|
|
197
|
+
# `type()` cannot be fooled by a `__class__` property, unlike `isinstance`.
|
|
198
|
+
attrs = {
|
|
199
|
+
name: value
|
|
200
|
+
for name, value in vars(module).items()
|
|
201
|
+
if not issubclass(type(value), marimo.Cell)
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
app = attrs.get("app")
|
|
205
|
+
needs_run = any(name not in attrs for name in names)
|
|
206
|
+
defs: Mapping[str, object] = {}
|
|
207
|
+
run_error = None
|
|
208
|
+
|
|
209
|
+
if run_failed is not None:
|
|
210
|
+
run_error = f"it needs the whole notebook to run, which failed: {run_failed}"
|
|
211
|
+
elif issubclass(type(app), marimo.App) and (needs_run or path in _defs):
|
|
212
|
+
defs, run_error = _run(path, cast(marimo.App, app))
|
|
213
|
+
|
|
214
|
+
found = {**attrs, **defs}
|
|
215
|
+
|
|
216
|
+
return {
|
|
217
|
+
name: found[name] if name in found else Missing(name, run_error or NOT_DEFINED)
|
|
218
|
+
for name in names
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _run(path: Path, app: marimo.App) -> Defs:
|
|
223
|
+
"""Run the whole notebook once per path and keep its definitions."""
|
|
224
|
+
|
|
225
|
+
if path in _defs:
|
|
226
|
+
return _defs[path]
|
|
227
|
+
|
|
228
|
+
try:
|
|
229
|
+
_, defs = app.run()
|
|
230
|
+
result: Defs = (dict(defs), None)
|
|
231
|
+
except BaseException as exc:
|
|
232
|
+
result = ({}, cap(run_failure(path, exc)))
|
|
233
|
+
|
|
234
|
+
_defs[path] = result
|
|
235
|
+
return result
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def run_failure(path: Path, exc: BaseException) -> str:
|
|
239
|
+
"""Why `app.run()` failed: the notebook line that raised, when known, and the exception.
|
|
240
|
+
|
|
241
|
+
The line is that of the outermost traceback entry in the notebook file, which is the
|
|
242
|
+
statement in the failing cell.
|
|
243
|
+
"""
|
|
244
|
+
|
|
245
|
+
summary = exception_summary(exc)
|
|
246
|
+
|
|
247
|
+
try:
|
|
248
|
+
lines = [
|
|
249
|
+
frame.lineno
|
|
250
|
+
for frame in traceback.extract_tb(exc.__traceback__)
|
|
251
|
+
if Path(frame.filename) == path
|
|
252
|
+
]
|
|
253
|
+
except Exception:
|
|
254
|
+
lines = []
|
|
255
|
+
|
|
256
|
+
match lines:
|
|
257
|
+
case [int() as line, *_]:
|
|
258
|
+
return f"running your notebook failed: line {line} raised {summary}"
|
|
259
|
+
case _:
|
|
260
|
+
return f"running your notebook failed ({summary})"
|