forked from cgpotts/cs224u
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremove_solutions.py
40 lines (34 loc) · 1.21 KB
/
remove_solutions.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
"""Removes the answers from homework notebooks.
"""
import json
import re
import sys
__author__ = 'Chris Potts'
def main(src_filename):
output_filename = src_filename.replace('_solved', '')
doc = None
with open(src_filename, 'rt') as f:
doc = json.load(f)
new_cells = []
for i, cell in enumerate(doc['cells']):
if cell['cell_type'] == 'code':
cell_start = cell['source'][0]
if not re.search(r"^#+\s*SOLUTION", cell_start):
removing = False
new_source = []
for line in cell['source']:
if "# <<<<<<<<<< TO BE COMPLETED" in line:
removing = True
if not removing:
new_source.append(line)
if "# >>>>>>>>>>" in line:
removing = False
cell['source'] = new_source
new_cells.append(cell)
else:
new_cells.append(cell)
doc['cells'] = new_cells
with open(output_filename, 'wt') as output:
json.dump(doc, output)
if __name__ == '__main__':
main(sys.argv[1])