-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
56 lines (45 loc) · 1.38 KB
/
build.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
#!/usr/bin/env python3
"""Build exercise notebook from answer notebook by removing answers"""
import argparse
import json
def censor_nb(nb):
"""Censor a json object of notebook nb
Args:
nb: notebook parsed as a python object
Returns:
censors python object nb in-place
"""
for cell in nb["cells"]:
if cell["cell_type"] != "code":
continue
cell["execution_count"] = 0
cell["outputs"] = []
censored_source = []
censor = False
for line in cell["source"]:
if "START" in line:
censor = True
if not censor:
censored_source.append(line)
if "END" in line:
censor = False
cell["source"] = censored_source
if __name__ == "__main__":
# Set up the argument parser
parser = argparse.ArgumentParser(
description="Create exercise version of notebooks"
)
parser.add_argument("--source",
"-s",
help="source (answer) notebook",
required=True)
flags = parser.parse_args()
with open(flags.source, "r") as f:
nb = json.load(f)
censor_nb(nb)
if "answers" in flags.source:
target = flags.source.replace("answers", "exercises")
else:
target = flags.source.replace(".ipynb", "-exercises.ipynb")
with open(target, "w") as f:
json.dump(nb, f)