《Python科学计算》第二版目录
写了一个小程序分析《Python科学计算》第二版目前的状态。在下面的目录中,[ ]中的两个数字分别表示章节的文字数和示例代码行数。上级目录的的数字和下级目录中的合计。目前全书有32万字,9100行示例代码。import os
from os import path
import re
import IPython.nbformat.current as nf
def iter_notebooks():
for folder, subfolder, files in os.walk(".."):
name = path.basename(folder)
if re.match(r"\d+-\w+", name):
for fn in files:
if fn.startswith("_"):
continue
if re.match(r"\w+-\d\d\d-.+?\.ipynb", fn):
fn = path.join(folder, fn)
yield path.abspath(fn)
class Heading(object):
def __init__(self, level, title):
self.level = level
self.title = title
self.text_count = 0
self.code_line = 0
self.children = []
def append_text(self, count):
self.text_count += count
def append_code(self, count):
self.code_line += count
def append_heading(self, heading):
self.children.append(heading)
@property
def total_count(self):
return self.text_count + sum(h.total_count for h in self.children)
@property
def total_line(self):
return self.code_line + sum(h.total_line for h in self.children)
root = Heading(0, u"Python科学计算")
stack = [root]
for fn in iter_notebooks():
with open(fn, "rb") as f:
nb = nf.read(f, "json")
first_heading = False
for cell in nb["worksheets"][0]["cells"]:
cell_type = cell["cell_type"]
if cell_type == "heading":
first_heading = True
if not first_heading:
continue
if cell_type == "markdown":
stack[-1].append_text(len(cell["source"]))
elif cell_type == "code":
stack[-1].append_code(len(cell["input"].split("\n")))
elif cell_type == "heading":
level = cell["level"]
heading = Heading(level, cell["source"])
while level
页:
[1]