PyKubeGrader 0.3.4__py3-none-any.whl → 0.3.5__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.
- {PyKubeGrader-0.3.4.dist-info → PyKubeGrader-0.3.5.dist-info}/METADATA +1 -1
- {PyKubeGrader-0.3.4.dist-info → PyKubeGrader-0.3.5.dist-info}/RECORD +9 -9
- pykubegrader/build/api_notebook_builder.py +86 -24
- pykubegrader/widgets_base/multi_select.py +2 -1
- pykubegrader/widgets_base/select.py +2 -1
- {PyKubeGrader-0.3.4.dist-info → PyKubeGrader-0.3.5.dist-info}/LICENSE.txt +0 -0
- {PyKubeGrader-0.3.4.dist-info → PyKubeGrader-0.3.5.dist-info}/WHEEL +0 -0
- {PyKubeGrader-0.3.4.dist-info → PyKubeGrader-0.3.5.dist-info}/entry_points.txt +0 -0
- {PyKubeGrader-0.3.4.dist-info → PyKubeGrader-0.3.5.dist-info}/top_level.txt +0 -0
@@ -5,7 +5,7 @@ pykubegrader/telemetry.py,sha256=ooLK-dY_hJQ7t4r83hWyO8wx6F_7TfWJS7tCp_nH7r8,130
|
|
5
5
|
pykubegrader/utils.py,sha256=jlJklKvRhY3O7Hz2aaU1m0y3p_n9eMAXNnAF7LUEaPY,1275
|
6
6
|
pykubegrader/validate.py,sha256=OKnItGyd-L8QPKcsE0KRuwBI_IxKiJzMLJKZiA2j3II,11184
|
7
7
|
pykubegrader/build/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
8
|
-
pykubegrader/build/api_notebook_builder.py,sha256=
|
8
|
+
pykubegrader/build/api_notebook_builder.py,sha256=320aFB1-6KLQt0OMmKD0FJtE17JKZaK5neA0mTD-Y8M,22960
|
9
9
|
pykubegrader/build/build_folder.py,sha256=Asc-VdhXgxQfOfFIWJShhXrF2EITJOIZQ5Dz_2y-P2I,85358
|
10
10
|
pykubegrader/build/clean_folder.py,sha256=8N0KyL4eXRs0DCw-V_2jR9igtFs_mOFMQufdL6tD-38,1323
|
11
11
|
pykubegrader/build/collate.py,sha256=cVvF7tf2U3iiH4R_dbghTcieedIx5w3Fyw9L_llInM8,6754
|
@@ -29,12 +29,12 @@ pykubegrader/widgets/style.py,sha256=fVBMYy_a6Yoz21avNpiORWC3f5FD-OrVpaZ3npmunvs
|
|
29
29
|
pykubegrader/widgets/true_false.py,sha256=QllIhHuJstJft_RuShkxI_fFFTaDAlzNZOFNs00HLIM,2842
|
30
30
|
pykubegrader/widgets/types_question.py,sha256=kZdRRXyFzOtYTmGdC7XWb_2oaxqg1WSuLcQn_sTj6Qc,2300
|
31
31
|
pykubegrader/widgets_base/__init__.py,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
32
|
-
pykubegrader/widgets_base/multi_select.py,sha256=
|
32
|
+
pykubegrader/widgets_base/multi_select.py,sha256=KtfAP0PyEbcjWlKNpI5_5-PLMtcUbbNX0Es_-w-H34Q,4226
|
33
33
|
pykubegrader/widgets_base/reading.py,sha256=ChUS3NOTa_HLtNpxR8hGX80LPKMvYMypnR6dFknfxus,5430
|
34
|
-
pykubegrader/widgets_base/select.py,sha256=
|
35
|
-
PyKubeGrader-0.3.
|
36
|
-
PyKubeGrader-0.3.
|
37
|
-
PyKubeGrader-0.3.
|
38
|
-
PyKubeGrader-0.3.
|
39
|
-
PyKubeGrader-0.3.
|
40
|
-
PyKubeGrader-0.3.
|
34
|
+
pykubegrader/widgets_base/select.py,sha256=uMncmVIqjvJkffMQY1L_PokrFCidK1PeVITX0i70fho,2750
|
35
|
+
PyKubeGrader-0.3.5.dist-info/LICENSE.txt,sha256=YTp-Ewc8Kems8PJEE27KnBPFnZSxoWvSg7nnknzPyYw,1546
|
36
|
+
PyKubeGrader-0.3.5.dist-info/METADATA,sha256=a5vrrGdf4XTTSfKPAuQ-2HaHISFczwu6uSy6KkrrQv4,2729
|
37
|
+
PyKubeGrader-0.3.5.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
38
|
+
PyKubeGrader-0.3.5.dist-info/entry_points.txt,sha256=RR57KvzDRJrP4omy5heS5cZ3E7g56YxcxJhDnp57ZU0,253
|
39
|
+
PyKubeGrader-0.3.5.dist-info/top_level.txt,sha256=e550Klfze6higFxER1V62fnGOcIgiKRbsrl9CC4UdtQ,13
|
40
|
+
PyKubeGrader-0.3.5.dist-info/RECORD,,
|
@@ -5,6 +5,8 @@ import shutil
|
|
5
5
|
from dataclasses import dataclass
|
6
6
|
from pathlib import Path
|
7
7
|
from typing import Optional
|
8
|
+
import base64
|
9
|
+
from typing import Any, Optional
|
8
10
|
|
9
11
|
import nbformat
|
10
12
|
|
@@ -16,14 +18,14 @@ class FastAPINotebookBuilder:
|
|
16
18
|
assignment_tag: Optional[str] = ""
|
17
19
|
require_key: Optional[bool] = False
|
18
20
|
|
19
|
-
def __post_init__(self):
|
21
|
+
def __post_init__(self) -> None:
|
20
22
|
self.root_path, self.filename = FastAPINotebookBuilder.get_filename_and_root(
|
21
23
|
self.notebook_path
|
22
24
|
)
|
23
25
|
self.total_points = 0
|
24
26
|
self.run()
|
25
27
|
|
26
|
-
def run(self):
|
28
|
+
def run(self) -> None:
|
27
29
|
# here for easy debugging
|
28
30
|
if self.temp_notebook is not None:
|
29
31
|
shutil.copy(
|
@@ -36,7 +38,46 @@ class FastAPINotebookBuilder:
|
|
36
38
|
self.assertion_tests_dict = self.question_dict()
|
37
39
|
self.add_api_code()
|
38
40
|
|
41
|
+
@staticmethod
|
42
|
+
def conceal_tests(cell_source):
|
43
|
+
"""
|
44
|
+
Takes a list of code lines, detects blocks between `# BEGIN HIDE` and `# END HIDE`,
|
45
|
+
encodes them in Base64, and replaces them with an `exec()` statement.
|
46
|
+
|
47
|
+
Returns a new list of lines with the concealed blocks.
|
48
|
+
"""
|
49
|
+
|
50
|
+
concealed_lines = []
|
51
|
+
hide_mode = False
|
52
|
+
hidden_code = []
|
53
|
+
|
54
|
+
for line in cell_source:
|
55
|
+
if "# BEGIN HIDE" in line:
|
56
|
+
hide_mode = True
|
57
|
+
hidden_code = [] # Start a new hidden block
|
58
|
+
concealed_lines.append(line) # Keep the marker for clarity
|
59
|
+
continue
|
60
|
+
elif "# END HIDE" in line:
|
61
|
+
hide_mode = False
|
62
|
+
# Encode the entire block
|
63
|
+
encoded_block = base64.b64encode(
|
64
|
+
"\n".join(hidden_code).encode()
|
65
|
+
).decode()
|
66
|
+
concealed_lines.append(
|
67
|
+
f'exec(base64.b64decode("{encoded_block}").decode()) # Obfuscated\n'
|
68
|
+
)
|
69
|
+
concealed_lines.append(line) # Keep the marker for clarity
|
70
|
+
continue
|
71
|
+
|
72
|
+
if hide_mode:
|
73
|
+
hidden_code.append(line.strip()) # Collect hidden code
|
74
|
+
else:
|
75
|
+
concealed_lines.append(line)
|
76
|
+
|
77
|
+
return concealed_lines
|
78
|
+
|
39
79
|
def add_api_code(self):
|
80
|
+
def add_api_code(self) -> None:
|
40
81
|
self.compute_max_points_free_response()
|
41
82
|
|
42
83
|
for i, (cell_index, cell_dict) in enumerate(self.assertion_tests_dict.items()):
|
@@ -48,6 +89,8 @@ class FastAPINotebookBuilder:
|
|
48
89
|
cell_source = FastAPINotebookBuilder.add_import_statements_to_tests(
|
49
90
|
cell["source"]
|
50
91
|
)
|
92
|
+
|
93
|
+
cell_source = FastAPINotebookBuilder.conceal_tests(cell_source)
|
51
94
|
|
52
95
|
last_import_line_ind = FastAPINotebookBuilder.find_last_import_line(
|
53
96
|
cell_source
|
@@ -94,7 +137,7 @@ class FastAPINotebookBuilder:
|
|
94
137
|
|
95
138
|
self.replace_cell_source(cell_index, updated_cell_source)
|
96
139
|
|
97
|
-
def compute_max_points_free_response(self):
|
140
|
+
def compute_max_points_free_response(self) -> None:
|
98
141
|
for cell_dict in self.assertion_tests_dict.values():
|
99
142
|
# gets the question name from the first cell to not double count
|
100
143
|
if cell_dict["is_first"]:
|
@@ -107,7 +150,7 @@ class FastAPINotebookBuilder:
|
|
107
150
|
|
108
151
|
self.total_points += max_question_points
|
109
152
|
|
110
|
-
def construct_first_cell_question_header(self, cell_dict):
|
153
|
+
def construct_first_cell_question_header(self, cell_dict: dict) -> list[str]:
|
111
154
|
max_question_points = sum(
|
112
155
|
cell["points"]
|
113
156
|
for cell in self.assertion_tests_dict.values()
|
@@ -136,7 +179,7 @@ class FastAPINotebookBuilder:
|
|
136
179
|
return first_cell_header
|
137
180
|
|
138
181
|
@staticmethod
|
139
|
-
def construct_update_responses(cell_dict):
|
182
|
+
def construct_update_responses(cell_dict: dict) -> list[str]:
|
140
183
|
update_responses = []
|
141
184
|
|
142
185
|
logging_variables = cell_dict["logging_variables"]
|
@@ -149,7 +192,9 @@ class FastAPINotebookBuilder:
|
|
149
192
|
return update_responses
|
150
193
|
|
151
194
|
@staticmethod
|
152
|
-
def split_list_at_marker(
|
195
|
+
def split_list_at_marker(
|
196
|
+
input_list: list[str], marker: str = """# END TEST CONFIG"""
|
197
|
+
) -> tuple[list[str], list[str]]:
|
153
198
|
"""
|
154
199
|
Splits a list into two parts at the specified marker string.
|
155
200
|
|
@@ -172,7 +217,7 @@ class FastAPINotebookBuilder:
|
|
172
217
|
) # If the marker is not in the list, return the original list and an empty list
|
173
218
|
|
174
219
|
@staticmethod
|
175
|
-
def construct_graders(cell_dict):
|
220
|
+
def construct_graders(cell_dict: dict) -> list[str]:
|
176
221
|
# Generate Python code
|
177
222
|
added_code = [
|
178
223
|
"if "
|
@@ -184,7 +229,7 @@ class FastAPINotebookBuilder:
|
|
184
229
|
return added_code
|
185
230
|
|
186
231
|
@staticmethod
|
187
|
-
def construct_question_info(cell_dict):
|
232
|
+
def construct_question_info(cell_dict: dict) -> list[str]:
|
188
233
|
question_info = []
|
189
234
|
|
190
235
|
question_id = cell_dict["question"] + "-" + str(cell_dict["test_number"])
|
@@ -197,8 +242,12 @@ class FastAPINotebookBuilder:
|
|
197
242
|
|
198
243
|
@staticmethod
|
199
244
|
def insert_list_at_index(
|
200
|
-
original_list
|
201
|
-
|
245
|
+
original_list: list[str],
|
246
|
+
insert_list: list[str],
|
247
|
+
index: int,
|
248
|
+
line_break: bool = True,
|
249
|
+
inplace_line_break: bool = True,
|
250
|
+
) -> list[str]:
|
202
251
|
"""
|
203
252
|
Inserts a list into another list at a specific index.
|
204
253
|
|
@@ -223,7 +272,7 @@ class FastAPINotebookBuilder:
|
|
223
272
|
return original_list[:index] + insert_list + original_list[index:]
|
224
273
|
|
225
274
|
@staticmethod
|
226
|
-
def add_import_statements_to_tests(cell_source):
|
275
|
+
def add_import_statements_to_tests(cell_source: list[str]) -> list[str]:
|
227
276
|
"""
|
228
277
|
Adds the necessary import statements to the first cell of the notebook.
|
229
278
|
"""
|
@@ -241,6 +290,7 @@ class FastAPINotebookBuilder:
|
|
241
290
|
" update_responses,\n",
|
242
291
|
")\n",
|
243
292
|
"import os\n",
|
293
|
+
"import base64\n",
|
244
294
|
]
|
245
295
|
|
246
296
|
for i, line in enumerate(cell_source):
|
@@ -251,7 +301,12 @@ class FastAPINotebookBuilder:
|
|
251
301
|
] + imports # Add a blank line for readability
|
252
302
|
return cell_source # Exit the loop once the imports are inserted
|
253
303
|
|
254
|
-
|
304
|
+
raise ValueError("End of test configuration not found")
|
305
|
+
|
306
|
+
# TODO: `Any` return not good; would be better to specify return type(s)
|
307
|
+
def extract_first_cell(self) -> Any:
|
308
|
+
if not self.temp_notebook:
|
309
|
+
raise ValueError("No temporary notebook file path provided")
|
255
310
|
with open(self.temp_notebook, "r", encoding="utf-8") as f:
|
256
311
|
notebook = json.load(f)
|
257
312
|
if "cells" in notebook and len(notebook["cells"]) > 0:
|
@@ -260,13 +315,16 @@ class FastAPINotebookBuilder:
|
|
260
315
|
return None
|
261
316
|
|
262
317
|
@staticmethod
|
263
|
-
def get_filename_and_root(path):
|
318
|
+
def get_filename_and_root(path: str) -> tuple[Path, str]:
|
264
319
|
path_obj = Path(path).resolve() # Resolve the path to get an absolute path
|
265
320
|
root_path = path_obj.parent # Get the parent directory
|
266
321
|
filename = path_obj.name # Get the filename
|
267
322
|
return root_path, filename
|
268
323
|
|
269
|
-
|
324
|
+
# TODO: `Any` return not good; would be better to specify return type(s)
|
325
|
+
def get_cell(self, cell_index: int) -> Any:
|
326
|
+
if not self.temp_notebook:
|
327
|
+
raise ValueError("No temporary notebook file path provided")
|
270
328
|
with open(self.temp_notebook, "r", encoding="utf-8") as f:
|
271
329
|
notebook = json.load(f)
|
272
330
|
if "cells" in notebook and len(notebook["cells"]) > cell_index:
|
@@ -274,7 +332,7 @@ class FastAPINotebookBuilder:
|
|
274
332
|
else:
|
275
333
|
return None
|
276
334
|
|
277
|
-
def replace_cell_source(self, cell_index, new_source):
|
335
|
+
def replace_cell_source(self, cell_index: int, new_source: str | list[str]) -> None:
|
278
336
|
"""
|
279
337
|
Replace the source code of a specific Jupyter notebook cell.
|
280
338
|
|
@@ -283,6 +341,8 @@ class FastAPINotebookBuilder:
|
|
283
341
|
new_source (str): New source code to replace the cell's content.
|
284
342
|
"""
|
285
343
|
# Load the notebook
|
344
|
+
if not self.temp_notebook:
|
345
|
+
raise ValueError("No temporary notebook file path provided")
|
286
346
|
with open(self.temp_notebook, "r", encoding="utf-8") as f:
|
287
347
|
notebook = nbformat.read(f, as_version=4)
|
288
348
|
|
@@ -301,7 +361,7 @@ class FastAPINotebookBuilder:
|
|
301
361
|
print(f"Updated notebook saved to {self.temp_notebook}")
|
302
362
|
|
303
363
|
@staticmethod
|
304
|
-
def find_last_import_line(cell_source):
|
364
|
+
def find_last_import_line(cell_source: list[str]) -> int:
|
305
365
|
"""
|
306
366
|
Finds the index of the last line with an import statement in a list of code lines,
|
307
367
|
including multiline import statements.
|
@@ -339,7 +399,7 @@ class FastAPINotebookBuilder:
|
|
339
399
|
return last_import_index
|
340
400
|
|
341
401
|
@staticmethod
|
342
|
-
def extract_log_variables(cell):
|
402
|
+
def extract_log_variables(cell: dict) -> list[str]:
|
343
403
|
"""Extracts log variables from the first cell."""
|
344
404
|
if "source" in cell:
|
345
405
|
for line in cell["source"]:
|
@@ -355,7 +415,8 @@ class FastAPINotebookBuilder:
|
|
355
415
|
pass
|
356
416
|
return []
|
357
417
|
|
358
|
-
|
418
|
+
@staticmethod
|
419
|
+
def tag_questions(cells_dict: dict) -> dict:
|
359
420
|
"""
|
360
421
|
Adds 'is_first' and 'is_last' boolean flags to the cells based on their position
|
361
422
|
within the group of the same question. All cells will have both flags.
|
@@ -377,7 +438,7 @@ class FastAPINotebookBuilder:
|
|
377
438
|
raise KeyError(f"Cell {key} is missing the 'question' key.")
|
378
439
|
|
379
440
|
# Group the keys by question name
|
380
|
-
question_groups = {}
|
441
|
+
question_groups: dict = {}
|
381
442
|
for key, cell in cells_dict.items():
|
382
443
|
question = cell.get(
|
383
444
|
"question"
|
@@ -397,7 +458,9 @@ class FastAPINotebookBuilder:
|
|
397
458
|
|
398
459
|
return cells_dict
|
399
460
|
|
400
|
-
def question_dict(self):
|
461
|
+
def question_dict(self) -> dict:
|
462
|
+
if not self.temp_notebook:
|
463
|
+
raise ValueError("No temporary notebook file path provided")
|
401
464
|
notebook_path = Path(self.temp_notebook)
|
402
465
|
if not notebook_path.exists():
|
403
466
|
raise FileNotFoundError(f"The file {notebook_path} does not exist.")
|
@@ -406,15 +469,14 @@ class FastAPINotebookBuilder:
|
|
406
469
|
notebook = json.load(f)
|
407
470
|
|
408
471
|
results_dict = {}
|
472
|
+
question_name = None # At least define the variable up front
|
409
473
|
|
410
474
|
for cell_index, cell in enumerate(notebook.get("cells", [])):
|
411
475
|
if cell.get("cell_type") == "raw":
|
412
476
|
source = "".join(cell.get("source", ""))
|
413
477
|
if source.strip().startswith("# BEGIN QUESTION"):
|
414
|
-
|
415
|
-
question_name = (
|
416
|
-
question_name.group(1).strip() if question_name else None
|
417
|
-
)
|
478
|
+
name_match = re.search(r"name:\s*(.*)", source)
|
479
|
+
question_name = name_match.group(1).strip() if name_match else None
|
418
480
|
|
419
481
|
elif cell.get("cell_type") == "code":
|
420
482
|
source = "".join(cell.get("source", ""))
|
@@ -2,6 +2,7 @@ import time
|
|
2
2
|
from typing import Callable, Optional, Tuple
|
3
3
|
|
4
4
|
import panel as pn
|
5
|
+
import numpy as np
|
5
6
|
|
6
7
|
from ..telemetry import ensure_responses, score_question, update_responses
|
7
8
|
from ..utils import shuffle_options, shuffle_questions
|
@@ -70,7 +71,7 @@ class MultiSelectQuestion:
|
|
70
71
|
|
71
72
|
# Panel layout
|
72
73
|
question_header = pn.pane.HTML(
|
73
|
-
f"<h2>Question {self.question_number}: {title}</h2>"
|
74
|
+
f"<h2>Question {self.question_number} (Points {np.sum(points)}): {title}</h2>"
|
74
75
|
)
|
75
76
|
|
76
77
|
question_body = pn.Column(
|
@@ -2,6 +2,7 @@ import time
|
|
2
2
|
from typing import Callable, Optional, Tuple
|
3
3
|
|
4
4
|
import panel as pn
|
5
|
+
import numpy as np
|
5
6
|
|
6
7
|
from ..telemetry import ensure_responses, score_question, update_responses
|
7
8
|
from ..utils import shuffle_options, shuffle_questions
|
@@ -57,7 +58,7 @@ class SelectQuestion:
|
|
57
58
|
widget_pairs = shuffle_questions(desc_widgets, self.widgets, seed)
|
58
59
|
|
59
60
|
self.layout = pn.Column(
|
60
|
-
f"# Question {self.question_number}: {title}",
|
61
|
+
f"# Question {self.question_number} (Points {np.sum(points)}): {title}",
|
61
62
|
*(
|
62
63
|
pn.Column(desc_widget, pn.Row(dropdown))
|
63
64
|
for desc_widget, dropdown in widget_pairs
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|