Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
probml
GitHub Repository: probml/pyprobml
Path: blob/master/internal/book2/handle_book2_pycode.py
1192 views
1
import os
2
import argparse
3
import nbformat as nbf
4
from probml_utils.nb_utils import get_ipynb_from_code
5
6
parser = argparse.ArgumentParser(description="Handle book2 pycode")
7
parser.add_argument("-bookgen", "--bookgen", type=int, choices=[0, 1], default=0, help="")
8
args = parser.parse_args()
9
10
# configs
11
macro = r"\newcommand{\pycode}[1]{\href{https://github.com/probml/pyprobml/blob/master/scripts/#1.py}{#1.py}}"
12
replaced_macro = r"\newcommand{\pycode}[1]{\href{https://github.com/probml/pyprobml/blob/master/scripts/#1.py}{ssssspy\twodigits{\thechapter}/#1.pyeeeeepy}}"
13
14
book_root = "../bookv2"
15
bookname = "book2"
16
17
if args.bookgen:
18
# replace the macro
19
with open(os.path.join(book_root, "macros.tex"), "r") as f:
20
content = f.read()
21
with open(os.path.join(book_root, "macros.tex"), "w") as f:
22
f.write(content.replace(macro, replaced_macro))
23
24
# generate the book
25
pdflatex_cmd = f"pdflatex --interaction=nonstopmode {bookname}"
26
bibtex_cmd = f"bibtex {bookname}"
27
os.system(
28
f"cd {book_root}/{bookname}; {pdflatex_cmd}; {bibtex_cmd}; {pdflatex_cmd}; mv {bookname}.pdf {bookname}_pycode.pdf"
29
)
30
31
# restore the macro
32
with open(os.path.join(book_root, "macros.tex"), "r") as f:
33
content = f.read()
34
with open(os.path.join(book_root, "macros.tex"), "w") as f:
35
f.write(content.replace(replaced_macro, macro))
36
37
# read the book
38
import fitz
39
40
with fitz.open(os.path.join(book_root, bookname, f"{bookname}_pycode.pdf")) as doc:
41
text = ""
42
for page in doc:
43
text += page.get_text() + ""
44
text_one_line = text.replace("\n", "")
45
46
# solve some glitches
47
glitch_dict = {"fi": "fi", "ff": "ff", "fl": "fl"}
48
for glitch, fix in glitch_dict.items():
49
text_one_line = text_one_line.replace(glitch, fix)
50
51
# parse script names
52
import re
53
54
all_scripts = set(re.findall("ssssspy(.*?)eeeeepy", text_one_line))
55
# print(all_scripts)
56
57
# start transfer
58
for chap_script in all_scripts:
59
chap, script = chap_script.split("/")
60
try:
61
with open("scripts/" + script, "r") as f:
62
nb = get_ipynb_from_code(f.read())
63
save_path = f"notebooks/book2/{chap}/{script.replace('.py', '.ipynb')}"
64
save_dir = os.path.dirname(save_path)
65
assert not os.path.exists(save_path)
66
if not os.path.exists(save_dir):
67
os.makedirs(save_dir)
68
with open(save_path, "w") as f:
69
nbf.write(nb, f)
70
# print("done", chap, script)
71
except Exception as e:
72
print(e, chap, script)
73
74