-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_statements.py
37 lines (28 loc) · 1.19 KB
/
extract_statements.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
import re
import json
def load_code_blocks(filepath):
""" Extracts all the code blocks. Each .ipynb file has a JSON key called
`cells` residing in the second dimension and containing another key
called `source` which contains a list of lists of code."""
sourcecode = []
with open(filepath, "r") as f:
content = json.load(f)
for item in content[0]["cells"]:
sourcecode.append(item["source"])
return sourcecode
def write_source_file(filepath, sourcefile, sourcecode):
"""Goes through each list and each part of the list if they are lists
themselves to get the source code."""
with open(sourcefile, "w") as f:
for line in sourcecode:
if isinstance(line, list):
for l in line: # remove markdown code block formating
if l.startswith("```"):
print("#", l.rstrip('\r\n'), file=f)
else:
print(l.rstrip('\r\n'), file=f)
else:
if line.startswith("```"):
print("#", l.rstrip('\r\n'), file=f)
else:
print(line.rstrip('\r\n'), file=f)