-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsize.py
30 lines (24 loc) · 931 Bytes
/
size.py
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
"""
Size of the book's code base.
Include all non-blank lines in .py files.
Also include all non-blank lines of the examples.txt files.
"""
from pathlib import Path
from typing import Iterator, NamedTuple
class SampleCode(NamedTuple):
path: Path
lines: int
def module_iter(base: Path = Path.cwd()) -> Iterator:
for p in base.glob("Chapter_*/*.py"):
lines = list(filter(None, p.read_text().splitlines()))
yield SampleCode(p, len(lines))
def example_iter(base: Path = Path.cwd()) -> Iterator:
for p in base.glob("Chapter_*/examples.txt"):
lines = list(filter(None, p.read_text().splitlines()))
yield SampleCode(p, len(lines))
if __name__ == "__main__":
book = list(module_iter()) + list(example_iter())
sample_files = sum(1 for s in book)
lines_of_code = sum(s.lines for s in book)
print(f"{lines_of_code:,d} lines of code")
print(f"{sample_files:,d} files")