Skip to content

Commit 8a9871a

Browse files
committed
move autograder helper code into its own file
1 parent cdb2edd commit 8a9871a

3 files changed

Lines changed: 37 additions & 67 deletions

File tree

extras/__init__.py

Whitespace-only changes.

extras/scripts/hw_0_helper.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import ast
2+
import os
3+
import pytest
4+
from .nb_helper import *
5+
6+
7+
class InputChecker(ast.NodeVisitor):
8+
def __init__(self):
9+
self.num_inputs = 0
10+
11+
def visit_Call(self, node):
12+
if isinstance(node.func, ast.Name) and node.func.id == "input":
13+
self.num_inputs += 1
14+
15+
16+
@pytest.fixture()
17+
def notebook():
18+
nb_full_path = os.path.join(os.getcwd(), "hw_0.ipynb")
19+
return read_notebook(nb_full_path)
20+
21+
22+
@pytest.fixture()
23+
def script(notebook):
24+
return notebook_to_script(notebook)
25+
26+
27+
@pytest.fixture()
28+
def tree(script):
29+
return ast.parse(script)

hw_0.ipynb

Lines changed: 8 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -196,58 +196,18 @@
196196
{
197197
"cell_type": "code",
198198
"execution_count": 1,
199-
"metadata": {},
200-
"outputs": [
201-
{
202-
"data": {
203-
"application/javascript": [
204-
"IPython.notebook.save_checkpoint();\n",
205-
"IPython.notebook.kernel.execute('nb_name = \"' + IPython.notebook.notebook_name + '\"')\n"
206-
],
207-
"text/plain": [
208-
"<IPython.core.display.Javascript object>"
209-
]
210-
},
211-
"metadata": {},
212-
"output_type": "display_data"
213-
}
214-
],
215-
"source": [
216-
"%%javascript\n",
217-
"IPython.notebook.save_checkpoint();\n",
218-
"IPython.notebook.kernel.execute('nb_name = \"' + IPython.notebook.notebook_name + '\"')"
219-
]
220-
},
221-
{
222-
"cell_type": "code",
223-
"execution_count": 2,
224199
"metadata": {
225200
"scrolled": true
226201
},
227202
"outputs": [],
228203
"source": [
229-
"import ast\n",
230204
"import ipytest\n",
231-
"import nbconvert\n",
232-
"import nbformat\n",
233-
"import os\n",
234-
"\n",
235-
"\n",
236-
"ipytest.autoconfig()\n",
237-
"\n",
238-
"\n",
239-
"class InputChecker(ast.NodeVisitor):\n",
240-
" def __init__(self):\n",
241-
" self.num_inputs = 0\n",
242-
"\n",
243-
" def visit_Call(self, node):\n",
244-
" if isinstance(node.func, ast.Name) and node.func.id == \"input\":\n",
245-
" self.num_inputs += 1"
205+
"ipytest.autoconfig()"
246206
]
247207
},
248208
{
249209
"cell_type": "code",
250-
"execution_count": 11,
210+
"execution_count": 3,
251211
"metadata": {},
252212
"outputs": [
253213
{
@@ -258,7 +218,7 @@
258218
"============================================= FAILURES =============================================\n",
259219
"\u001b[31m\u001b[1m_________________________________________ test_num_inputs __________________________________________\u001b[0m\n",
260220
"\n",
261-
"tree = <ast.Module object at 0x10d086dd0>\n",
221+
"tree = <ast.Module object at 0x109a5a860>\n",
262222
"\n",
263223
" \u001b[94mdef\u001b[39;49;00m \u001b[92mtest_num_inputs\u001b[39;49;00m(tree):\n",
264224
" checker = InputChecker()\n",
@@ -267,43 +227,24 @@
267227
"> \u001b[94massert\u001b[39;49;00m checker.num_inputs >= \u001b[94m8\u001b[39;49;00m, \u001b[33m\"\u001b[39;49;00m\u001b[33myou don\u001b[39;49;00m\u001b[33m'\u001b[39;49;00m\u001b[33mt have enough calls to input()\u001b[39;49;00m\u001b[33m\"\u001b[39;49;00m\n",
268228
"\u001b[1m\u001b[31mE AssertionError: you don't have enough calls to input()\u001b[0m\n",
269229
"\u001b[1m\u001b[31mE assert 2 >= 8\u001b[0m\n",
270-
"\u001b[1m\u001b[31mE + where 2 = <__main__.InputChecker object at 0x10d084fa0>.num_inputs\u001b[0m\n",
230+
"\u001b[1m\u001b[31mE + where 2 = <extras.scripts.hw_0_helper.InputChecker object at 0x10acc9ba0>.num_inputs\u001b[0m\n",
271231
"\n",
272-
"\u001b[1m\u001b[31m/var/folders/kg/1ys0dccx4237f5wsd_w10dt80000gn/T/ipykernel_65587/682324548.py\u001b[0m:27: AssertionError\n",
232+
"\u001b[1m\u001b[31m/var/folders/kg/1ys0dccx4237f5wsd_w10dt80000gn/T/ipykernel_67029/2901258849.py\u001b[0m:8: AssertionError\n",
273233
"===================================== short test summary info ======================================\n",
274-
"FAILED tmpciht1t3f.py::test_num_inputs - AssertionError: you don't have enough calls to input()\n"
234+
"FAILED tmpyoyk9z3m.py::test_num_inputs - AssertionError: you don't have enough calls to input()\n"
275235
]
276236
}
277237
],
278238
"source": [
279239
"%%ipytest -qq\n",
280240
"\n",
281-
"import pytest\n",
282-
"\n",
283-
"\n",
284-
"\n",
285-
"@pytest.fixture()\n",
286-
"def notebook():\n",
287-
" nb_full_path = os.path.join(os.getcwd(), nb_name)\n",
288-
" return nbformat.read(nb_full_path, as_version=4)\n",
289-
"\n",
290-
"\n",
291-
"@pytest.fixture()\n",
292-
"def script(notebook):\n",
293-
" exporter = nbconvert.exporters.PythonExporter\n",
294-
" script, _ = nbconvert.exporters.export(exporter, notebook)\n",
295-
" return script\n",
296-
"\n",
297-
"\n",
298-
"@pytest.fixture()\n",
299-
"def tree(script):\n",
300-
" return ast.parse(script)\n",
241+
"from extras.scripts.hw_0_helper import *\n",
301242
"\n",
302243
"\n",
303244
"def test_num_inputs(tree):\n",
304245
" checker = InputChecker()\n",
305246
" checker.visit(tree)\n",
306-
" \n",
247+
"\n",
307248
" assert checker.num_inputs >= 8, \"you don't have enough calls to input()\""
308249
]
309250
},

0 commit comments

Comments
 (0)