-
Notifications
You must be signed in to change notification settings - Fork 661
/
generate_jupyter.py
128 lines (111 loc) · 4.37 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
import nbformat
import re
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 {num}
{exercise}
#### Make [this test](/edit/{path}/{module}.py) pass: `{module}.py:{test_suite}:{test}`'''
UNITTEST_TEMPLATE_2 = '''# Exercise {num}
reload({module})
run({module}.{test_suite}("{test}"))'''
PRACTICE_TEMPLATE_1 = '''### Exercise {num}
{exercise}'''
PRACTICE_TEMPLATE_2 = '''# Exercise {num}
{hints}'''
for chapter in range(1, 13):
notebook = nbformat.v4.new_notebook()
if chapter < 10:
path = 'code-ch0{}'.format(chapter)
else:
path = 'code-ch{}'.format(chapter)
with open('{}/examples.py'.format(path), 'r') as f:
examples = {}
current = ''
current_key = None
capture = False
for line in f:
if line.startswith('>>>') or line.startswith('...'):
if line.endswith('\\\n'):
current += line[4:-2]
capture = True
else:
current += line[4:]
elif capture:
if line.endswith('\\\n'):
current += line[:-2]
capture = True
else:
current += line
capture = False
elif line.startswith('# tag::example'):
index = line.rfind('[')
current_key = line[7:index]
elif line.startswith('# end::example'):
raw = current
raw = re.sub(r' \# \<[0-9]+\>', r'', raw)
examples[current_key] = raw
current = ''
current_key = None
with open('{}/answers.py'.format(path), 'r') as f:
exercises = {}
current = ''
current_key = None
capture = False
for l in f:
line = l.lstrip(' ')
if line.startswith('# tag::exercise'):
index = line.rfind('[')
current_key = line[7:index]
elif line.startswith('# end::exercise'):
raw = current.strip()
raw = raw[raw.find('\n\n')+1:]
raw = re.sub(r'([a-zA-Z+-])~(.+?)~', r'\\\\(\1_{\2}\\\\)', raw)
raw = re.sub(r'([a-zA-Z0-9()\-+]+)\^(.+?)\^', r'\\\\(\1^{\2}\\\\)', raw)
exercises[current_key] = raw
current = ''
current_key = None
elif current_key is not None:
if line.endswith('\\\n'):
current += line[:-2]
else:
current += line
with open('{}/jupyter.txt'.format(path), 'r') as f:
raw_cells = f.read().split('---\n')
cells = notebook['cells']
# first cell is always added with this:
cells.append(nbformat.v4.new_code_cell(FIRST_CELL + raw_cells[0].strip()))
for raw_cell in raw_cells[1:]:
if raw_cell.startswith('exercise'):
components = raw_cell.split(':')
key = components[0]
if len(components) == 4:
template_dict = {
'path': path,
'num': key[8:],
'module': components[1],
'test_suite': components[2],
'test': components[3].strip(),
'exercise': exercises[key].strip(),
}
contents_1 = UNITTEST_TEMPLATE_1.format(**template_dict)
contents_2 = UNITTEST_TEMPLATE_2.format(**template_dict)
else:
hints = ':'.join(components[1:]).strip()
template_dict = {
'num': key[8:],
'exercise': exercises[key],
'hints': hints,
}
contents_1 = PRACTICE_TEMPLATE_1.format(**template_dict)
contents_2 = PRACTICE_TEMPLATE_2.format(**template_dict)
cells.append(nbformat.v4.new_markdown_cell(contents_1))
cells.append(nbformat.v4.new_code_cell(contents_2))
elif raw_cell.startswith('example'):
key = raw_cell.strip()
cells.append(nbformat.v4.new_code_cell(examples[key].strip()))
else:
raise RuntimeError
nbformat.write(notebook, '{}/Chapter{}.ipynb'.format(path, chapter))