forked from jimmysong/learnhd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_jupyter.py
141 lines (125 loc) · 5.81 KB
/
generate_jupyter.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
import nbformat
import re
import sys
sessions = ['']
FIRST_CELL = '''############## PLEASE RUN THIS CELL FIRST! ###################
# import everything and define a test runner function
from importlib import reload
from helper import run
'''
UNITTEST_TEMPLATE_1 = '''### Exercise {exercise_number}
{instructions}
#### Make [this test](/edit/{path}/{module}.py) pass: `{module}.py:{test_suite}:{test}`'''
UNITTEST_TEMPLATE_2 = '''# Exercise {exercise_number}
reload({module})
run({module}.{test_suite}('{test}'))'''
EXERCISE_TEMPLATE_1 = '''### Exercise {exercise_number}
{instructions}'''
EXERCISE_TEMPLATE_2 = '''# Exercise {exercise_number}
{code}'''
for session in sessions:
notebook = nbformat.v4.new_notebook()
notebook_complete = nbformat.v4.new_notebook()
cells = notebook['cells']
cells_complete = notebook_complete['cells']
path = 'session{}'.format(session)
with open('{}/answers.py'.format(path), 'r') as f:
current = ''
cell_type = None
exercise_number = 1
for l in f:
line = l.strip()
if line in ('#markdown', '#code', '#exercise', '#unittest'):
cell_type = line[1:]
elif line == '#end{}'.format(cell_type):
if cell_type == 'markdown':
markdown_cell = nbformat.v4.new_markdown_cell(current)
markdown_cell.metadata['slideshow'] = dict(slide_type='slide')
cells.append(markdown_cell)
cells_complete.append(markdown_cell)
elif cell_type == 'code':
# only take >>> or ... lines
lines = []
for line in current.split('\n'):
if line.startswith('>>> ') or line.startswith('... '):
line = re.sub(r'\\\\x', r'\\x', line)
lines.append(line[4:])
if lines[0].startswith('import '):
code = FIRST_CELL + '\n'.join(lines)
slide_type = 'skip'
else:
code = '\n'.join(lines)
slide_type = 'slide'
code_cell = nbformat.v4.new_code_cell(code)
code_cell.metadata['slideshow'] = dict(slide_type=slide_type)
cells.append(code_cell)
cells_complete.append(code_cell)
elif cell_type == 'exercise':
instructions, code = current.split('---')
markdown = EXERCISE_TEMPLATE_1.format(
exercise_number=exercise_number,
instructions=instructions,
)
markdown_cell = nbformat.v4.new_markdown_cell(markdown)
markdown_cell.metadata['slideshow'] = dict(slide_type='slide')
cells.append(markdown_cell)
cells_complete.append(markdown_cell)
lines = []
lines_complete = []
for line in code.split('\n'):
if line.startswith('>>> ') or line.startswith('... '):
line = re.sub(r'\\\\x', r'\\x', line)
start_alt = line.find(' #/')
if start_alt == -1:
lines.append(line[4:])
lines_complete.append(line[4:])
else:
lines.append(line[start_alt+4:])
lines_complete.append(line[4:start_alt])
code_1 = EXERCISE_TEMPLATE_2.format(
code='\n'.join(lines),
exercise_number=exercise_number,
)
code_cell_1 = nbformat.v4.new_code_cell(code_1)
cells.append(code_cell_1)
code_2 = EXERCISE_TEMPLATE_2.format(
code='\n'.join(lines_complete),
exercise_number=exercise_number,
)
code_cell_2 = nbformat.v4.new_code_cell(code_2)
cells_complete.append(code_cell_2)
exercise_number += 1
elif cell_type == 'unittest':
module, test_suite, test, instructions = current.split(':', 3)
markdown = UNITTEST_TEMPLATE_1.format(
exercise_number=exercise_number,
instructions=instructions,
module=module,
path=path,
test=test,
test_suite=test_suite,
)
markdown_cell = nbformat.v4.new_markdown_cell(markdown)
markdown_cell.metadata['slideshow'] = dict(slide_type='slide')
cells.append(markdown_cell)
cells_complete.append(markdown_cell)
code = UNITTEST_TEMPLATE_2.format(
exercise_number=exercise_number,
module=module,
path=path,
test=test,
test_suite=test_suite,
)
code_cell = nbformat.v4.new_code_cell(code)
cells.append(code_cell)
cells_complete.append(code_cell)
exercise_number += 1
current = ''
cell_type = None
elif cell_type:
if cell_type == 'markdown':
current += l
else:
current += line + '\n'
nbformat.write(notebook, '{}/session{}.ipynb'.format(path, session))
nbformat.write(notebook_complete, '{}/complete/session{}.ipynb'.format(path, session))