Skip to content

Commit fa176e7

Browse files
committed
Don't have stats be a subclass of dict
1 parent 901b952 commit fa176e7

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

Tools/scripts/summarize_stats.py

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,22 @@ def pretty(name: str) -> str:
4343
return name.replace("_", " ").lower()
4444

4545

46-
class Stats(dict):
46+
class Stats:
4747
def __init__(self, input: Path):
4848
super().__init__()
4949

5050
if input.is_file():
5151
with open(input, "r") as fd:
52-
self.update(json.load(fd))
52+
self._data = json.load(fd)
5353

54-
self["_stats_defines"] = {
54+
self._data["_stats_defines"] = {
5555
int(k): v for k, v in self["_stats_defines"].items()
5656
}
57-
self["_defines"] = {int(k): v for k, v in self["_defines"].items()}
57+
self._data["_defines"] = {int(k): v for k, v in self["_defines"].items()}
5858

5959
elif input.is_dir():
60-
stats: collections.Counter = collections.Counter()
60+
stats = collections.Counter()
61+
6162
for filename in input.iterdir():
6263
with open(filename) as fd:
6364
for line in fd:
@@ -72,15 +73,35 @@ def __init__(self, input: Path):
7273
stats[key.strip()] += int(value)
7374
stats["__nfiles__"] += 1
7475

75-
self.update(stats)
76-
76+
self._data = dict(stats)
7777
self._load_metadata_from_source()
7878

7979
else:
8080
raise ValueError(f"{input:r} is not a file or directory path")
8181

8282
self._opcode_stats: dict[str, OpcodeStats] = {}
8383

84+
def __getitem__(self, key):
85+
return self._data[key]
86+
87+
def __contains__(self, key):
88+
return key in self._data
89+
90+
def get(self, key, default=None):
91+
return self._data.get(key, default)
92+
93+
def items(self):
94+
return self._data.items()
95+
96+
def keys(self):
97+
return self._data.keys()
98+
99+
def values(self):
100+
return self._data.values()
101+
102+
def save(self, json_output: TextIO):
103+
json.dump(self._data, json_output)
104+
84105
def _load_metadata_from_source(self):
85106
def get_defines(filepath: Path, prefix: str = "SPEC_FAIL") -> Defines:
86107
with open(SOURCE_DIR / filepath) as spec_src:
@@ -97,27 +118,27 @@ def get_defines(filepath: Path, prefix: str = "SPEC_FAIL") -> Defines:
97118

98119
import opcode
99120

100-
self["_specialized_instructions"] = [
121+
self._data["_specialized_instructions"] = [
101122
op
102123
for op in opcode._specialized_opmap.keys() # type: ignore
103124
if "__" not in op
104125
]
105-
self["_stats_defines"] = get_defines(
126+
self._data["_stats_defines"] = get_defines(
106127
Path("Include") / "cpython" / "pystats.h", "EVAL_CALL"
107128
)
108-
self["_defines"] = get_defines(Path("Python") / "specialize.c")
129+
self._data["_defines"] = get_defines(Path("Python") / "specialize.c")
109130

110131
@property
111132
def defines(self) -> Defines:
112-
return self["_defines"]
133+
return self._data["_defines"]
113134

114135
@property
115136
def pystats_defines(self) -> Defines:
116-
return self["_stats_defines"]
137+
return self._data["_stats_defines"]
117138

118139
@property
119140
def specialized_instructions(self) -> list[str]:
120-
return self["_specialized_instructions"]
141+
return self._data["_specialized_instructions"]
121142

122143
def get_opcode_stats(self, prefix: str) -> OpcodeStats:
123144
if prefix in self._opcode_stats:
@@ -803,7 +824,8 @@ def calculate_rows(self, stats: Stats) -> Rows:
803824
"Trace too short",
804825
Count(trace_too_short),
805826
Ratio(trace_too_short, created),
806-
)("Inner loop found", Count(inner_loop), Ratio(inner_loop, created)),
827+
),
828+
("Inner loop found", Count(inner_loop), Ratio(inner_loop, created)),
807829
(
808830
"Recursive call",
809831
Count(recursive_call),
@@ -976,11 +998,11 @@ def output_markdown(out: TextIO, base_stats: Stats, head_stats: Stats | None = N
976998
print("Stats gathered on:", date.today(), file=out)
977999

9781000

979-
def output_stats(inputs: list[Path], json_output=None):
1001+
def output_stats(inputs: list[Path], json_output=TextIO | None):
9801002
if len(inputs) == 1:
9811003
stats = Stats(Path(inputs[0]))
9821004
if json_output is not None:
983-
json.dump(stats, json_output)
1005+
stats.save(json_output)
9841006
output_markdown(sys.stdout, stats)
9851007
elif len(inputs) == 2:
9861008
if json_output is not None:

0 commit comments

Comments
 (0)