codehood-cli 0.5.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.
- codehood/__init__.py +0 -0
- codehood/__main__.py +4 -0
- codehood/actions/__init__.py +0 -0
- codehood/actions/base.py +119 -0
- codehood/actions/resources.py +30 -0
- codehood/api/__init__.py +0 -0
- codehood/api/base.py +256 -0
- codehood/api/generate.py +1370 -0
- codehood/api/generated.py +3818 -0
- codehood/api/openapi.py +134 -0
- codehood/api/pages.py +53 -0
- codehood/cli/__init__.py +64 -0
- codehood/cli/api.py +422 -0
- codehood/cli/attendance.py +136 -0
- codehood/cli/auth.py +106 -0
- codehood/cli/base.py +42 -0
- codehood/cli/chill.py +52 -0
- codehood/cli/exam.py +107 -0
- codehood/cli/grades.py +185 -0
- codehood/cli/init.py +161 -0
- codehood/cli/push.py +609 -0
- codehood/cli/show.py +77 -0
- codehood/cli/uitils.py +26 -0
- codehood/functional.py +40 -0
- codehood/globs.py +71 -0
- codehood/grades.py +74 -0
- codehood/hypothesis.py +274 -0
- codehood/models/__init__.py +0 -0
- codehood/models/base.py +15 -0
- codehood/models/repo.py +187 -0
- codehood/models/validators.py +177 -0
- codehood/push/__init__.py +225 -0
- codehood/push/base.py +216 -0
- codehood/push/calendar.py +664 -0
- codehood/push/errors.py +136 -0
- codehood/push/exam.py +636 -0
- codehood/push/exclude.py +60 -0
- codehood/push/grades.py +455 -0
- codehood/push/hook.py +48 -0
- codehood/push/links.py +614 -0
- codehood/push/ours.py +78 -0
- codehood/push/plan.py +1981 -0
- codehood/push/project.py +272 -0
- codehood/push/question.py +338 -0
- codehood/push/report.py +244 -0
- codehood/push/repository.py +326 -0
- codehood/push/resource.py +576 -0
- codehood/push/run.py +1275 -0
- codehood/push/unconsumed.py +208 -0
- codehood/push/wire.py +222 -0
- codehood/py.typed +0 -0
- codehood/repo/__init__.py +62 -0
- codehood/repo/_config.py +28 -0
- codehood/repo/_layout.py +132 -0
- codehood/repo/_scaffold.py +244 -0
- codehood/repo/diff.py +71 -0
- codehood/repo/file_tracker.py +124 -0
- codehood/theme.py +101 -0
- codehood/widgets/__init__.py +0 -0
- codehood/widgets/mascot.py +76 -0
- codehood/widgets/questions/__init__.py +65 -0
- codehood/widgets/questions/base.py +119 -0
- codehood/widgets/questions/controls.py +194 -0
- codehood/widgets/questions/fill_in.py +81 -0
- codehood/widgets/questions/responses.py +137 -0
- codehood/widgets/questions/simple.py +117 -0
- codehood_cli-0.5.0.dist-info/METADATA +294 -0
- codehood_cli-0.5.0.dist-info/RECORD +71 -0
- codehood_cli-0.5.0.dist-info/WHEEL +4 -0
- codehood_cli-0.5.0.dist-info/entry_points.txt +4 -0
- codehood_cli-0.5.0.dist-info/licenses/LICENSE +21 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"""
|
|
2
|
+
`QuestionWidget`: the shared shape every question-type widget in this
|
|
3
|
+
package follows.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
from collections.abc import Callable, Mapping
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
import mdq
|
|
12
|
+
from textual.app import ComposeResult
|
|
13
|
+
from textual.containers import Vertical
|
|
14
|
+
from textual.widget import Widget
|
|
15
|
+
from textual.widgets import Button, Markdown
|
|
16
|
+
|
|
17
|
+
from .responses import Response
|
|
18
|
+
|
|
19
|
+
__all__ = ["QuestionDocument", "QuestionWidget", "question_document"]
|
|
20
|
+
|
|
21
|
+
#: A question as plain data, built by `question_document` -- this package
|
|
22
|
+
#: never parses MDQ source itself.
|
|
23
|
+
QuestionDocument = Mapping[str, Any]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def question_document(question: mdq.Question) -> QuestionDocument:
|
|
27
|
+
"""
|
|
28
|
+
Return `question` as the mapping a widget reads, with an id on every
|
|
29
|
+
choice.
|
|
30
|
+
"""
|
|
31
|
+
return question.with_ids().model_dump(mode="json", exclude_none=True)
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class QuestionWidget(Widget):
|
|
35
|
+
"""
|
|
36
|
+
One interactive MDQ question.
|
|
37
|
+
|
|
38
|
+
Renders `preamble`/`stem`/`epilogue` as Markdown around whatever
|
|
39
|
+
`compose_body` contributes, and turns a submit press into a call to
|
|
40
|
+
`on_response` with the `Response` `build_response` produces.
|
|
41
|
+
`on_response` is a plain callback, not a Textual message -- see
|
|
42
|
+
`dev/specs/to-do/question-widgets.md`.
|
|
43
|
+
|
|
44
|
+
Never reads a choice's `score`/`answer`, a short-answer's `oneOf`, an
|
|
45
|
+
essay's `answerKey`, or any other answer-key field, even though
|
|
46
|
+
`self.document` carries them. Grading is the `on_response` callback's
|
|
47
|
+
business entirely.
|
|
48
|
+
"""
|
|
49
|
+
|
|
50
|
+
DEFAULT_CSS = """
|
|
51
|
+
QuestionWidget {
|
|
52
|
+
height: auto;
|
|
53
|
+
}
|
|
54
|
+
QuestionWidget .stem {
|
|
55
|
+
margin: 1 0;
|
|
56
|
+
}
|
|
57
|
+
QuestionWidget #submit {
|
|
58
|
+
margin-top: 1;
|
|
59
|
+
}
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
def __init__(
|
|
63
|
+
self,
|
|
64
|
+
document: QuestionDocument,
|
|
65
|
+
*,
|
|
66
|
+
on_response: Callable[[Response], None] | None = None,
|
|
67
|
+
name: str | None = None,
|
|
68
|
+
id: str | None = None,
|
|
69
|
+
classes: str | None = None,
|
|
70
|
+
) -> None:
|
|
71
|
+
super().__init__(name=name, id=id, classes=classes)
|
|
72
|
+
self.document = document
|
|
73
|
+
self.on_response = on_response
|
|
74
|
+
|
|
75
|
+
def compose(self) -> ComposeResult:
|
|
76
|
+
with Vertical():
|
|
77
|
+
if preamble := self.document.get("preamble"):
|
|
78
|
+
yield Markdown(preamble, classes="preamble")
|
|
79
|
+
yield Markdown(self.render_stem(), classes="stem")
|
|
80
|
+
yield from self.compose_body()
|
|
81
|
+
if epilogue := self.document.get("epilogue"):
|
|
82
|
+
yield Markdown(epilogue, classes="epilogue")
|
|
83
|
+
yield Button("Submit", id="submit", variant="primary")
|
|
84
|
+
|
|
85
|
+
@property
|
|
86
|
+
def shuffle(self) -> bool:
|
|
87
|
+
"""
|
|
88
|
+
Whether the choices may be shuffled.
|
|
89
|
+
|
|
90
|
+
`shuffle` is `true`, `false` or `"inherit"` (the default), which
|
|
91
|
+
resolves to `false` outside an exam, where this widget always runs.
|
|
92
|
+
"""
|
|
93
|
+
|
|
94
|
+
return self.document.get("shuffle") is True
|
|
95
|
+
|
|
96
|
+
def render_stem(self) -> str:
|
|
97
|
+
"""The stem's Markdown source. Overridden by `FillInQuestion`."""
|
|
98
|
+
|
|
99
|
+
return self.document["stem"]
|
|
100
|
+
|
|
101
|
+
def compose_body(self) -> ComposeResult:
|
|
102
|
+
"""Yield the control(s) the student answers through."""
|
|
103
|
+
raise NotImplementedError
|
|
104
|
+
yield # pragma: no cover - makes this a generator function
|
|
105
|
+
|
|
106
|
+
def build_response(self) -> Response:
|
|
107
|
+
"""Read the composed control(s) and produce this question's `Response`."""
|
|
108
|
+
raise NotImplementedError
|
|
109
|
+
|
|
110
|
+
def on_button_pressed(self, event: Button.Pressed) -> None:
|
|
111
|
+
if event.button.id == "submit":
|
|
112
|
+
self.submit()
|
|
113
|
+
|
|
114
|
+
def submit(self) -> None:
|
|
115
|
+
"""Build a `Response` and hand it to `on_response`, if one was given."""
|
|
116
|
+
|
|
117
|
+
response = self.build_response()
|
|
118
|
+
if self.on_response is not None:
|
|
119
|
+
self.on_response(response)
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reusable input controls shared across question widgets.
|
|
3
|
+
|
|
4
|
+
Each control renders one answer-input shape and exposes what the student
|
|
5
|
+
entered as `.result` -- nothing here knows what a "correct" answer looks
|
|
6
|
+
like, only what shape a response takes. `base.QuestionWidget` composes one
|
|
7
|
+
control per question; `fill_in.py` composes one per blank.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import random
|
|
13
|
+
from collections.abc import Mapping, Sequence
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from textual.app import ComposeResult
|
|
17
|
+
from textual.containers import Horizontal, Vertical
|
|
18
|
+
from textual.widget import Widget
|
|
19
|
+
from textual.widgets import Input, Label, RadioButton, RadioSet, SelectionList, Static
|
|
20
|
+
from textual.widgets.selection_list import Selection
|
|
21
|
+
|
|
22
|
+
from .responses import parse_numeric_text
|
|
23
|
+
|
|
24
|
+
__all__ = [
|
|
25
|
+
"ChoiceControl",
|
|
26
|
+
"Control",
|
|
27
|
+
"MultiChoiceControl",
|
|
28
|
+
"NumericControl",
|
|
29
|
+
"ShortAnswerControl",
|
|
30
|
+
"TrueFalseControl",
|
|
31
|
+
]
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def _ordered(
|
|
35
|
+
items: Sequence[Mapping[str, Any]], *, shuffle: bool
|
|
36
|
+
) -> list[Mapping[str, Any]]:
|
|
37
|
+
"""Return `items` in the order a control should render them."""
|
|
38
|
+
|
|
39
|
+
return random.sample(list(items), len(items)) if shuffle else list(items)
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class Control(Widget):
|
|
43
|
+
"""Base for a control that exposes what the student entered as `.result`."""
|
|
44
|
+
|
|
45
|
+
DEFAULT_CSS = """
|
|
46
|
+
Control {
|
|
47
|
+
height: auto;
|
|
48
|
+
}
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
@property
|
|
52
|
+
def result(self) -> Any:
|
|
53
|
+
"""What the student entered, in whatever shape this control produces."""
|
|
54
|
+
raise NotImplementedError
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class ChoiceControl(Control):
|
|
58
|
+
"""
|
|
59
|
+
Exactly one choice, out of `choices`, picked via a `RadioSet`.
|
|
60
|
+
|
|
61
|
+
Backs `multiple-choice` questions and `fill-in`'s choice blanks.
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
def __init__(
|
|
65
|
+
self,
|
|
66
|
+
choices: Sequence[Mapping[str, Any]],
|
|
67
|
+
*,
|
|
68
|
+
shuffle: bool = False,
|
|
69
|
+
id: str | None = None,
|
|
70
|
+
) -> None:
|
|
71
|
+
super().__init__(id=id)
|
|
72
|
+
self._choices = _ordered(choices, shuffle=shuffle)
|
|
73
|
+
|
|
74
|
+
def compose(self) -> ComposeResult:
|
|
75
|
+
with RadioSet():
|
|
76
|
+
for choice in self._choices:
|
|
77
|
+
yield RadioButton(choice["text"])
|
|
78
|
+
|
|
79
|
+
@property
|
|
80
|
+
def result(self) -> str | None:
|
|
81
|
+
"""The picked choice's `id`, or `None` if nothing was picked."""
|
|
82
|
+
|
|
83
|
+
index = self.query_one(RadioSet).pressed_index
|
|
84
|
+
return self._choices[index]["id"] if index >= 0 else None
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class MultiChoiceControl(Control):
|
|
88
|
+
"""Any number of `choices`, picked via a `SelectionList`."""
|
|
89
|
+
|
|
90
|
+
def __init__(
|
|
91
|
+
self,
|
|
92
|
+
choices: Sequence[Mapping[str, Any]],
|
|
93
|
+
*,
|
|
94
|
+
shuffle: bool = False,
|
|
95
|
+
id: str | None = None,
|
|
96
|
+
) -> None:
|
|
97
|
+
super().__init__(id=id)
|
|
98
|
+
self._choices = _ordered(choices, shuffle=shuffle)
|
|
99
|
+
|
|
100
|
+
def compose(self) -> ComposeResult:
|
|
101
|
+
yield SelectionList(
|
|
102
|
+
*(Selection(choice["text"], choice["id"]) for choice in self._choices)
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def result(self) -> frozenset[str]:
|
|
107
|
+
return frozenset(self.query_one(SelectionList).selected)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class TrueFalseControl(Control):
|
|
111
|
+
"""
|
|
112
|
+
One statement per row, each judged independently via its own two-option
|
|
113
|
+
`RadioSet`. A row never clicked stays out of `.result` -- an abstention,
|
|
114
|
+
not a `False`.
|
|
115
|
+
"""
|
|
116
|
+
|
|
117
|
+
DEFAULT_CSS = """
|
|
118
|
+
TrueFalseControl .statement {
|
|
119
|
+
height: auto;
|
|
120
|
+
}
|
|
121
|
+
"""
|
|
122
|
+
|
|
123
|
+
def __init__(
|
|
124
|
+
self,
|
|
125
|
+
statements: Sequence[Mapping[str, Any]],
|
|
126
|
+
*,
|
|
127
|
+
shuffle: bool = False,
|
|
128
|
+
id: str | None = None,
|
|
129
|
+
) -> None:
|
|
130
|
+
super().__init__(id=id)
|
|
131
|
+
self._statements = _ordered(statements, shuffle=shuffle)
|
|
132
|
+
|
|
133
|
+
def compose(self) -> ComposeResult:
|
|
134
|
+
for statement in self._statements:
|
|
135
|
+
with Vertical(classes="statement"):
|
|
136
|
+
yield Static(statement["text"])
|
|
137
|
+
with RadioSet(id=self._set_id(statement["id"])):
|
|
138
|
+
yield RadioButton("True")
|
|
139
|
+
yield RadioButton("False")
|
|
140
|
+
|
|
141
|
+
@staticmethod
|
|
142
|
+
def _set_id(statement_id: str) -> str:
|
|
143
|
+
return f"statement-{statement_id}"
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def result(self) -> dict[str, bool]:
|
|
147
|
+
answers: dict[str, bool] = {}
|
|
148
|
+
for statement in self._statements:
|
|
149
|
+
index = self.query_one(
|
|
150
|
+
f"#{self._set_id(statement['id'])}", RadioSet
|
|
151
|
+
).pressed_index
|
|
152
|
+
if index == 0:
|
|
153
|
+
answers[statement["id"]] = True
|
|
154
|
+
elif index == 1:
|
|
155
|
+
answers[statement["id"]] = False
|
|
156
|
+
return answers
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
class NumericControl(Control):
|
|
160
|
+
"""A single numeric entry, parsed on read rather than as-you-type."""
|
|
161
|
+
|
|
162
|
+
DEFAULT_CSS = """
|
|
163
|
+
NumericControl Horizontal {
|
|
164
|
+
height: auto;
|
|
165
|
+
}
|
|
166
|
+
"""
|
|
167
|
+
|
|
168
|
+
def __init__(self, *, unit: str | None = None, id: str | None = None) -> None:
|
|
169
|
+
super().__init__(id=id)
|
|
170
|
+
self._unit = unit
|
|
171
|
+
|
|
172
|
+
def compose(self) -> ComposeResult:
|
|
173
|
+
with Horizontal():
|
|
174
|
+
yield Input(placeholder="answer", id="value")
|
|
175
|
+
if self._unit:
|
|
176
|
+
yield Label(self._unit)
|
|
177
|
+
|
|
178
|
+
@property
|
|
179
|
+
def result(self) -> tuple[str, float | None]:
|
|
180
|
+
"""`(text, value)` -- `value` is `None` for a malformed response."""
|
|
181
|
+
|
|
182
|
+
text = self.query_one("#value", Input).value
|
|
183
|
+
return text, parse_numeric_text(text)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
class ShortAnswerControl(Control):
|
|
187
|
+
"""A single line of free text."""
|
|
188
|
+
|
|
189
|
+
def compose(self) -> ComposeResult:
|
|
190
|
+
yield Input(placeholder="answer", id="value")
|
|
191
|
+
|
|
192
|
+
@property
|
|
193
|
+
def result(self) -> str:
|
|
194
|
+
return self.query_one("#value", Input).value
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"""
|
|
2
|
+
`fill-in`: a stem with named blanks, each graded like a smaller
|
|
3
|
+
multiple-choice, short-answer, or numeric question.
|
|
4
|
+
|
|
5
|
+
Blanks render below the stem rather than inside it -- see
|
|
6
|
+
`docs/adr/0001-fill-in-blanks-render-below-the-stem.md`.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import re
|
|
12
|
+
from collections.abc import Mapping
|
|
13
|
+
from typing import Any
|
|
14
|
+
|
|
15
|
+
from textual.app import ComposeResult
|
|
16
|
+
from textual.containers import Vertical
|
|
17
|
+
from textual.widgets import Label
|
|
18
|
+
|
|
19
|
+
from .base import QuestionWidget
|
|
20
|
+
from .controls import ChoiceControl, Control, NumericControl, ShortAnswerControl
|
|
21
|
+
from .responses import (
|
|
22
|
+
BlankResponse,
|
|
23
|
+
FillInResponse,
|
|
24
|
+
MultipleChoiceResponse,
|
|
25
|
+
NumericResponse,
|
|
26
|
+
ShortAnswerResponse,
|
|
27
|
+
)
|
|
28
|
+
|
|
29
|
+
__all__ = ["FillInQuestion"]
|
|
30
|
+
|
|
31
|
+
#: Matches an inline blank reference in a fill-in stem, e.g. `[^capital]`.
|
|
32
|
+
_BLANK_REF_RE = re.compile(r"\[\^([a-zA-Z0-9](?:[-_][a-zA-Z0-9]+)*)\]")
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class FillInQuestion(QuestionWidget):
|
|
36
|
+
DEFAULT_CSS = """
|
|
37
|
+
FillInQuestion #control {
|
|
38
|
+
height: auto;
|
|
39
|
+
}
|
|
40
|
+
"""
|
|
41
|
+
|
|
42
|
+
def render_stem(self) -> str:
|
|
43
|
+
return _BLANK_REF_RE.sub(
|
|
44
|
+
lambda m: f"**[[{m.group(1)}]]**", self.document["stem"]
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
def compose_body(self) -> ComposeResult:
|
|
48
|
+
shuffle = self.shuffle
|
|
49
|
+
with Vertical(id="control"):
|
|
50
|
+
for blank in self.document["blanks"]:
|
|
51
|
+
yield Label(f"[[{blank['id']}]]")
|
|
52
|
+
yield _blank_control(blank, shuffle=shuffle)
|
|
53
|
+
|
|
54
|
+
def build_response(self) -> FillInResponse:
|
|
55
|
+
blanks: dict[str, BlankResponse] = {}
|
|
56
|
+
for blank in self.document["blanks"]:
|
|
57
|
+
control = self.query_one(f"#{_control_id(blank['id'])}", Control)
|
|
58
|
+
blanks[blank["id"]] = _blank_response(blank, control)
|
|
59
|
+
return FillInResponse(blanks)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def _control_id(blank_id: str) -> str:
|
|
63
|
+
return f"blank-{blank_id}"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _blank_control(blank: Mapping[str, Any], *, shuffle: bool) -> Control:
|
|
67
|
+
control_id = _control_id(blank["id"])
|
|
68
|
+
if blank["type"] == "multiple-choice":
|
|
69
|
+
return ChoiceControl(blank["choices"], shuffle=shuffle, id=control_id)
|
|
70
|
+
if blank["type"] == "numeric":
|
|
71
|
+
return NumericControl(unit=blank.get("unit"), id=control_id)
|
|
72
|
+
return ShortAnswerControl(id=control_id)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def _blank_response(blank: Mapping[str, Any], control: Control) -> BlankResponse:
|
|
76
|
+
if blank["type"] == "multiple-choice":
|
|
77
|
+
return MultipleChoiceResponse(control.result)
|
|
78
|
+
if blank["type"] == "numeric":
|
|
79
|
+
text, value = control.result
|
|
80
|
+
return NumericResponse(text, value)
|
|
81
|
+
return ShortAnswerResponse(control.result)
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Typed responses produced by the question widgets in this package.
|
|
3
|
+
|
|
4
|
+
A `Response` captures only what a student marked -- never a score, a
|
|
5
|
+
correctness judgement, or anything read from a question's answer key. See
|
|
6
|
+
`dev/specs/to-do/question-widgets.md` ("The widget never reads a question's
|
|
7
|
+
answer key"): grading is entirely the `on_response` callback's job.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import re
|
|
13
|
+
from dataclasses import dataclass
|
|
14
|
+
|
|
15
|
+
__all__ = [
|
|
16
|
+
"BlankResponse",
|
|
17
|
+
"EssayResponse",
|
|
18
|
+
"FillInResponse",
|
|
19
|
+
"MultipleChoiceResponse",
|
|
20
|
+
"MultipleSelectionResponse",
|
|
21
|
+
"NumericResponse",
|
|
22
|
+
"Response",
|
|
23
|
+
"ShortAnswerResponse",
|
|
24
|
+
"TrueFalseResponse",
|
|
25
|
+
"parse_numeric_text",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
@dataclass(frozen=True, slots=True)
|
|
30
|
+
class MultipleChoiceResponse:
|
|
31
|
+
"""The single choice a student picked, or `None` if they skipped it."""
|
|
32
|
+
|
|
33
|
+
choice_id: str | None
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@dataclass(frozen=True, slots=True)
|
|
37
|
+
class MultipleSelectionResponse:
|
|
38
|
+
"""The choices a student ticked. Empty is a valid response, not a skip."""
|
|
39
|
+
|
|
40
|
+
choice_ids: frozenset[str]
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
@dataclass(frozen=True, slots=True)
|
|
44
|
+
class TrueFalseResponse:
|
|
45
|
+
"""
|
|
46
|
+
One judgement per statement a student actually judged.
|
|
47
|
+
|
|
48
|
+
A statement id missing from `answers` is an abstention
|
|
49
|
+
(`mdq.spec/CONTEXT.md`: "leaving one individual true-false statement
|
|
50
|
+
unjudged"), distinct from a `False` entry.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
answers: dict[str, bool]
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True, slots=True)
|
|
57
|
+
class NumericResponse:
|
|
58
|
+
"""
|
|
59
|
+
`text` is exactly what the student typed; `value` is `None` when `text`
|
|
60
|
+
doesn't parse as a number -- a malformed response, never a wrong one
|
|
61
|
+
(`mdq.spec/CONTEXT.md`).
|
|
62
|
+
"""
|
|
63
|
+
|
|
64
|
+
text: str
|
|
65
|
+
value: float | None
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
@dataclass(frozen=True, slots=True)
|
|
69
|
+
class ShortAnswerResponse:
|
|
70
|
+
text: str
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
@dataclass(frozen=True, slots=True)
|
|
74
|
+
class EssayResponse:
|
|
75
|
+
text: str
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
#: What a single fill-in blank can produce, reusing the response type of
|
|
79
|
+
#: whichever question type the blank is graded like.
|
|
80
|
+
BlankResponse = MultipleChoiceResponse | ShortAnswerResponse | NumericResponse
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
@dataclass(frozen=True, slots=True)
|
|
84
|
+
class FillInResponse:
|
|
85
|
+
"""One typed response per blank id, keyed exactly like `document["blanks"]`."""
|
|
86
|
+
|
|
87
|
+
blanks: dict[str, BlankResponse]
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
Response = (
|
|
91
|
+
MultipleChoiceResponse
|
|
92
|
+
| MultipleSelectionResponse
|
|
93
|
+
| TrueFalseResponse
|
|
94
|
+
| NumericResponse
|
|
95
|
+
| ShortAnswerResponse
|
|
96
|
+
| EssayResponse
|
|
97
|
+
| FillInResponse
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
_FRACTION_RE = re.compile(r"^(?P<sign>[+-])?(?P<num>\d+)/(?P<den>\d+)$")
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def parse_numeric_text(text: str) -> float | None:
|
|
105
|
+
"""
|
|
106
|
+
Parse `text` as the numeric domain mdq accepts: a plain float, or an
|
|
107
|
+
`a/b` fraction. Returns `None` for anything else -- a malformed
|
|
108
|
+
response, not a wrong one.
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
text: Exactly what the student typed.
|
|
112
|
+
|
|
113
|
+
Returns:
|
|
114
|
+
The parsed value, or `None` if `text` doesn't parse.
|
|
115
|
+
|
|
116
|
+
Example:
|
|
117
|
+
>>> parse_numeric_text("3.5")
|
|
118
|
+
3.5
|
|
119
|
+
>>> parse_numeric_text("3/4")
|
|
120
|
+
0.75
|
|
121
|
+
>>> parse_numeric_text("abc") is None
|
|
122
|
+
True
|
|
123
|
+
"""
|
|
124
|
+
|
|
125
|
+
text = text.strip()
|
|
126
|
+
if not text:
|
|
127
|
+
return None
|
|
128
|
+
if m := _FRACTION_RE.match(text):
|
|
129
|
+
den = int(m.group("den"))
|
|
130
|
+
if den == 0:
|
|
131
|
+
return None
|
|
132
|
+
value = int(m.group("num")) / den
|
|
133
|
+
return -value if m.group("sign") == "-" else value
|
|
134
|
+
try:
|
|
135
|
+
return float(text)
|
|
136
|
+
except ValueError:
|
|
137
|
+
return None
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"""
|
|
2
|
+
The six question types whose answer shape is exactly one control:
|
|
3
|
+
`multiple-choice`, `multiple-selection`, `true-false`, `numeric`,
|
|
4
|
+
`short-answer`, `essay`. `fill-in` composes several of the same controls per
|
|
5
|
+
blank -- see `fill_in.py`.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from textual.app import ComposeResult
|
|
11
|
+
from textual.widgets import TextArea
|
|
12
|
+
|
|
13
|
+
from .base import QuestionWidget
|
|
14
|
+
from .controls import (
|
|
15
|
+
ChoiceControl,
|
|
16
|
+
MultiChoiceControl,
|
|
17
|
+
NumericControl,
|
|
18
|
+
ShortAnswerControl,
|
|
19
|
+
TrueFalseControl,
|
|
20
|
+
)
|
|
21
|
+
from .responses import (
|
|
22
|
+
EssayResponse,
|
|
23
|
+
MultipleChoiceResponse,
|
|
24
|
+
MultipleSelectionResponse,
|
|
25
|
+
NumericResponse,
|
|
26
|
+
ShortAnswerResponse,
|
|
27
|
+
TrueFalseResponse,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
__all__ = [
|
|
31
|
+
"EssayQuestion",
|
|
32
|
+
"MultipleChoiceQuestion",
|
|
33
|
+
"MultipleSelectionQuestion",
|
|
34
|
+
"NumericQuestion",
|
|
35
|
+
"ShortAnswerQuestion",
|
|
36
|
+
"TrueFalseQuestion",
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class MultipleChoiceQuestion(QuestionWidget):
|
|
41
|
+
def compose_body(self) -> ComposeResult:
|
|
42
|
+
yield ChoiceControl(
|
|
43
|
+
self.document["choices"],
|
|
44
|
+
shuffle=self.shuffle,
|
|
45
|
+
id="control",
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
def build_response(self) -> MultipleChoiceResponse:
|
|
49
|
+
control = self.query_one("#control", ChoiceControl)
|
|
50
|
+
return MultipleChoiceResponse(control.result)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class MultipleSelectionQuestion(QuestionWidget):
|
|
54
|
+
def compose_body(self) -> ComposeResult:
|
|
55
|
+
yield MultiChoiceControl(
|
|
56
|
+
self.document["choices"],
|
|
57
|
+
shuffle=self.shuffle,
|
|
58
|
+
id="control",
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
def build_response(self) -> MultipleSelectionResponse:
|
|
62
|
+
control = self.query_one("#control", MultiChoiceControl)
|
|
63
|
+
return MultipleSelectionResponse(control.result)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class TrueFalseQuestion(QuestionWidget):
|
|
67
|
+
def compose_body(self) -> ComposeResult:
|
|
68
|
+
yield TrueFalseControl(
|
|
69
|
+
self.document["choices"],
|
|
70
|
+
shuffle=self.shuffle,
|
|
71
|
+
id="control",
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
def build_response(self) -> TrueFalseResponse:
|
|
75
|
+
control = self.query_one("#control", TrueFalseControl)
|
|
76
|
+
return TrueFalseResponse(control.result)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
class NumericQuestion(QuestionWidget):
|
|
80
|
+
def compose_body(self) -> ComposeResult:
|
|
81
|
+
yield NumericControl(unit=self.document.get("unit"), id="control")
|
|
82
|
+
|
|
83
|
+
def build_response(self) -> NumericResponse:
|
|
84
|
+
control = self.query_one("#control", NumericControl)
|
|
85
|
+
text, value = control.result
|
|
86
|
+
return NumericResponse(text, value)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class ShortAnswerQuestion(QuestionWidget):
|
|
90
|
+
def compose_body(self) -> ComposeResult:
|
|
91
|
+
yield ShortAnswerControl(id="control")
|
|
92
|
+
|
|
93
|
+
def build_response(self) -> ShortAnswerResponse:
|
|
94
|
+
control = self.query_one("#control", ShortAnswerControl)
|
|
95
|
+
return ShortAnswerResponse(control.result)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
class EssayQuestion(QuestionWidget):
|
|
99
|
+
"""
|
|
100
|
+
`input: code` gets a syntax-highlighted `TextArea` when `highlight`
|
|
101
|
+
names a language Textual ships with; anything else -- `text`, `plain`,
|
|
102
|
+
or an unrecognized `highlight` -- gets a plain one.
|
|
103
|
+
"""
|
|
104
|
+
|
|
105
|
+
def compose_body(self) -> ComposeResult:
|
|
106
|
+
text_area = TextArea(id="control")
|
|
107
|
+
language = self.document.get("highlight")
|
|
108
|
+
if (
|
|
109
|
+
self.document.get("input") == "code"
|
|
110
|
+
and language in text_area.available_languages
|
|
111
|
+
):
|
|
112
|
+
text_area.language = language
|
|
113
|
+
yield text_area
|
|
114
|
+
|
|
115
|
+
def build_response(self) -> EssayResponse:
|
|
116
|
+
control = self.query_one("#control", TextArea)
|
|
117
|
+
return EssayResponse(control.text)
|