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)
@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