forked from hcientist/OnlinePythonTutor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
doctest_reader.py
42 lines (35 loc) · 1.11 KB
/
doctest_reader.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
'''
Input:
- sys.argv[1] - filename of a doctest pickle file like lab1_doctests.pickle
Output:
- a JSON blob representing its contents, printed to stdout
'''
import pickle
import sys
import json
import traceback
def encode_doctest(t):
encoded_test = {}
encoded_test['docstring'] = t.docstring
encoded_test['name'] = t.name
encoded_test['filename'] = t.filename
encoded_examples = []
encoded_test['examples'] = encoded_examples
for e in t.examples:
encoded_examples.append(
dict(source=e.source,
want=e.want,
options=e.options,
exc_msg=e.exc_msg))
return encoded_test
if __name__ == "__main__":
try:
fullpath = sys.argv[1]
assert fullpath.endswith(".pickle")
with open(fullpath, 'rU') as f: # use 'U' to work on Windows
tests = pickle.load(f)
all_encoded_tests = [encode_doctest(t) for t in tests]
print(json.dumps(all_encoded_tests, indent=2))
except:
print(json.dumps("ERROR in doctest_reader.py")) # print JSON to stdout
traceback.print_exc()