-
Notifications
You must be signed in to change notification settings - Fork 42
/
proc_notebooks.py
198 lines (151 loc) · 5.14 KB
/
proc_notebooks.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import sys, shutil
import pathlib
import os
import re
import nbformat
# Update and copy Jupyter notebooks to be built as static pages by nbsphinx
def is_target_simplelife(node):
return (node['cell_type'] == 'code'
and re.match("^import simplelife", node['source']))
def proc_simplelife(node):
node['metadata']['nbsphinx'] = 'hidden'
src = re.sub(
"import simplelife",
"import lifelib.projects.simplelife.simplelife as simplelife",
node['source']
)
node['source'] = src
def is_target_ifrs17sim(node):
return (node['cell_type'] == 'code'
and re.match("^%matplotlib notebook", node['source']))
def proc_ifrs17sim(node):
node['metadata']['nbsphinx'] = 'hidden'
src = re.sub(
"^%matplotlib notebook",
"%matplotlib inline",
node['source']
)
src = re.sub(
"import ifrs17sim",
"import lifelib.projects.ifrs17sim.ifrs17sim as ifrs17sim",
src
)
src = re.sub(
"from draw_charts import draw_waterfall",
"from lifelib.projects.ifrs17sim.draw_charts import draw_waterfall",
src
)
node['source'] = src
entries = [
{"dir": "libraries",
"project": "savings",
"notebook": "savings_example3.ipynb",
"is_target": lambda node: True,
"proc": lambda node: None},
{"dir": "libraries",
"project": "savings",
"notebook": "generate_100K_model_points.ipynb",
"is_target": lambda node: True,
"proc": lambda node: None},
{"dir": "libraries",
"project": "savings",
"notebook": "savings_example2.ipynb",
"is_target": lambda node: True,
"proc": lambda node: None},
{"dir": "libraries",
"project": "savings",
"notebook": "savings_example1.ipynb",
"is_target": lambda node: True,
"proc": lambda node: None},
{"dir": "libraries",
"project": "basiclife",
"notebook": "generate_model_points.ipynb",
"is_target": lambda node: True,
"proc": lambda node: None},
{"dir": "libraries",
"project": "basiclife",
"notebook": "generate_model_points_with_duration.ipynb",
"is_target": lambda node: True,
"proc": lambda node: None},
{"dir": "libraries",
"project": "basiclife",
"notebook": "create_premium_table.ipynb",
"is_target": lambda node: True,
"proc": lambda node: None},
{"dir": "projects",
"project": "fastlife",
"notebook": "fastlife-introduction.ipynb",
"is_target": lambda node: True,
"proc": lambda node: None},
{"dir": "projects",
"project": "simplelife",
"notebook": "simplelife-space-overview.ipynb",
"is_target": is_target_simplelife,
"proc": lambda node: None},
{"dir": "projects",
"project": "ifrs17sim",
"notebook": "ifrs17sim_csm_waterfall.ipynb",
"is_target": is_target_ifrs17sim,
"proc": proc_ifrs17sim},
{"dir": "projects",
"project": "ifrs17sim",
"notebook": "ifrs17sim_charts_baseline.ipynb",
"is_target": is_target_ifrs17sim,
"proc": proc_ifrs17sim},
{"dir": "projects",
"project": "ifrs17sim",
"notebook": "ifrs17sim_charts_lapsescen.ipynb",
"is_target": is_target_ifrs17sim,
"proc": proc_ifrs17sim},
{"dir": "projects",
"project": "smithwilson",
"notebook": "smithwilson-overview.ipynb",
"is_target": lambda node: True,
"proc": lambda node: None}
]
def adjust_notebook(file, is_target, proc):
nb = nbformat.read(file, as_version=nbformat.NO_CONVERT)
try:
node = next(c for c in nb.cells if is_target(c))
proc(node)
except StopIteration:
pass
return nb
thisdir = pathlib.Path(os.path.abspath(os.path.dirname(__file__))).as_posix()
def _get_dirs(project, dir_):
return (thisdir + '/../lifelib/'+ dir_ + '/' + project,
thisdir + '/source/' + dir_ + '/notebooks/' + project)
def prepare_notebooks():
for entry in entries:
entry = entry.copy() # To not delete global entries directory
project = entry.pop("project")
notebook = entry.pop("notebook")
dir_ = entry.pop("dir")
srcdir, trgdir = _get_dirs(project, dir_)
nb = adjust_notebook(srcdir + '/' + notebook, **entry)
if not os.path.exists(trgdir):
pathlib.Path(trgdir).mkdir(parents=True)
nbformat.write(nb, trgdir + '/' + notebook)
def prepare_models():
for entry in entries:
project = entry["project"]
dir_ = entry["dir"]
srcdir, trgdir = _get_dirs(project, dir_)
srcmodel = srcdir + "/" + "model"
trgmodel = trgdir + "/" + "model"
if os.path.exists(srcmodel) and os.path.isdir(srcmodel) and not os.path.exists(trgmodel):
shutil.copytree(srcmodel, trgdir + "/" + "model")
def remove_notebooks():
for nbdir in (thisdir + "/source/libraries/notebooks",
thisdir + "/source/projects/notebooks"):
if os.path.exists(nbdir):
shutil.rmtree(nbdir)
if __name__ == "__main__":
arg = sys.argv[1]
if arg == "prepare":
prepare_notebooks()
prepare_models()
elif arg == "remove":
remove_notebooks()
else:
raise KeyError