|
1 |
| -"""Dictionary-like structure with information about timers""" |
| 1 | +"""Dictionary-like structure with information about timers.""" |
2 | 2 |
|
3 | 3 | # Standard library imports
|
4 | 4 | import collections
|
|
14 | 14 |
|
15 | 15 |
|
16 | 16 | class Timers(UserDict):
|
17 |
| - """Custom dictionary that stores information about timers""" |
| 17 | + """Custom dictionary that stores information about timers.""" |
18 | 18 |
|
19 | 19 | def __init__(self, *args: Any, **kwargs: Any) -> None:
|
20 | 20 | """Add a private dictionary keeping track of all timings"""
|
21 | 21 | super().__init__(*args, **kwargs)
|
22 | 22 | self._timings: Dict[str, List[float]] = collections.defaultdict(list)
|
23 | 23 |
|
24 | 24 | def add(self, name: str, value: float) -> None:
|
25 |
| - """Add a timing value to the given timer""" |
| 25 | + """Add a timing value to the given timer.""" |
26 | 26 | self._timings[name].append(value)
|
27 | 27 | self.data.setdefault(name, 0)
|
28 | 28 | self.data[name] += value
|
29 | 29 |
|
30 | 30 | def clear(self) -> None:
|
31 |
| - """Clear timers""" |
| 31 | + """Clear timers.""" |
32 | 32 | self.data.clear()
|
33 | 33 | self._timings.clear()
|
34 | 34 |
|
35 | 35 | def __setitem__(self, name: str, value: float) -> None:
|
36 |
| - """Disallow setting of timer values""" |
| 36 | + """Disallow setting of timer values.""" |
37 | 37 | raise TypeError(
|
38 | 38 | f"{self.__class__.__name__!r} does not support item assignment. "
|
39 | 39 | "Use '.add()' to update values."
|
40 | 40 | )
|
41 | 41 |
|
42 | 42 | def apply(self, func: Callable[[List[float]], float], name: str) -> float:
|
43 |
| - """Apply a function to the results of one named timer""" |
| 43 | + """Apply a function to the results of one named timer.""" |
44 | 44 | if name in self._timings:
|
45 | 45 | return func(self._timings[name])
|
46 | 46 | raise KeyError(name)
|
47 | 47 |
|
48 | 48 | def count(self, name: str) -> float:
|
49 |
| - """Number of timings""" |
| 49 | + """Number of timings.""" |
50 | 50 | return self.apply(len, name=name)
|
51 | 51 |
|
52 | 52 | def total(self, name: str) -> float:
|
53 |
| - """Total time for timers""" |
| 53 | + """Total time for timers.""" |
54 | 54 | return self.apply(sum, name=name)
|
55 | 55 |
|
56 | 56 | def min(self, name: str) -> float:
|
57 |
| - """Minimal value of timings""" |
| 57 | + """Minimal value of timings.""" |
58 | 58 | return self.apply(lambda values: min(values or [0]), name=name)
|
59 | 59 |
|
60 | 60 | def max(self, name: str) -> float:
|
61 |
| - """Maximal value of timings""" |
| 61 | + """Maximal value of timings.""" |
62 | 62 | return self.apply(lambda values: max(values or [0]), name=name)
|
63 | 63 |
|
64 | 64 | def mean(self, name: str) -> float:
|
65 |
| - """Mean value of timings""" |
| 65 | + """Mean value of timings.""" |
66 | 66 | return self.apply(lambda values: statistics.mean(values or [0]), name=name)
|
67 | 67 |
|
68 | 68 | def median(self, name: str) -> float:
|
69 |
| - """Median value of timings""" |
| 69 | + """Median value of timings.""" |
70 | 70 | return self.apply(lambda values: statistics.median(values or [0]), name=name)
|
71 | 71 |
|
72 | 72 | def stdev(self, name: str) -> float:
|
73 |
| - """Standard deviation of timings""" |
| 73 | + """Standard deviation of timings.""" |
74 | 74 | if name in self._timings:
|
75 | 75 | value = self._timings[name]
|
76 | 76 | return statistics.stdev(value) if len(value) >= 2 else math.nan
|
|
0 commit comments