Repository navigation
Expand file tree
/
Copy pathjntools.py
More file actions
404 lines (318 loc) · 12.6 KB
/
Copy pathjntools.py
File metadata and controls
404 lines (318 loc) · 12.6 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import ast
import sys
import astor
from pathlib import Path
from typing import List
import nbformat as nbf
import typer
import shelve
app = typer.Typer()
@app.command()
def replace_pattern(filenames: List[Path]):
""" Processes a list of file paths and applies pattern replacement based on the file type.
For Jupyter Notebook files (.ipynb), it calls the
`replace_pattern_in_notebook` function. For Python module files (.py),
it calls the `replace_pattern_in_module` function.
Args:
filenames (List[Path]): A list of file paths to process. Each file
should have a suffix of either '.ipynb' or '.py'.
"""
for filename in filenames:
if filename.suffix == ".ipynb":
replace_pattern_in_notebook(filename)
elif filename.suffix == ".py":
replace_pattern_in_module(filename)
def replace_pattern_in_notebook(filename):
""" Processes a Jupyter Notebook file and applies pattern replacement.
Args:
filename (Path): The path to the Jupyter Notebook file to process.
"""
notebook = nbf.read(filename, nbf.NO_CONVERT)
for cell in notebook.cells:
if cell["cell_type"] == "code":
source = cell["source"]
cell.source = replace_pattern_in_source(source)
elif cell["cell_type"] == "markdown":
source = cell["source"]
cell.source = replace_pattern_in_markdown(source)
nbf.write(notebook, filename)
def replace_pattern_in_module(filename):
""" Processes a Python module file and applies pattern replacement.
Args:
filename (Path): The path to the Python module file to process.
"""
source = filename.read_text()
source = replace_pattern_in_source(source)
filename.write_text(source)
def replace_pattern_in_source(source):
return source
def replace_pattern_in_markdown(source):
""" Processes a markdown cell in a Jupyter Notebook and applies pattern replacement.
Args:
source (str): The source code of the markdown cell.
Returns:
str: The modified source code with patterns replaced.
"""
pattern = r"(?<!`)(Series|DataFrame|Index)(?!`)"
replacement = r"`\1`"
source = re.sub(pattern, replacement, source)
pattern = r"(?<!`)(Hist|Pmf|Cdf|Surv|Hazard)(?!`)"
replacement = r"`\1`"
source = re.sub(pattern, replacement, source)
pattern = r"(?<!`)(Normal|NormalPdf|EstimatedPdf|HypothesisTest)(?!`)"
replacement = r"`\1`"
source = re.sub(pattern, replacement, source)
pattern = r"pandas"
replacement = r"Pandas"
source = re.sub(pattern, replacement, source)
return source
def put_each_sentence_on_a_new_line(source):
""" Processes a string and places each sentence on a new line.
Args:
source (str): The source string to process.
Returns:
str: The modified string with each sentence on a new line.
"""
# replace a single newline with a space
pattern = r"(?<!\n)\n(?!\n)"
replacement = r" "
source = re.sub(pattern, replacement, source)
# replace four spaces with one
pattern = r" "
replacement = r" "
source = re.sub(pattern, replacement, source)
# split between sentences
pattern = r"(?<=[^\s.]{3})\.[ ]+"
replacement = r".\n"
source = re.sub(pattern, replacement, source)
return source
@app.command()
def format_sentences(filenames: List[Path]):
for filename in filenames:
format_sentences_in_notebook(filename)
def format_sentences_in_notebook(filename):
notebook = nbf.read(filename, nbf.NO_CONVERT)
for cell in notebook.cells:
if cell["cell_type"] == "markdown":
cell.source = put_each_sentence_on_a_new_line(cell.source)
nbf.write(notebook, filename)
@app.command()
def replace_functions(filenames: List[Path]):
with shelve.open("function_names") as db:
function_names = db["function_names"]
for filename in filenames:
if filename.suffix == ".ipynb":
replace_functions_in_notebook(filename, function_names)
elif filename.suffix == ".py":
replace_functions_in_module(filename, function_names)
def replace_functions_in_notebook(filename, function_names):
notebook = nbf.read(filename, nbf.NO_CONVERT)
for cell in notebook.cells:
if cell["cell_type"] == "code":
source = cell["source"]
modified_source = replace_functions_in_source(source, function_names)
cell.source = modified_source.rstrip()
nbf.write(notebook, filename)
def replace_functions_in_module(filename, function_names):
source = filename.read_text()
source = replace_functions_in_source(source, function_names)
filename.write_text(source)
def replace_functions_in_source(source, function_names):
class_names = [
"Cdf",
"Pmf",
"Hist",
"Pdf",
"HazardFunction",
"SurvivalFunction",
"HypothesisTest",
]
class FunctionNameReplacer(ast.NodeTransformer):
def visit_FunctionDef(self, node):
if node.name in function_names:
node.name = camel_to_snake(node.name)
self.generic_visit(node)
return node
def visit_Call(self, node):
if isinstance(node.func, ast.Attribute) and isinstance(
node.func.value, ast.Name
):
module_name = node.func.value.id
function_name = node.func.attr
if module_name == "thinkstats2" and function_name in class_names:
return node
if function_name in function_names:
node.func.attr = camel_to_snake(function_name)
elif isinstance(node.func, ast.Name):
if node.func.id in function_names:
node.func.id = camel_to_snake(node.func.id)
self.generic_visit(node)
return node
try:
tree = ast.parse(source)
replacer = FunctionNameReplacer()
tree = replacer.visit(tree)
return astor.to_source(tree)
except SyntaxError as e:
print(f"Syntax error in code:\n{source}\nError: {e}")
return source
@app.command()
def get_functions(filenames: List[Path]):
function_names = set()
for filename in filenames:
if filename.suffix == ".ipynb":
print(f"Getting functions from {filename}")
notebook = nbf.read(filename, nbf.NO_CONVERT)
for cell in notebook.cells:
if cell["cell_type"] == "code":
source = "".join(cell["source"])
extract_function_names(source, function_names)
elif filename.suffix == ".py":
print(f"Getting functions from {filename}")
source = filename.read_text()
extract_function_names(source, function_names)
with shelve.open("function_names") as db:
db["function_names"] = function_names
return function_names
def to_camelcase(s):
parts = s.split("_")
return parts[0] + "".join((word.capitalize() for word in parts[1:]))
def is_camelcase(s):
return s[0].isupper() and s == to_camelcase(s)
import re
def camel_to_snake(s):
return re.sub("(?<!^)(?=[A-Z])", "_", s).lower()
def extract_function_names(source, function_names):
class FunctionNameExtractor(ast.NodeVisitor):
def visit_FunctionDef(self, node):
if is_camelcase(node.name):
function_names.add(node.name)
self.generic_visit(node)
try:
tree = ast.parse(source)
extractor = FunctionNameExtractor()
extractor.visit(tree)
except SyntaxError as e:
print(f"Syntax error in code:\n{source}\nError: {e}")
for name in list(function_names):
if is_camelcase(name):
print(name, camel_to_snake(name))
@app.command()
def add_header(header_filename: Path, filenames: List[Path]):
"""
Add a header to the beginning of every notebook
"""
typer.echo(f"Header file: {header_filename}")
typer.echo(f"Target files: {filenames}")
header = nbf.read(header_filename, nbf.NO_CONVERT)
for filename in filenames:
typer.echo(f"Adding {header_filename} to {filename}")
notebook = nbf.read(filename, nbf.NO_CONVERT)
notebook.cells = header.cells + notebook.cells
nbf.write(notebook, filename)
@app.command()
def add_footer(footer_filename: Path, filenames: List[Path]):
"""
Add a footer to the end of every notebook
"""
typer.echo(f"Footer file: {footer_filename}")
typer.echo(f"Target files: {filenames}")
footer = nbf.read(footer_filename, nbf.NO_CONVERT)
for filename in filenames:
typer.echo(f"Adding {footer_filename} to {filename}")
notebook = nbf.read(filename, nbf.NO_CONVERT)
notebook.cells = notebook.cells + footer.cells
nbf.write(notebook, filename)
@app.command()
def replace_header(header_filename: Path, filenames: List[Path],
marker: str = "You can order print"):
"""Replace an existing header cell, matching on content rather than position.
Looks for the first cell whose source starts with `marker` and replaces it
with the cells from `header_filename`. Notebooks without a matching cell are
left alone and reported, so this is safe to run over a directory that mixes
notebooks with and without headers.
"""
header = nbf.read(header_filename, nbf.NO_CONVERT)
replaced, skipped = [], []
for filename in filenames:
notebook = nbf.read(filename, nbf.NO_CONVERT)
index = None
for i, cell in enumerate(notebook.cells):
if cell["cell_type"] == "markdown" and cell["source"].startswith(marker):
index = i
break
if index is None:
skipped.append(filename)
continue
notebook.cells[index:index + 1] = header.cells
nbf.write(notebook, filename)
replaced.append(filename)
typer.echo(f"Replaced the header in {len(replaced)} notebooks")
if skipped:
typer.echo(f"No header found in {len(skipped)} notebooks (left alone):")
for filename in skipped:
typer.echo(f" {filename}")
@app.command()
def remove_header(n: int, filenames: List[Path]):
"""Remove n cells from the beginning of every notebook"""
if n == 0:
print("Removing 0 cells. Nothing to do.")
return
for filename in filenames:
print("Removing first", n, "cells from", filename)
notebook = nbf.read(filename, nbf.NO_CONVERT)
notebook.cells = notebook.cells[n:]
nbf.write(notebook, filename)
@app.command()
def remove_footer(n: int, filenames: List[Path]):
"""Remove n cells from the end of every notebook"""
if n == 0:
print("Removing 0 cells. Nothing to do.")
return
for filename in filenames:
print("Removing last", n, "cells from", filename)
notebook = nbf.read(filename, nbf.NO_CONVERT)
notebook.cells = notebook.cells[:-n]
nbf.write(notebook, filename)
@app.command()
def remove_cell(index: int, filenames: List[Path]):
"""Remove a single cell at the specified index from every notebook"""
for filename in filenames:
print(f"Removing cell at index {index} from {filename}")
notebook = nbf.read(filename, nbf.NO_CONVERT)
if index < 0 or index >= len(notebook.cells):
print(f"Warning: Index {index} is out of range for {filename} (has {len(notebook.cells)} cells)")
continue
# Remove the cell at the specified index
notebook.cells.pop(index)
nbf.write(notebook, filename)
@app.command()
def prepare_latex(filenames: List[Path]):
for filename in filenames:
print(f"Preparing {filename} for LaTeX")
process_notebook(filename, process_cell_latex)
def process_notebook(filename, cell_func):
notebook = nbf.read(filename, nbf.NO_CONVERT)
for cell in notebook.cells:
cell_func(cell)
nbf.write(notebook, filename)
def process_cell_latex(cell):
tags = cell["metadata"].get("tags", [])
if cell["cell_type"] == "code":
source = cell["source"]
if source.startswith("# Solution"):
tag = "hide-cell"
if tag not in tags:
tags.append(tag)
if source.startswith("%%expect"):
t = source.split("\n")[1:]
cell["source"] = "\n".join(t)
for tag in tags:
if tag.startswith("chapter") or tag.startswith("section"):
print(tag)
label = f"({tag})=\n"
cell["source"] = label + cell["source"]
if len(tags) > 0:
cell["metadata"]["tags"] = tags
if __name__ == "__main__":
app()