forked from rougier/numpy-100
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generators.py
137 lines (99 loc) · 4.29 KB
/
generators.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
129
130
131
132
133
134
135
136
137
import os
import nbformat as nbf
import mdutils
def ktx_to_dict(input_file, keystarter='<'):
""" parsing keyed text to a python dictionary. """
answer = dict()
with open(input_file, 'r+', encoding='utf-8') as f:
lines = f.readlines()
k, val = '', ''
for line in lines:
if line.startswith(keystarter):
k = line.replace(keystarter, '').strip()
val = ''
else:
val += line
if k:
answer.update({k: val.strip()})
return answer
def dict_to_ktx(input_dict, output_file, keystarter='<'):
""" Store a python dictionary to a keyed text"""
with open(output_file, 'w+') as f:
for k, val in input_dict.items():
f.write(f'{keystarter} {k}\n')
f.write(f'{val}\n\n')
HEADERS = ktx_to_dict(os.path.join('source', 'headers.ktx'))
QHA = ktx_to_dict(os.path.join('source', 'exercises100.ktx'))
def create_jupyter_notebook(destination_filename='100_Numpy_exercises.ipynb'):
""" Programmatically create jupyter notebook with the questions (and hints and solutions if required)
saved under source files """
# Create cells sequence
nb = nbf.v4.new_notebook()
nb['cells'] = []
# - Add header:
nb['cells'].append(nbf.v4.new_markdown_cell(HEADERS["header"]))
nb['cells'].append(nbf.v4.new_markdown_cell(HEADERS["sub_header"]))
nb['cells'].append(nbf.v4.new_markdown_cell(HEADERS["jupyter_instruction"]))
# - Add initialisation
nb['cells'].append(nbf.v4.new_code_cell('%run initialise.py'))
# - Add questions and empty spaces for answers
for n in range(1, 101):
nb['cells'].append(nbf.v4.new_markdown_cell(f'#### {n}. ' + QHA[f'q{n}']))
nb['cells'].append(nbf.v4.new_code_cell(""))
# Delete file if one with the same name is found
if os.path.exists(destination_filename):
os.remove(destination_filename)
# Write sequence to file
nbf.write(nb, destination_filename)
def create_jupyter_notebook_random_question(destination_filename='100_Numpy_random.ipynb'):
""" Programmatically create jupyter notebook with the questions (and hints and solutions if required)
saved under source files """
# Create cells sequence
nb = nbf.v4.new_notebook()
nb['cells'] = []
# - Add header:
nb['cells'].append(nbf.v4.new_markdown_cell(HEADERS["header"]))
nb['cells'].append(nbf.v4.new_markdown_cell(HEADERS["sub_header"]))
nb['cells'].append(nbf.v4.new_markdown_cell(HEADERS["jupyter_instruction_rand"]))
# - Add initialisation
nb['cells'].append(nbf.v4.new_code_cell('%run initialise.py'))
nb['cells'].append(nbf.v4.new_code_cell("pick()"))
# Delete file if one with the same name is found
if os.path.exists(destination_filename):
os.remove(destination_filename)
# Write sequence to file
nbf.write(nb, destination_filename)
def create_markdown(destination_filename='100_Numpy_exercises', with_hints=False, with_solutions=False):
# Create file name
if with_hints:
destination_filename += '_with_hints'
if with_solutions:
destination_filename += '_with_solutions'
# Initialise file
mdfile = mdutils.MdUtils(file_name=destination_filename)
# Add headers
mdfile.write(HEADERS["header"] + '\n')
mdfile.write(HEADERS["sub_header"] + '\n')
# Add questions (and hint or answers if required)
for n in range(1, 101):
mdfile.new_header(title=f"{n}. {QHA[f'q{n}']}", level=4)
if with_hints:
mdfile.write(f"`{QHA[f'h{n}']}`")
if with_solutions:
mdfile.insert_code(QHA[f'a{n}'], language='python')
# Delete file if one with the same name is found
if os.path.exists(destination_filename):
os.remove(destination_filename)
# Write sequence to file
mdfile.create_md_file()
def create_rst(destination_filename, with_ints=False, with_answers=False):
# TODO: use rstdoc python library.
# also see possible integrations with https://github.com/rougier/numpy-100/pull/38
pass
if __name__ == '__main__':
create_jupyter_notebook()
create_jupyter_notebook_random_question()
create_markdown()
create_markdown(with_hints=False, with_solutions=True)
create_markdown(with_hints=True, with_solutions=False)
create_markdown(with_hints=True, with_solutions=True)