Belle II Software
release-08-01-10
online_book_statistics.py
1
#!/usr/bin/env python3
2
3
10
11
"""
12
This script calculates some numbers about the online book, e.g. number of
13
exercises etc.
14
"""
15
16
from
pathlib
import
Path
17
import
os
18
import
pprint
19
import
re
20
21
22
class
Statistics
:
23
"""
24
Statistics base class
25
"""
26
def
__init__
(self):
27
"""Initialize members"""
28
self.code_inclusions: int = 0
29
self.hints: int = 0
30
self.solutions: int = 0
31
self.exercises: int = 0
32
self.overview_boxes: int = 0
33
self.key_points: int = 0
34
self.figures: int = 0
35
self.characters: int = 0
36
37
def
print_summary
(self):
38
"""Print the summary"""
39
pprint.pprint(self.__dict__)
40
41
42
class
StatisticsVisitor
:
43
"""
44
Statistics visitor class
45
"""
46
47
regexes = dict(
48
code_inclusions=re.compile(
"(code-block\\s*::)|(literalinclude\\s*::)"
),
49
hints=re.compile(
":class:.*hint"
),
50
solutions=re.compile(
":class:.*solution"
),
51
exercises=re.compile(
":class:.*exercise"
),
52
overview_boxes=re.compile(
":class:.*overview"
),
53
key_points=re.compile(
":class:.*key-points"
),
54
figures=re.compile(
"figure\\s*::"
)
55
)
56
57
def
__init__
(self):
58
"""Initialize class"""
59
60
self.
statistics
statistics =
Statistics
()
61
62
def
read_rst_file
(self, path: Path):
63
"""Read rst file"""
64
text = path.read_text()
65
self.
statistics
statistics.characters += len(text)
66
for
line
in
text.split(
"\n"
):
67
for
key, regex
in
self.
regexes
regexes.items():
68
if
regex.findall(line):
69
setattr(
70
self.
statistics
statistics, key, getattr(self.
statistics
statistics, key) + 1
71
)
72
73
def
walk_directory
(self, path: Path):
74
"""Loop over all directories in the path"""
75
print(f
"Walking {path}"
)
76
for
root, _, files
in
os.walk(path):
77
for
file
in
files:
78
path = Path(root) / file
79
if
path.suffix ==
".rst"
:
80
self.
read_rst_file
read_rst_file(path)
81
82
83
def
main
():
84
this_dir = Path(__file__).resolve().parent
85
sv =
StatisticsVisitor
()
86
sv.walk_directory(this_dir)
87
sv.statistics.print_summary()
88
89
90
if
__name__ ==
"__main__"
:
91
main
()
online_book_statistics.StatisticsVisitor
Definition:
online_book_statistics.py:42
online_book_statistics.StatisticsVisitor.statistics
statistics
statistics object
Definition:
online_book_statistics.py:60
online_book_statistics.StatisticsVisitor.walk_directory
def walk_directory(self, Path path)
Definition:
online_book_statistics.py:73
online_book_statistics.StatisticsVisitor.regexes
regexes
Dictionary keys must match attributes of Statistics class.
Definition:
online_book_statistics.py:47
online_book_statistics.StatisticsVisitor.__init__
def __init__(self)
Definition:
online_book_statistics.py:57
online_book_statistics.StatisticsVisitor.read_rst_file
def read_rst_file(self, Path path)
Definition:
online_book_statistics.py:62
online_book_statistics.Statistics
Definition:
online_book_statistics.py:22
online_book_statistics.Statistics.print_summary
def print_summary(self)
Definition:
online_book_statistics.py:37
online_book_statistics.Statistics.__init__
def __init__(self)
Definition:
online_book_statistics.py:26
main
Definition:
main.py:1
main
int main(int argc, char **argv)
Run all tests.
Definition:
test_main.cc:91
online_book
online_book_statistics.py
Generated on Mon Sep 23 2024 14:03:31 for Belle II Software by
1.9.1