chalk-classroom 0.1.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.
Files changed (86) hide show
  1. chalk/__init__.py +2 -0
  2. chalk/__main__.py +4 -0
  3. chalk/assets.py +40 -0
  4. chalk/autograder/__init__.py +0 -0
  5. chalk/autograder/regex_answer.py +167 -0
  6. chalk/autograder/regex_grader.py +30 -0
  7. chalk/cli/__init__.py +65 -0
  8. chalk/cli/autograde_cmd.py +433 -0
  9. chalk/cli/clear_app.py +43 -0
  10. chalk/cli/codehood_app.py +110 -0
  11. chalk/cli/contrib_app.py +85 -0
  12. chalk/cli/grade_cmd.py +197 -0
  13. chalk/cli/info_app.py +48 -0
  14. chalk/cli/init_app.py +64 -0
  15. chalk/cli/quiz_app.py +112 -0
  16. chalk/cli/response_cmds.py +160 -0
  17. chalk/cli/roster_app.py +315 -0
  18. chalk/cli/show_cmd.py +356 -0
  19. chalk/cli/util.py +106 -0
  20. chalk/codehood.py +169 -0
  21. chalk/codehood_api.py +103 -0
  22. chalk/codehood_autograde.py +814 -0
  23. chalk/contrib/__init__.py +0 -0
  24. chalk/contrib/data/__init__.py +0 -0
  25. chalk/contrib/data/convert.py +106 -0
  26. chalk/contrib/github/__init__.py +0 -0
  27. chalk/contrib/github/classroom.py +236 -0
  28. chalk/contrib/github/repo_submissions.py +107 -0
  29. chalk/contrib/github/roster.py +150 -0
  30. chalk/contrib/grades/__init__.py +0 -0
  31. chalk/contrib/grades/rescale.py +56 -0
  32. chalk/contrib/parser/__init__.py +0 -0
  33. chalk/contrib/parser/elm_test.py +78 -0
  34. chalk/core/__init__.py +66 -0
  35. chalk/core/adjustment.py +421 -0
  36. chalk/core/answer.py +160 -0
  37. chalk/core/classroom.py +600 -0
  38. chalk/core/decimal.py +107 -0
  39. chalk/core/disqualification.py +228 -0
  40. chalk/core/error.py +152 -0
  41. chalk/core/grader.py +140 -0
  42. chalk/core/grades.py +817 -0
  43. chalk/core/issue.py +303 -0
  44. chalk/core/models.py +271 -0
  45. chalk/core/quiz.py +487 -0
  46. chalk/core/roster.py +355 -0
  47. chalk/core/score.py +34 -0
  48. chalk/core/student.py +162 -0
  49. chalk/core/submissions.py +893 -0
  50. chalk/data/__init__.py +66 -0
  51. chalk/data/core.py +300 -0
  52. chalk/data/disqualifiers.py +144 -0
  53. chalk/data/json.py +57 -0
  54. chalk/data/protocols.py +32 -0
  55. chalk/data/serializers.py +169 -0
  56. chalk/data/validators.py +361 -0
  57. chalk/data/yaml.py +112 -0
  58. chalk/env.py +91 -0
  59. chalk/info.py +81 -0
  60. chalk/numeric.py +97 -0
  61. chalk/protocols.py +60 -0
  62. chalk/py.typed +0 -0
  63. chalk/question/__init__.py +119 -0
  64. chalk/question/base.py +712 -0
  65. chalk/question/mdq.py +391 -0
  66. chalk/question/renderable.py +111 -0
  67. chalk/question/repo.py +121 -0
  68. chalk/question/textual.py +138 -0
  69. chalk/question/util.py +39 -0
  70. chalk/table.py +505 -0
  71. chalk/testing/strategies.py +77 -0
  72. chalk/types.py +7 -0
  73. chalk/util/__init__.py +130 -0
  74. chalk/util/dataframe.py +49 -0
  75. chalk/util/external.py +13 -0
  76. chalk/util/functional.py +112 -0
  77. chalk/util/parsing.py +112 -0
  78. chalk/util/path.py +310 -0
  79. chalk/util/sequence.py +93 -0
  80. chalk/util/show.py +160 -0
  81. chalk/util/string.py +27 -0
  82. chalk_classroom-0.1.0.dist-info/METADATA +170 -0
  83. chalk_classroom-0.1.0.dist-info/RECORD +86 -0
  84. chalk_classroom-0.1.0.dist-info/WHEEL +4 -0
  85. chalk_classroom-0.1.0.dist-info/entry_points.txt +3 -0
  86. chalk_classroom-0.1.0.dist-info/licenses/LICENSE +21 -0
chalk/__init__.py ADDED
@@ -0,0 +1,2 @@
1
+ __version__ = "0.1.0"
2
+ __authors__ = ["Fábio Macêdo Mendes"]
chalk/__main__.py ADDED
@@ -0,0 +1,4 @@
1
+ from .cli import main
2
+
3
+ if __name__ == "__main__":
4
+ main()
chalk/assets.py ADDED
@@ -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))
chalk/cli/__init__.py ADDED
@@ -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()