chalk-classroom 0.1.0__tar.gz

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.
Files changed (86) hide show
  1. chalk_classroom-0.1.0/LICENSE +21 -0
  2. chalk_classroom-0.1.0/PKG-INFO +170 -0
  3. chalk_classroom-0.1.0/README.md +137 -0
  4. chalk_classroom-0.1.0/chalk/__init__.py +2 -0
  5. chalk_classroom-0.1.0/chalk/__main__.py +4 -0
  6. chalk_classroom-0.1.0/chalk/assets.py +40 -0
  7. chalk_classroom-0.1.0/chalk/autograder/__init__.py +0 -0
  8. chalk_classroom-0.1.0/chalk/autograder/regex_answer.py +167 -0
  9. chalk_classroom-0.1.0/chalk/autograder/regex_grader.py +30 -0
  10. chalk_classroom-0.1.0/chalk/cli/__init__.py +65 -0
  11. chalk_classroom-0.1.0/chalk/cli/autograde_cmd.py +433 -0
  12. chalk_classroom-0.1.0/chalk/cli/clear_app.py +43 -0
  13. chalk_classroom-0.1.0/chalk/cli/codehood_app.py +110 -0
  14. chalk_classroom-0.1.0/chalk/cli/contrib_app.py +85 -0
  15. chalk_classroom-0.1.0/chalk/cli/grade_cmd.py +197 -0
  16. chalk_classroom-0.1.0/chalk/cli/info_app.py +48 -0
  17. chalk_classroom-0.1.0/chalk/cli/init_app.py +64 -0
  18. chalk_classroom-0.1.0/chalk/cli/quiz_app.py +112 -0
  19. chalk_classroom-0.1.0/chalk/cli/response_cmds.py +160 -0
  20. chalk_classroom-0.1.0/chalk/cli/roster_app.py +315 -0
  21. chalk_classroom-0.1.0/chalk/cli/show_cmd.py +356 -0
  22. chalk_classroom-0.1.0/chalk/cli/util.py +106 -0
  23. chalk_classroom-0.1.0/chalk/codehood.py +169 -0
  24. chalk_classroom-0.1.0/chalk/codehood_api.py +103 -0
  25. chalk_classroom-0.1.0/chalk/codehood_autograde.py +814 -0
  26. chalk_classroom-0.1.0/chalk/contrib/__init__.py +0 -0
  27. chalk_classroom-0.1.0/chalk/contrib/data/__init__.py +0 -0
  28. chalk_classroom-0.1.0/chalk/contrib/data/convert.py +106 -0
  29. chalk_classroom-0.1.0/chalk/contrib/github/__init__.py +0 -0
  30. chalk_classroom-0.1.0/chalk/contrib/github/classroom.py +236 -0
  31. chalk_classroom-0.1.0/chalk/contrib/github/repo_submissions.py +107 -0
  32. chalk_classroom-0.1.0/chalk/contrib/github/roster.py +150 -0
  33. chalk_classroom-0.1.0/chalk/contrib/grades/__init__.py +0 -0
  34. chalk_classroom-0.1.0/chalk/contrib/grades/rescale.py +56 -0
  35. chalk_classroom-0.1.0/chalk/contrib/parser/__init__.py +0 -0
  36. chalk_classroom-0.1.0/chalk/contrib/parser/elm_test.py +78 -0
  37. chalk_classroom-0.1.0/chalk/core/__init__.py +66 -0
  38. chalk_classroom-0.1.0/chalk/core/adjustment.py +421 -0
  39. chalk_classroom-0.1.0/chalk/core/answer.py +160 -0
  40. chalk_classroom-0.1.0/chalk/core/classroom.py +600 -0
  41. chalk_classroom-0.1.0/chalk/core/decimal.py +107 -0
  42. chalk_classroom-0.1.0/chalk/core/disqualification.py +228 -0
  43. chalk_classroom-0.1.0/chalk/core/error.py +152 -0
  44. chalk_classroom-0.1.0/chalk/core/grader.py +140 -0
  45. chalk_classroom-0.1.0/chalk/core/grades.py +817 -0
  46. chalk_classroom-0.1.0/chalk/core/issue.py +303 -0
  47. chalk_classroom-0.1.0/chalk/core/models.py +271 -0
  48. chalk_classroom-0.1.0/chalk/core/quiz.py +487 -0
  49. chalk_classroom-0.1.0/chalk/core/roster.py +355 -0
  50. chalk_classroom-0.1.0/chalk/core/score.py +34 -0
  51. chalk_classroom-0.1.0/chalk/core/student.py +162 -0
  52. chalk_classroom-0.1.0/chalk/core/submissions.py +893 -0
  53. chalk_classroom-0.1.0/chalk/data/__init__.py +66 -0
  54. chalk_classroom-0.1.0/chalk/data/core.py +300 -0
  55. chalk_classroom-0.1.0/chalk/data/disqualifiers.py +144 -0
  56. chalk_classroom-0.1.0/chalk/data/json.py +57 -0
  57. chalk_classroom-0.1.0/chalk/data/protocols.py +32 -0
  58. chalk_classroom-0.1.0/chalk/data/serializers.py +169 -0
  59. chalk_classroom-0.1.0/chalk/data/validators.py +361 -0
  60. chalk_classroom-0.1.0/chalk/data/yaml.py +112 -0
  61. chalk_classroom-0.1.0/chalk/env.py +91 -0
  62. chalk_classroom-0.1.0/chalk/info.py +81 -0
  63. chalk_classroom-0.1.0/chalk/numeric.py +97 -0
  64. chalk_classroom-0.1.0/chalk/protocols.py +60 -0
  65. chalk_classroom-0.1.0/chalk/py.typed +0 -0
  66. chalk_classroom-0.1.0/chalk/question/__init__.py +119 -0
  67. chalk_classroom-0.1.0/chalk/question/base.py +712 -0
  68. chalk_classroom-0.1.0/chalk/question/mdq.py +391 -0
  69. chalk_classroom-0.1.0/chalk/question/renderable.py +111 -0
  70. chalk_classroom-0.1.0/chalk/question/repo.py +121 -0
  71. chalk_classroom-0.1.0/chalk/question/textual.py +138 -0
  72. chalk_classroom-0.1.0/chalk/question/util.py +39 -0
  73. chalk_classroom-0.1.0/chalk/table.py +505 -0
  74. chalk_classroom-0.1.0/chalk/testing/strategies.py +77 -0
  75. chalk_classroom-0.1.0/chalk/types.py +7 -0
  76. chalk_classroom-0.1.0/chalk/util/__init__.py +130 -0
  77. chalk_classroom-0.1.0/chalk/util/dataframe.py +49 -0
  78. chalk_classroom-0.1.0/chalk/util/external.py +13 -0
  79. chalk_classroom-0.1.0/chalk/util/functional.py +112 -0
  80. chalk_classroom-0.1.0/chalk/util/parsing.py +112 -0
  81. chalk_classroom-0.1.0/chalk/util/path.py +310 -0
  82. chalk_classroom-0.1.0/chalk/util/sequence.py +93 -0
  83. chalk_classroom-0.1.0/chalk/util/show.py +160 -0
  84. chalk_classroom-0.1.0/chalk/util/string.py +27 -0
  85. chalk_classroom-0.1.0/pyproject.toml +140 -0
  86. chalk_classroom-0.1.0/pyproject.toml.orig +108 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Fábio Macêdo Mendes
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,170 @@
1
+ Metadata-Version: 2.4
2
+ Name: chalk-classroom
3
+ Version: 0.1.0
4
+ Summary: Command line tool to grade quizzes and keep a roster and grades in plain text files.
5
+ Author: Fábio Macêdo Mendes
6
+ Author-email: Fábio Macêdo Mendes <fabiomacedomendes@gmail.com>
7
+ License-Expression: MIT
8
+ License-File: LICENSE
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Environment :: Console
11
+ Classifier: Intended Audience :: Education
12
+ Classifier: Operating System :: POSIX :: Linux
13
+ Classifier: Programming Language :: Python :: 3.13
14
+ Classifier: Topic :: Education :: Testing
15
+ Requires-Dist: chalk-grader>=0.3.0
16
+ Requires-Dist: errorz>=0.2.1
17
+ Requires-Dist: frozendict>=2.4.7
18
+ Requires-Dist: gitignore-lib>=0.1.1
19
+ Requires-Dist: mdq>=0.2.0
20
+ Requires-Dist: natsort>=8.4.0
21
+ Requires-Dist: optionz>=0.2.0
22
+ Requires-Dist: pandas>=3.0.3
23
+ Requires-Dist: pydantic>=2.13.4
24
+ Requires-Dist: pygments>=2.20.0
25
+ Requires-Dist: pyyaml>=6.0.3
26
+ Requires-Dist: rich>=15.0.0
27
+ Requires-Dist: typer
28
+ Requires-Python: >=3.13
29
+ Project-URL: Homepage, https://github.com/fabiommendes/chalk
30
+ Project-URL: Source, https://github.com/fabiommendes/chalk
31
+ Project-URL: Issues, https://github.com/fabiommendes/chalk/issues
32
+ Description-Content-Type: text/markdown
33
+
34
+ Chalk classroom manager
35
+ =======================
36
+
37
+ Chalk is a command line tool to grade quizzes and keep track of students and
38
+ grades. Everything lives in plain text files in a directory: a roster, one
39
+ directory per quiz, the submissions and the grades. Chalk grades what was
40
+ answered outside of it (a paper exam, a Moodle or Google Forms export, GitHub
41
+ Classroom repositories), either automatically or by hand.
42
+
43
+ Chalk can share a repository with a codehood course:
44
+ both read the same `roster.csv`, and `chalk codehood grades` writes the quiz
45
+ grades to the `grades.csv` that codehood publishes. When the classroom sits in
46
+ a directory with a `codehood.toml`, `chalk init` and `chalk quiz create` add
47
+ their paths (`chalk.toml`, each quiz directory) to the course's
48
+ `.codehoodignore`, so `codehood push` skips them.
49
+
50
+
51
+ Installation
52
+ ------------
53
+
54
+ ```bash
55
+ uvx chalk # or pipx run chalk
56
+ ```
57
+
58
+ Chalk needs Python 3.13 or newer.
59
+
60
+
61
+ Getting started
62
+ ---------------
63
+
64
+ ```bash
65
+ chalk init # creates chalk.toml and roster.csv
66
+ chalk quiz create p1 --questions 3 # creates p1/conf.yaml
67
+ # write the questions in p1/questions/ and the answers in p1/submissions.csv
68
+ chalk autograde p1/q1 # grades q1 with its answer key
69
+ chalk grade p1/q3 # grades q3 by hand
70
+ chalk show p1 --grades # shows the grades table
71
+ ```
72
+
73
+
74
+ Directory structure
75
+ -------------------
76
+
77
+ ```
78
+ classroom/
79
+ ├── chalk.toml # classroom configuration
80
+ ├── roster.csv # students
81
+ ├── conf.py # optional: final_grade() and other hooks
82
+ └── p1/ # a quiz: any directory with a conf.yaml
83
+ ├── conf.yaml # name, dates, weight and chalk settings of questions
84
+ ├── questions/ # question documents in the mdq format
85
+ │ ├── q1.md
86
+ │ └── q2.yaml
87
+ ├── q3/ # files of question q3: autograder script, template/
88
+ ├── submissions.csv # one row per student, one column per question
89
+ └── grades.csv # written by chalk
90
+ ```
91
+
92
+
93
+ Questions
94
+ ---------
95
+
96
+ Objective questions are [mdq](https://pypi.org/project/mdq/) documents in
97
+ `<quiz>/questions/`, named after the question id. The document may be
98
+ Markdown (`.md`, `.mdq.md`) or the parsed form of mdq in YAML or JSON. It
99
+ holds the stem, the choices and the answer key, and chalk grades responses
100
+ with mdq.
101
+
102
+ ```markdown
103
+ ---
104
+ id: q1
105
+ title: Capital
106
+ ---
107
+
108
+ What is the capital of Brazil?
109
+
110
+ - [*] [df] Brasília
111
+ - [ ] [sp] São Paulo
112
+ ```
113
+
114
+ `conf.yaml` lists the questions in order and keeps what is specific to chalk:
115
+
116
+ ```yaml
117
+ name: Exam 1
118
+ weight: 1
119
+ questions:
120
+ - id: q1 # mdq document in questions/q1.md
121
+ - id: q2
122
+ value: 2 # weight of the question in the quiz
123
+ - id: q3 # no document: an open question, graded by hand
124
+ - id: q4
125
+ type: text # free text, graded by an autograder script or a regex
126
+ format: python
127
+ - id: q5
128
+ type: repo # the answer is a directory, graded by a script
129
+ - id: q6
130
+ source: ../questions/recursion/01.md # mdq document from elsewhere
131
+ ```
132
+
133
+ `source` points to an mdq file outside `<quiz>/questions/`, relative to the
134
+ quiz directory. Use it to reuse questions from the `questions/` bank of a
135
+ codehood course without copying them. The quiz refers to the question by the
136
+ id of its entry, whatever id the file declares.
137
+
138
+ Answers in `submissions.csv` follow the mdq responses: the id of a choice (or
139
+ its text) for multiple choice, a list of ids such as `a, c` or `[a, c]` for
140
+ multiple selection, a number for numeric questions and text for the others.
141
+
142
+ ```csv
143
+ id,q1,q2,q3
144
+ 1234,df,"a, c",An open answer
145
+ ```
146
+
147
+
148
+ Roster
149
+ ------
150
+
151
+ `roster.csv` uses the columns that codehood also uses. Every column is
152
+ optional, and other columns are kept.
153
+
154
+ ```csv
155
+ school_id,username,name,email,github,status
156
+ 1234,jdoe,John Doe,john.doe@example.com,jdoe,active
157
+ ```
158
+
159
+ The student id is `school_id`. Set `student_id = "username"` (or another
160
+ column) in the `[chalk]` section of `chalk.toml` to use another column.
161
+ Rosters with an `id` column instead of `school_id` also work.
162
+
163
+
164
+ Documentation
165
+ -------------
166
+
167
+ ```bash
168
+ uv sync --group dev
169
+ uv run mkdocs serve
170
+ ```
@@ -0,0 +1,137 @@
1
+ Chalk classroom manager
2
+ =======================
3
+
4
+ Chalk is a command line tool to grade quizzes and keep track of students and
5
+ grades. Everything lives in plain text files in a directory: a roster, one
6
+ directory per quiz, the submissions and the grades. Chalk grades what was
7
+ answered outside of it (a paper exam, a Moodle or Google Forms export, GitHub
8
+ Classroom repositories), either automatically or by hand.
9
+
10
+ Chalk can share a repository with a codehood course:
11
+ both read the same `roster.csv`, and `chalk codehood grades` writes the quiz
12
+ grades to the `grades.csv` that codehood publishes. When the classroom sits in
13
+ a directory with a `codehood.toml`, `chalk init` and `chalk quiz create` add
14
+ their paths (`chalk.toml`, each quiz directory) to the course's
15
+ `.codehoodignore`, so `codehood push` skips them.
16
+
17
+
18
+ Installation
19
+ ------------
20
+
21
+ ```bash
22
+ uvx chalk # or pipx run chalk
23
+ ```
24
+
25
+ Chalk needs Python 3.13 or newer.
26
+
27
+
28
+ Getting started
29
+ ---------------
30
+
31
+ ```bash
32
+ chalk init # creates chalk.toml and roster.csv
33
+ chalk quiz create p1 --questions 3 # creates p1/conf.yaml
34
+ # write the questions in p1/questions/ and the answers in p1/submissions.csv
35
+ chalk autograde p1/q1 # grades q1 with its answer key
36
+ chalk grade p1/q3 # grades q3 by hand
37
+ chalk show p1 --grades # shows the grades table
38
+ ```
39
+
40
+
41
+ Directory structure
42
+ -------------------
43
+
44
+ ```
45
+ classroom/
46
+ ├── chalk.toml # classroom configuration
47
+ ├── roster.csv # students
48
+ ├── conf.py # optional: final_grade() and other hooks
49
+ └── p1/ # a quiz: any directory with a conf.yaml
50
+ ├── conf.yaml # name, dates, weight and chalk settings of questions
51
+ ├── questions/ # question documents in the mdq format
52
+ │ ├── q1.md
53
+ │ └── q2.yaml
54
+ ├── q3/ # files of question q3: autograder script, template/
55
+ ├── submissions.csv # one row per student, one column per question
56
+ └── grades.csv # written by chalk
57
+ ```
58
+
59
+
60
+ Questions
61
+ ---------
62
+
63
+ Objective questions are [mdq](https://pypi.org/project/mdq/) documents in
64
+ `<quiz>/questions/`, named after the question id. The document may be
65
+ Markdown (`.md`, `.mdq.md`) or the parsed form of mdq in YAML or JSON. It
66
+ holds the stem, the choices and the answer key, and chalk grades responses
67
+ with mdq.
68
+
69
+ ```markdown
70
+ ---
71
+ id: q1
72
+ title: Capital
73
+ ---
74
+
75
+ What is the capital of Brazil?
76
+
77
+ - [*] [df] Brasília
78
+ - [ ] [sp] São Paulo
79
+ ```
80
+
81
+ `conf.yaml` lists the questions in order and keeps what is specific to chalk:
82
+
83
+ ```yaml
84
+ name: Exam 1
85
+ weight: 1
86
+ questions:
87
+ - id: q1 # mdq document in questions/q1.md
88
+ - id: q2
89
+ value: 2 # weight of the question in the quiz
90
+ - id: q3 # no document: an open question, graded by hand
91
+ - id: q4
92
+ type: text # free text, graded by an autograder script or a regex
93
+ format: python
94
+ - id: q5
95
+ type: repo # the answer is a directory, graded by a script
96
+ - id: q6
97
+ source: ../questions/recursion/01.md # mdq document from elsewhere
98
+ ```
99
+
100
+ `source` points to an mdq file outside `<quiz>/questions/`, relative to the
101
+ quiz directory. Use it to reuse questions from the `questions/` bank of a
102
+ codehood course without copying them. The quiz refers to the question by the
103
+ id of its entry, whatever id the file declares.
104
+
105
+ Answers in `submissions.csv` follow the mdq responses: the id of a choice (or
106
+ its text) for multiple choice, a list of ids such as `a, c` or `[a, c]` for
107
+ multiple selection, a number for numeric questions and text for the others.
108
+
109
+ ```csv
110
+ id,q1,q2,q3
111
+ 1234,df,"a, c",An open answer
112
+ ```
113
+
114
+
115
+ Roster
116
+ ------
117
+
118
+ `roster.csv` uses the columns that codehood also uses. Every column is
119
+ optional, and other columns are kept.
120
+
121
+ ```csv
122
+ school_id,username,name,email,github,status
123
+ 1234,jdoe,John Doe,john.doe@example.com,jdoe,active
124
+ ```
125
+
126
+ The student id is `school_id`. Set `student_id = "username"` (or another
127
+ column) in the `[chalk]` section of `chalk.toml` to use another column.
128
+ Rosters with an `id` column instead of `school_id` also work.
129
+
130
+
131
+ Documentation
132
+ -------------
133
+
134
+ ```bash
135
+ uv sync --group dev
136
+ uv run mkdocs serve
137
+ ```
@@ -0,0 +1,2 @@
1
+ __version__ = "0.1.0"
2
+ __authors__ = ["Fábio Macêdo Mendes"]
@@ -0,0 +1,4 @@
1
+ from .cli import main
2
+
3
+ if __name__ == "__main__":
4
+ main()
@@ -0,0 +1,40 @@
1
+ from locale import getdefaultlocale
2
+
3
+ __all__ = ["create_chalk_toml"]
4
+
5
+ CHALK_TOML = """[chalk]
6
+ classroom = "<classroom-name>"
7
+ teacher = "<teacher-name>"
8
+ lang = "<locale>"
9
+ """
10
+
11
+
12
+ def create_chalk_toml(
13
+ teacher: str = "<teacher-name>",
14
+ classroom: str = "<classroom-name>",
15
+ locale: str | None = None,
16
+ ) -> str:
17
+ """
18
+ Generate the contents of a chalk.toml file.
19
+
20
+ Args:
21
+ teacher: The name of the teacher.
22
+ classroom: The name of the classroom.
23
+ locale: The locale to use.
24
+ """
25
+ data = CHALK_TOML.replace("<teacher-name>", teacher)
26
+ data = data.replace("<classroom-name>", classroom)
27
+
28
+ if locale is None:
29
+ loc, encoding = getdefaultlocale()
30
+ if loc and encoding:
31
+ locale = f"{loc}.{encoding}"
32
+ elif loc:
33
+ locale = loc
34
+ elif encoding:
35
+ locale = encoding
36
+ else:
37
+ locale = "C"
38
+
39
+ data = data.replace("<locale>", locale)
40
+ return data
File without changes
@@ -0,0 +1,167 @@
1
+ """
2
+ Grade answers that are regular expressions (the `regex` autograder profile).
3
+
4
+ The student's regex runs in a separate Python process, because a regex with
5
+ catastrophic backtracking cannot be interrupted inside the process that runs
6
+ it. Each example has its own timeout; when it expires, the worker is killed,
7
+ the example counts as wrong and a new worker takes the remaining examples.
8
+ """
9
+
10
+ from __future__ import annotations
11
+
12
+ import json
13
+ import selectors
14
+ import subprocess
15
+ import sys
16
+ from dataclasses import dataclass
17
+ from fractions import Fraction
18
+ from typing import Any
19
+
20
+ from ..core import StudentId, models
21
+ from ..core import disqualification as dq
22
+ from ..core.grader import Graded, Grader
23
+
24
+ #: Seconds each example may take.
25
+ EXAMPLE_TIMEOUT = 1.0
26
+
27
+ #: Address space of the worker process.
28
+ WORKER_MEMORY = 512 * 1024 * 1024
29
+
30
+ WORKER = """
31
+ import json, re, resource, sys
32
+ resource.setrlimit(resource.RLIMIT_AS, (int(sys.argv[3]), int(sys.argv[3])))
33
+ try:
34
+ pattern = re.compile(sys.argv[1])
35
+ except re.error as e:
36
+ print(json.dumps({"error": str(e)}), flush=True)
37
+ raise SystemExit
38
+ print(json.dumps({"ok": True}), flush=True)
39
+ match = pattern.fullmatch if sys.argv[2] == "full" else pattern.search
40
+ for line in sys.stdin:
41
+ print(json.dumps(match(json.loads(line)) is not None), flush=True)
42
+ """
43
+
44
+
45
+ class InvalidRegex(Exception):
46
+ pass
47
+
48
+
49
+ @dataclass(frozen=True)
50
+ class RegexAnswerGrader(Grader[Any]):
51
+ """
52
+ Grade a regex answer by the examples it must match and must not match.
53
+
54
+ The score is the fraction of examples with the expected outcome.
55
+ """
56
+
57
+ conf: models.Autograder
58
+ example_timeout: float = EXAMPLE_TIMEOUT
59
+
60
+ def __post_init__(self) -> None:
61
+ if not self.conf.match and not self.conf.reject:
62
+ raise ValueError("the regex profile needs examples in `match` or `reject`")
63
+
64
+ def __call__(self, id: StudentId, answer: Any, /, **kwargs: Any) -> Graded | None:
65
+ if not isinstance(answer, str):
66
+ return Graded(dq.InvalidInput(), feedback="The answer must be a regex.")
67
+ if len(answer.encode()) > self.conf.limits.answer_size:
68
+ limit = self.conf.limits.answer_size
69
+ return Graded(dq.InvalidInput(), feedback=f"The answer is larger than {limit} bytes.")
70
+
71
+ pattern = answer.rstrip("\r\n")
72
+ if not pattern:
73
+ return Graded(Fraction(0), feedback="Empty regex.")
74
+
75
+ examples = [(ex, True) for ex in self.conf.match] + [(ex, False) for ex in self.conf.reject]
76
+ try:
77
+ outcomes = run_examples(
78
+ pattern, [ex for ex, _ in examples], self.conf.regex_mode, self.example_timeout
79
+ )
80
+ except InvalidRegex as e:
81
+ return Graded(Fraction(0), feedback=f"Invalid regex: {e}")
82
+
83
+ wrong: list[str] = []
84
+ for (example, expected), outcome in zip(examples, outcomes, strict=True):
85
+ if outcome is None:
86
+ wrong.append(f"- took too long on {example!r}")
87
+ elif outcome != expected:
88
+ verb = "should match" if expected else "should not match"
89
+ wrong.append(f"- {verb} {example!r}")
90
+
91
+ score = Fraction(len(examples) - len(wrong), len(examples))
92
+ return Graded(score, feedback="\n".join(wrong) or None)
93
+
94
+
95
+ def run_examples(pattern: str, examples: list[str], mode: str, timeout: float) -> list[bool | None]:
96
+ """
97
+ Test the pattern against each example in a worker process.
98
+
99
+ Returns True or False for each example, or None when the example timed
100
+ out. Raises InvalidRegex if the pattern does not compile.
101
+ """
102
+ outcomes: list[bool | None] = []
103
+ pending = list(examples)
104
+ while pending:
105
+ with _Worker(pattern, mode) as worker:
106
+ worker.check_compiled(timeout)
107
+ while pending:
108
+ outcome = worker.test(pending[0], timeout)
109
+ pending.pop(0)
110
+ outcomes.append(outcome)
111
+ if outcome is None:
112
+ break # the worker was killed; start another one
113
+ return outcomes
114
+
115
+
116
+ class _Worker:
117
+ def __init__(self, pattern: str, mode: str) -> None:
118
+ self.process = subprocess.Popen(
119
+ [sys.executable, "-I", "-c", WORKER, pattern, mode, str(WORKER_MEMORY)],
120
+ stdin=subprocess.PIPE,
121
+ stdout=subprocess.PIPE,
122
+ stderr=subprocess.DEVNULL,
123
+ text=True,
124
+ env={},
125
+ )
126
+ assert self.process.stdout is not None
127
+ self._selector = selectors.DefaultSelector()
128
+ self._selector.register(self.process.stdout, selectors.EVENT_READ)
129
+
130
+ def __enter__(self) -> _Worker:
131
+ return self
132
+
133
+ def __exit__(self, *exc: object) -> None:
134
+ self.process.kill()
135
+ self.process.wait()
136
+ self._selector.close()
137
+
138
+ def _read(self, timeout: float) -> Any:
139
+ if not self._selector.select(timeout):
140
+ return None
141
+ assert self.process.stdout is not None
142
+ line = self.process.stdout.readline()
143
+ if not line:
144
+ raise RuntimeError("regex worker died")
145
+ return json.loads(line)
146
+
147
+ def check_compiled(self, timeout: float) -> None:
148
+ try:
149
+ reply = self._read(timeout)
150
+ except RuntimeError:
151
+ raise InvalidRegex("the regex uses too much memory") from None
152
+ if reply is None:
153
+ raise InvalidRegex("the regex took too long to compile")
154
+ if "error" in reply:
155
+ raise InvalidRegex(reply["error"])
156
+
157
+ def test(self, example: str, timeout: float) -> bool | None:
158
+ assert self.process.stdin is not None
159
+ try:
160
+ self.process.stdin.write(json.dumps(example) + "\n")
161
+ self.process.stdin.flush()
162
+ except BrokenPipeError:
163
+ return None
164
+ try:
165
+ return self._read(timeout)
166
+ except RuntimeError:
167
+ return None # killed by the memory limit
@@ -0,0 +1,30 @@
1
+ from dataclasses import dataclass
2
+ from decimal import Decimal
3
+ from re import Pattern
4
+ from typing import Any, Literal
5
+
6
+ from ..core import StudentId
7
+ from ..core.grader import Graded, Grader
8
+
9
+
10
+ @dataclass(frozen=True)
11
+ class RegexGrader(Grader[str]):
12
+ """
13
+ Grades based on a regular expression.
14
+
15
+ The answer is considered correct if it matches the regex.
16
+ """
17
+
18
+ pattern: Pattern[str]
19
+ grade: Decimal = Decimal(1)
20
+ match: Literal["full", "search", "start"] = "full"
21
+
22
+ def __call__(self, id: StudentId, answer: str, /, **kwargs: Any) -> Graded | None:
23
+ if self.match == "full" and self.pattern.fullmatch(answer):
24
+ return Graded(self.grade)
25
+ elif self.match == "search" and self.pattern.search(answer):
26
+ return Graded(self.grade)
27
+ elif self.match == "start" and self.pattern.match(answer):
28
+ return Graded(self.grade)
29
+ else:
30
+ return Graded(score=Decimal(0))
@@ -0,0 +1,65 @@
1
+ import sys
2
+
3
+ import typer
4
+
5
+ from chalk_grader import cgroup
6
+
7
+ from . import (
8
+ autograde_cmd,
9
+ clear_app,
10
+ codehood_app,
11
+ contrib_app,
12
+ grade_cmd,
13
+ info_app,
14
+ init_app,
15
+ quiz_app,
16
+ response_cmds,
17
+ roster_app,
18
+ show_cmd,
19
+ )
20
+
21
+ app = typer.Typer(name="Chalk", no_args_is_help=True, add_help_option=True)
22
+
23
+
24
+ NOT_GIVEN = object()
25
+ ADD_STUDENT_OPTIONS = {"email", "github", "name", "force"}
26
+
27
+ init_app.add_init_command(app)
28
+ app.add_typer(
29
+ roster_app.app,
30
+ name="roster",
31
+ help="Manage the student roster.",
32
+ )
33
+ app.add_typer(
34
+ quiz_app.app,
35
+ name="quiz",
36
+ help="Manage quizzes.",
37
+ )
38
+ app.add_typer(
39
+ codehood_app.app,
40
+ name="codehood",
41
+ help="Exchange data with a codehood course in the same repository.",
42
+ )
43
+ grade_cmd.add_grade_cmd(app)
44
+ autograde_cmd.add_autograde_command(app)
45
+ info_app.add_info_command(app)
46
+ contrib_app.add_contrib_command(app)
47
+ clear_app.add_clear_command(app)
48
+ show_cmd.add_show_command(app)
49
+ response_cmds.add_response_commands(app)
50
+
51
+
52
+ #: Commands that grade: they run again in a cgroup of their own, so that
53
+ #: each run gets a memory and process limit (see chalk_grader.cgroup).
54
+ GRADING_COMMANDS = (("autograde",), ("codehood", "autograde"))
55
+
56
+
57
+ def main() -> None:
58
+ words = [arg for arg in sys.argv[1:] if not arg.startswith("-")]
59
+ grading = any(tuple(words[: len(c)]) == c for c in GRADING_COMMANDS)
60
+ if grading and "--help" not in sys.argv:
61
+ cgroup.reexec_delegated()
62
+ # Before the grader starts any process: the setup of the cgroups
63
+ # gives up when the delegated cgroup has other processes.
64
+ cgroup.controller()
65
+ app()