Path: blob/master/internal/book2/handle_book2_pycode.py
1192 views
import os1import argparse2import nbformat as nbf3from probml_utils.nb_utils import get_ipynb_from_code45parser = argparse.ArgumentParser(description="Handle book2 pycode")6parser.add_argument("-bookgen", "--bookgen", type=int, choices=[0, 1], default=0, help="")7args = parser.parse_args()89# configs10macro = r"\newcommand{\pycode}[1]{\href{https://github.com/probml/pyprobml/blob/master/scripts/#1.py}{#1.py}}"11replaced_macro = r"\newcommand{\pycode}[1]{\href{https://github.com/probml/pyprobml/blob/master/scripts/#1.py}{ssssspy\twodigits{\thechapter}/#1.pyeeeeepy}}"1213book_root = "../bookv2"14bookname = "book2"1516if args.bookgen:17# replace the macro18with open(os.path.join(book_root, "macros.tex"), "r") as f:19content = f.read()20with open(os.path.join(book_root, "macros.tex"), "w") as f:21f.write(content.replace(macro, replaced_macro))2223# generate the book24pdflatex_cmd = f"pdflatex --interaction=nonstopmode {bookname}"25bibtex_cmd = f"bibtex {bookname}"26os.system(27f"cd {book_root}/{bookname}; {pdflatex_cmd}; {bibtex_cmd}; {pdflatex_cmd}; mv {bookname}.pdf {bookname}_pycode.pdf"28)2930# restore the macro31with open(os.path.join(book_root, "macros.tex"), "r") as f:32content = f.read()33with open(os.path.join(book_root, "macros.tex"), "w") as f:34f.write(content.replace(replaced_macro, macro))3536# read the book37import fitz3839with fitz.open(os.path.join(book_root, bookname, f"{bookname}_pycode.pdf")) as doc:40text = ""41for page in doc:42text += page.get_text() + ""43text_one_line = text.replace("\n", "")4445# solve some glitches46glitch_dict = {"fi": "fi", "ff": "ff", "fl": "fl"}47for glitch, fix in glitch_dict.items():48text_one_line = text_one_line.replace(glitch, fix)4950# parse script names51import re5253all_scripts = set(re.findall("ssssspy(.*?)eeeeepy", text_one_line))54# print(all_scripts)5556# start transfer57for chap_script in all_scripts:58chap, script = chap_script.split("/")59try:60with open("scripts/" + script, "r") as f:61nb = get_ipynb_from_code(f.read())62save_path = f"notebooks/book2/{chap}/{script.replace('.py', '.ipynb')}"63save_dir = os.path.dirname(save_path)64assert not os.path.exists(save_path)65if not os.path.exists(save_dir):66os.makedirs(save_dir)67with open(save_path, "w") as f:68nbf.write(nb, f)69# print("done", chap, script)70except Exception as e:71print(e, chap, script)727374