-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy path_timer.py
More file actions
124 lines (101 loc) · 4.18 KB
/
_timer.py
File metadata and controls
124 lines (101 loc) · 4.18 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""Definition of Timer.
See help(codetiming) for quick instructions, and
https://pypi.org/project/codetiming/ for more details.
"""
# Standard library imports
import math
import time
from contextlib import ContextDecorator
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Callable, ClassVar, Optional, Union
# Codetiming imports
from codetiming._timers import Timers
# Special types, Protocol only works for Python >= 3.8
if TYPE_CHECKING: # pragma: nocover
# Standard library imports
from typing import Protocol, TypeVar
T = TypeVar("T")
class FloatArg(Protocol):
"""Protocol type that allows classes that take one float argument"""
def __call__(self: T, __seconds: float) -> T:
"""Callable signature"""
... # pragma: nocover
else:
class FloatArg:
"""Dummy runtime class"""
# Timer code
class TimerError(Exception):
"""A custom exception used to report errors in use of Timer class."""
@dataclass
class Timer(ContextDecorator):
"""Time your code using a class, context manager, or decorator."""
timers: ClassVar[Timers] = Timers()
name: Optional[str] = None
text: Union[str, FloatArg, Callable[[float], str]] = "Elapsed time: {:0.4f} seconds"
totaltext: Union[str, FloatArg, Callable[[float], str]] = "Total time: {:0.4f} seconds"
initial_text: Union[bool, str, FloatArg] = False
logger: Optional[Callable[[str], None]] = print
last: float = field(default=math.nan, init=False, repr=False)
tick: int = field(default=0, init=False, repr=False)
period: int = field(default=100, init=False, repr=False)
min : Optional[int] = 0
_start_time: Optional[float] = field(default=None, init=False, repr=False)
def start(self) -> None:
"""Start a new timer."""
if self._start_time is not None:
raise TimerError("Timer is running. Use .stop() to stop it")
# Log initial text when timer starts
if self.logger and self.initial_text:
if isinstance(self.initial_text, str):
initial_text = self.initial_text.format(name=self.name)
elif self.name:
initial_text = "Timer {name} started".format(name=self.name)
else:
initial_text = "Timer started"
self.logger(initial_text)
self._start_time = time.perf_counter()
def stop(self) -> float:
"""Stop the timer, and report the elapsed time."""
if self._start_time is None:
raise TimerError("Timer is not running. Use .start() to start it")
# Calculate elapsed time
self.tick += 1
self.last = time.perf_counter() - self._start_time
self._start_time = None
# Report elapsed time
if self.logger and ((self.last >= self.min)) :
if callable(self.text):
text = self.text(self.last)
else:
attributes = {
"name": self.name,
"milliseconds": self.last * 1000,
"seconds": self.last,
"minutes": self.last / 60,
"tick": self.tick
}
text = self.text.format(self.last, **attributes)
self.logger(str(text))
if self.name:
self.timers.add(self.name, self.last)
if (self.tick % self.period) == 0:
if callable(self.totaltext):
text = self.totaltext(self.timers.total(self.name))
else:
attributes = {
"name": self.name,
"milliseconds": self.last * 1000,
"seconds": self.last,
"minutes": self.last / 60,
"tick": self.tick
}
text = self.totaltext.format(self.timers.total(self.name), **attributes)
self.logger(str(text))
return self.last
def __enter__(self) -> "Timer":
"""Start a new timer as a context manager."""
self.start()
return self
def __exit__(self, *exc_info: Any) -> None:
"""Stop the context manager timer."""
self.stop()