-
Notifications
You must be signed in to change notification settings - Fork 1
/
grading_tools.py
220 lines (180 loc) · 7.72 KB
/
grading_tools.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
""" Helper functions to do most common testings.
A tester function should return a dictionary or a list of dictionaries, each one
representing the output of one test case and with one of the
following formats:
{'correct': (bool), 'function': (str), 'result': (str), 'expected': (str)}
{'correct': False, 'error': (str)}
"""
import logging
import sys
import traceback
def function(fct=None, values=None, solution=None, expected=None):
"""Tests a function against a solution function or solution list.
Arguments:
fct (function): function to be tested
values (list): list of test values.
Either like [(0,1), (2,4), ...] or [3, 4, 6, 9, ...]
In the former case we call `fct(*val)` for each
element, in the latter we call `fct(val)`
each `val` in this list.
solution (function): function that returns the correct results
when called `solution(val)`
expected (list): List of expected output values.
Note:
You must specify either `solution` or `expected`.
Returns:
details (list): List of test results.
"""
if solution is None and expected is None:
logging.error('In `grading_tools.function` you must specify '
"one of 'solution' and 'expected'!")
raise ValueError('Expected exactly one of the kwargs '
"'solution' or 'expected'.")
elif solution is not None and expected is not None:
logging.warning('In `grading_tools.function` you must only specify '
"one of 'solution' and 'expected'! "
"Ignoring 'expected'.")
details = []
for i, val in enumerate(values):
# FIXME: This does certainly not work with all types of
# iterable inputs.
try:
iter(val)
except TypeError:
val = list(val)
else:
if isinstance(val, str):
val = [val]
out = dict()
# Create test case info text
out['function'] = '{}({})'.format(fct.__name__, ', '.join(
"'{}'".format(v) if isinstance(v, str) else str(v) for v in val))
# Get correct solution
if solution is not None:
out['expected'] = solution(*val)
else:
out['expected'] = expected[i]
# Get student solution
try:
out['result'] = fct(*val)
except Exception as err: # pylint: disable=broad-except
out['result'] = '{}: {}'.format(type(err).__name__, str(err))
out['correct'] = False
else:
out['correct'] = bool(out['result'] == out['expected'])
details.append(out)
return details
def userinput(code, values, solution=None, expected=None):
"""Tests usercode with `input` and `print` against a solution.
This is for student code that gets values with a single
`input` statement and returns the result with print.
Arguments:
code (str): student code as string.
values (list): list of test values, like [val1, val2, val3, ...]
When `input()` is called, it will return `str(val1)`.
solution (function): function that returns the correct results
when called `solution(val1)`
expected (list): List of expected output values.
Note:
You must specify either `solution` or `expected`.
Returns:
details (list): List of test results.
"""
if solution is None and expected is None:
logging.error('In `grading_tools.function` you must specify '
"one of 'solution' and 'expected'!")
raise ValueError('Expected exactly one of the kwargs '
"'solution' or 'expected'.")
elif solution is not None and expected is not None:
logging.warning('In `grading_tools.function` you must only specify '
"one of 'solution' and 'expected'! "
"Ignoring 'expected'.")
# overwrite builtin input function.
class Input(): # pylint: disable=too-few-public-methods
"""Overwriting standard input function."""
def __init__(self, value):
self.value = str(value)
def __call__(self, msg=''):
# calling `input()` inside the usercode will return `str(value)`.
return self.value
details = []
for i, val in enumerate(values):
# pylint: disable=redefined-builtin, possibly-unused-variable
input = Input(val)
# Create test case info text
out = dict()
out['function'] = 'Testing with input: {}'.format(val)
# Get correct solution
if solution is not None:
out['expected'] = solution(val)
else:
out['expected'] = expected[i]
# evaluate the user code.
try:
sys.stdout.truncate(0)
exec(code, locals()) # pylint: disable=exec-used
# Note: sys.stdout will be redirected to a StringIO
# pylint: disable=no-member
out['result'] = sys.stdout.getvalue().strip('\u0000\n')
except Exception: # pylint: disable=broad-except
out['error'] = traceback.format_exc(limit=0)
out['correct'] = False
else:
out['correct'] = bool(out['result'] == out['expected'])
details.append(out)
return details
def userinput_generator(code, generators, solutions=None, expected=None):
"""Tests usercode with `input` and `print` against a solution.
This is for student code that gets values with a single
`input` statement and returns the result with print.
Arguments:
code (str): student code as string.
generators (list):
solution (function): function that returns the correct results
when called `solution(val1)`
expected (list): List of expected output values.
Note:
You must specify either `solution` or `expected`.
Returns:
details (list): List of test results.
"""
if generators is None:
raise ValueError('Expected exactly one of the kwargs '
"'solution' or 'expected'.")
# overwrite builtin input function.
class Input(): # pylint: disable=too-few-public-methods
"""Overwriting standard input function.
Raises:
StopIteration: If generator runs out
"""
def __init__(self, gen):
self.gen = gen
def __call__(self, msg=''):
# calling `input()` inside the usercode will return `str(value)`.
return next(self.gen)
details = []
for i, gen in enumerate(generators):
# pylint: disable=redefined-builtin, possibly-unused-variable
input = Input(gen)
# Create test case info text
out = dict()
out['function'] = ''
# Get correct solution
if solution is not None:
out['expected'] = solution(gen)
else:
out['expected'] = expected[i]
# evaluate the user code.
try:
sys.stdout.truncate(0)
exec(code, locals()) # pylint: disable=exec-used
# Note: sys.stdout will be redirected to a StringIO
# pylint: disable=no-member
out['result'] = sys.stdout.getvalue().strip('\u0000\n')
except Exception: # pylint: disable=broad-except
out['error'] = traceback.format_exc(limit=0)
out['correct'] = False
else:
out['correct'] = bool(out['result'] == out['expected'])
details.append(out)
return details