forked from ucbds-infra/otter-grader
/
questions.py
86 lines (72 loc) · 2.38 KB
/
questions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Question configurations for Otter Assign
"""
import copy
import yaml
from .constants import BLOCK_QUOTE, ALLOWED_NAME
from .utils import get_source, lock, get_spec, EmptyCellException
from ..utils import convert_config_description_dict
_DEFAULT_QUESTION_CONFIGURATIONS_WITH_DESCRIPTIONS = [
{
"key": "name",
"description": "(required) the path to a requirements.txt file",
"required": True,
},
{
"key": "manual",
"description": "whether this is a manually-graded question",
"default": False,
},
{
"key": "points",
"description": "how many points this question is worth; defaults to 1 internally",
"default": None,
},
{
"key": "check_cell",
"description": "whether to include a check cell after this question (for autograded questions only)",
"default": True,
},
{
"key": "export",
"description": "whether to force-include this question in the exported PDF",
"default": False,
},
]
DEFAULT_QUESTION_CONFIGURATIONS = convert_config_description_dict(_DEFAULT_QUESTION_CONFIGURATIONS_WITH_DESCRIPTIONS)
def create_question_config(parsed_config):
"""
"""
config = DEFAULT_QUESTION_CONFIGURATIONS.copy()
config.update(parsed_config)
if "name" not in config:
raise ValueError(f"Question name not specified in config YAML: {parsed_config}")
return config
def is_question_cell(cell):
"""
Returns whether cell contains BEGIN QUESTION in a block quote
Args:
cell (``nbformat.NotebookNode``): a notebook cell
Returns:
``bool``: whether the current cell is a question definition cell
"""
if cell.cell_type != 'markdown':
return False
return get_spec(get_source(cell), "question") is not None
def read_question_metadata(cell):
"""
Returns parsed question metadata from a question cell
Args:
cell (``nbformat.NotebookNode``): the question cell
Returns:
``dict``: question metadata
"""
source = get_source(cell)
begin_question_line = get_spec(source, "question")
i, lines = begin_question_line + 1, []
while source[i].strip() != BLOCK_QUOTE:
lines.append(source[i])
i = i + 1
metadata = yaml.full_load('\n'.join(lines))
assert ALLOWED_NAME.match(metadata.get('name', '')), metadata
return metadata