timer-for-python 0.9.1__py3-none-any.whl → 0.9.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- timer/decorator/function.py +28 -2
- timer/error/__init__.py +2 -2
- timer/helper/decimals.py +1 -1
- timer/helper/thread/list.py +3 -3
- timer/model/thread_item.py +1 -1
- timer/model/timer.py +14 -14
- timer/model/timer_base.py +14 -7
- timer/version.py +1 -1
- {timer_for_python-0.9.1.dist-info → timer_for_python-0.9.3.dist-info}/METADATA +4 -4
- {timer_for_python-0.9.1.dist-info → timer_for_python-0.9.3.dist-info}/RECORD +13 -13
- {timer_for_python-0.9.1.dist-info → timer_for_python-0.9.3.dist-info}/LICENSE.md +0 -0
- {timer_for_python-0.9.1.dist-info → timer_for_python-0.9.3.dist-info}/WHEEL +0 -0
- {timer_for_python-0.9.1.dist-info → timer_for_python-0.9.3.dist-info}/top_level.txt +0 -0
timer/decorator/function.py
CHANGED
|
@@ -9,10 +9,20 @@ from ..model.timer import Timer
|
|
|
9
9
|
def function_timer(thread: str | None = None, decimals: int = 2) -> Callable[..., Any]:
|
|
10
10
|
"""Function decorator to measure the performance of a function."""
|
|
11
11
|
|
|
12
|
+
def get_function_with_arguments_as_thread_name(function: Callable[..., Any], args: tuple[Any, ...], kwargs: dict[str, Any]) -> str:
|
|
13
|
+
thread_name = function.__name__
|
|
14
|
+
if args or kwargs:
|
|
15
|
+
arg_names = function.__code__.co_varnames[:function.__code__.co_argcount]
|
|
16
|
+
mapped_args = [f"{name}={value!r}" for name, value in zip(arg_names, args)]
|
|
17
|
+
mapped_kwargs = [f"{key}={value!r}" for key, value in kwargs.items()]
|
|
18
|
+
all_mapped_args = ", ".join(mapped_args + mapped_kwargs)
|
|
19
|
+
thread_name += f"({all_mapped_args})"
|
|
20
|
+
return thread_name
|
|
21
|
+
|
|
12
22
|
def decorator(function: Callable[..., Any]) -> Callable[..., Any]:
|
|
13
23
|
@functools.wraps(function)
|
|
14
24
|
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
15
|
-
thread_name = thread if thread else function
|
|
25
|
+
thread_name = thread if thread else get_function_with_arguments_as_thread_name(function, args, kwargs)
|
|
16
26
|
with Timer(thread=thread_name, decimals=decimals):
|
|
17
27
|
return function(*args, **kwargs)
|
|
18
28
|
return wrapper
|
|
@@ -59,6 +69,22 @@ class MkDocstringsWrapper(MkDocstringsWrapper_ABC):
|
|
|
59
69
|
How it appears in the terminal:
|
|
60
70
|
|
|
61
71
|
<pre><code>% Elapsed time: 0.12345 seconds for thread <span class="fg-green">CUSTOM</span></code></pre>
|
|
72
|
+
|
|
73
|
+
The `@function_timer` automatically names the function and its arguments as thread name. For example:
|
|
74
|
+
|
|
75
|
+
```python linenums="1" hl_lines="3-4"
|
|
76
|
+
from timer import function_timer
|
|
77
|
+
|
|
78
|
+
@function_timer()
|
|
79
|
+
def sum_numbers(a, b):
|
|
80
|
+
return a + b
|
|
81
|
+
|
|
82
|
+
sum_numbers(1, 2)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
How it appears in the terminal:
|
|
86
|
+
|
|
87
|
+
<pre><code>% Elapsed time: 0.12 seconds for thread <span class="fg-green">SUM_NUMBERS(A=1, B=2)</span></code></pre>
|
|
62
88
|
"""
|
|
63
89
|
|
|
64
|
-
return function_timer(thread=thread, decimals=decimals)
|
|
90
|
+
return function_timer(thread=thread, decimals=decimals) # pragma: no cover
|
timer/error/__init__.py
CHANGED
|
@@ -18,8 +18,8 @@ def stop_controller(timer: TimerBase, thread: str | None) -> None:
|
|
|
18
18
|
print(f"{Color.YELLOW}Timer is not running. Use .start() to start it.{Color.OFF}")
|
|
19
19
|
else:
|
|
20
20
|
print(f"{Color.YELLOW}Timer for thread {thread} is not running. Use .start({thread=}) to start it.{Color.OFF}")
|
|
21
|
-
if len(timer.
|
|
22
|
-
open_threads = [thread_item.name for thread_item in timer.
|
|
21
|
+
if len(timer._threads) > 0:
|
|
22
|
+
open_threads = [thread_item.name for thread_item in timer._threads]
|
|
23
23
|
print(f"Or maybe you aren't stopping the right thread? Currently open threads: {', '.join(open_threads)}")
|
|
24
24
|
|
|
25
25
|
|
timer/helper/decimals.py
CHANGED
|
@@ -8,7 +8,7 @@ from ..model.timer_base import TimerBase
|
|
|
8
8
|
def mediate(timer: TimerBase, decimals: int | None) -> int:
|
|
9
9
|
"""If the start function doesn't have decimals defined, then use the decimals value defined when the Timer() was initiated."""
|
|
10
10
|
|
|
11
|
-
return timer.
|
|
11
|
+
return timer._decimals if decimals is None else validate_and_normalise(decimals)
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
def validate_and_normalise(decimals: int | float | None) -> int:
|
timer/helper/thread/list.py
CHANGED
|
@@ -6,7 +6,7 @@ from ...model.timer_base import TimerBase
|
|
|
6
6
|
def try_get_thread_item_and_index(timer: TimerBase, thread: str) -> tuple[ThreadItem, int] | tuple[None, None]:
|
|
7
7
|
try:
|
|
8
8
|
index_counter = 0
|
|
9
|
-
for thread_item in timer.
|
|
9
|
+
for thread_item in timer._threads:
|
|
10
10
|
# Check if thread already exists in list. Note that this expects a normalised thread (i.e. string and not None) and already in uppercase so it evaluates as intended.
|
|
11
11
|
if thread_item.name == thread:
|
|
12
12
|
# Make sure only the first match is returned, yet the main function is not designed to allow input of duplicates.
|
|
@@ -21,14 +21,14 @@ def try_get_thread_item_and_index(timer: TimerBase, thread: str) -> tuple[Thread
|
|
|
21
21
|
|
|
22
22
|
def add(timer: TimerBase, thread: str, start_time: int, decimals: int) -> None:
|
|
23
23
|
try:
|
|
24
|
-
timer.
|
|
24
|
+
timer._threads.append(ThreadItem(name=thread, start_time=start_time, decimals=decimals))
|
|
25
25
|
except Exception:
|
|
26
26
|
error.message_for_action("when trying to add entry to the Timer's thread list", thread)
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
def remove(timer: TimerBase, entry_index: int) -> None:
|
|
30
30
|
try:
|
|
31
|
-
timer.
|
|
31
|
+
timer._threads.pop(entry_index)
|
|
32
32
|
except Exception:
|
|
33
33
|
error.message_for_action(
|
|
34
34
|
f"when trying to remove entry from the Timer's thread list for entry index \"{entry_index}\"")
|
timer/model/thread_item.py
CHANGED
timer/model/timer.py
CHANGED
|
@@ -9,8 +9,8 @@ from .timer_base import TimerBase
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
class Timer(TimerBase):
|
|
12
|
-
__slots__ = ["
|
|
13
|
-
"
|
|
12
|
+
__slots__ = ["_threads", "_decimals",
|
|
13
|
+
"_context_manager_threads", "_context_manager_latest_thread", "_context_manager_latest_decimals",
|
|
14
14
|
"__dict__"]
|
|
15
15
|
|
|
16
16
|
_instance = None
|
|
@@ -85,21 +85,21 @@ class Timer(TimerBase):
|
|
|
85
85
|
"""
|
|
86
86
|
|
|
87
87
|
if not self._lock_init: # Ensure that initialisation of the lists only runs the first time.
|
|
88
|
-
self.
|
|
89
|
-
self.
|
|
88
|
+
self._threads: list[ThreadItem] = []
|
|
89
|
+
self._context_manager_threads: list[str] = []
|
|
90
90
|
self._lock_init = True
|
|
91
|
-
self.
|
|
91
|
+
self._decimals: int = decimals if decimals == constant.decimals.DEFAULT else helper.decimals.validate_and_normalise(
|
|
92
92
|
decimals)
|
|
93
|
-
self.
|
|
94
|
-
self.
|
|
93
|
+
self._context_manager_latest_thread: str = helper.thread.normalise_to_string_and_uppercase(thread)
|
|
94
|
+
self._context_manager_latest_decimals: int = self._decimals
|
|
95
95
|
|
|
96
96
|
def __enter__(self) -> Timer:
|
|
97
|
-
self.
|
|
98
|
-
self.start(self.
|
|
97
|
+
self._context_manager_threads.append(self._context_manager_latest_thread)
|
|
98
|
+
self.start(self._context_manager_latest_thread, self._context_manager_latest_decimals)
|
|
99
99
|
return self
|
|
100
100
|
|
|
101
101
|
def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> None:
|
|
102
|
-
last_context_manager_thread = self.
|
|
102
|
+
last_context_manager_thread = self._context_manager_threads.pop()
|
|
103
103
|
self.stop(last_context_manager_thread)
|
|
104
104
|
|
|
105
105
|
def start(self, thread: str | None = None, decimals: int | None = None) -> None:
|
|
@@ -150,8 +150,8 @@ class Timer(TimerBase):
|
|
|
150
150
|
try:
|
|
151
151
|
start_time = time.perf_counter_ns() # For precision, this is the first operation of the function.
|
|
152
152
|
controller.start(self, thread, start_time, decimals)
|
|
153
|
-
except Exception:
|
|
154
|
-
error.message_for_action("when trying to start the Timer", thread)
|
|
153
|
+
except Exception: # pragma: no cover
|
|
154
|
+
error.message_for_action("when trying to start the Timer", thread) # pragma: no cover
|
|
155
155
|
|
|
156
156
|
def stop(self, thread: str | None = None) -> None:
|
|
157
157
|
"""Stops the Timer. Should always be called after `timer.start()`.
|
|
@@ -200,5 +200,5 @@ class Timer(TimerBase):
|
|
|
200
200
|
try:
|
|
201
201
|
stop_time = time.perf_counter_ns() # For precision, this is the first operation of the function.
|
|
202
202
|
controller.stop(self, thread, stop_time)
|
|
203
|
-
except Exception:
|
|
204
|
-
error.message_for_action("when trying to stop the Timer", thread)
|
|
203
|
+
except Exception: # pragma: no cover
|
|
204
|
+
error.message_for_action("when trying to stop the Timer", thread) # pragma: no cover
|
timer/model/timer_base.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
from abc import ABC, abstractmethod
|
|
2
|
+
from types import TracebackType
|
|
2
3
|
|
|
3
4
|
from .thread_item import ThreadItem
|
|
4
5
|
|
|
@@ -6,20 +7,26 @@ from .thread_item import ThreadItem
|
|
|
6
7
|
class TimerBase(ABC):
|
|
7
8
|
"""Abstract base class of Timer object."""
|
|
8
9
|
|
|
9
|
-
__slots__ = ["
|
|
10
|
+
__slots__ = ["_decimals", "_threads"]
|
|
10
11
|
|
|
11
12
|
def __init__(self) -> None:
|
|
12
|
-
self.
|
|
13
|
-
self.
|
|
13
|
+
self._decimals: int # pragma: no cover
|
|
14
|
+
self._threads: list[ThreadItem] # pragma: no cover
|
|
15
|
+
|
|
16
|
+
def __enter__(self) -> "TimerBase":
|
|
17
|
+
raise NotImplementedError # pragma: no cover
|
|
18
|
+
|
|
19
|
+
def __exit__(self, exc_type: type[BaseException] | None, exc_value: BaseException | None, traceback: TracebackType | None) -> None:
|
|
20
|
+
raise NotImplementedError # pragma: no cover
|
|
14
21
|
|
|
15
22
|
@abstractmethod
|
|
16
|
-
def start(self, thread: str, decimals: int) -> None:
|
|
23
|
+
def start(self, thread: str | None = None, decimals: int | None = None) -> None:
|
|
17
24
|
"""Starts the Timer."""
|
|
18
25
|
|
|
19
|
-
raise NotImplementedError
|
|
26
|
+
raise NotImplementedError # pragma: no cover
|
|
20
27
|
|
|
21
28
|
@abstractmethod
|
|
22
|
-
def stop(self, thread: str) -> None:
|
|
29
|
+
def stop(self, thread: str | None = None) -> None:
|
|
23
30
|
"""Stops the Timer."""
|
|
24
31
|
|
|
25
|
-
raise NotImplementedError
|
|
32
|
+
raise NotImplementedError # pragma: no cover
|
timer/version.py
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
__version_info__ = (0, 9,
|
|
1
|
+
__version_info__ = (0, 9, 3)
|
|
2
2
|
__version__ = ".".join(map(str, __version_info__))
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: timer-for-python
|
|
3
|
-
Version: 0.9.
|
|
3
|
+
Version: 0.9.3
|
|
4
4
|
Summary: Timer for Python
|
|
5
5
|
Home-page: https://jakob-bagterp.github.io/timer-for-python/
|
|
6
6
|
Download-URL: https://pypi.org/project/timer-for-python/
|
|
@@ -27,15 +27,15 @@ Description-Content-Type: text/markdown
|
|
|
27
27
|
License-File: LICENSE.md
|
|
28
28
|
Requires-Dist: colorist==1.8.3
|
|
29
29
|
Provides-Extra: testing
|
|
30
|
-
Requires-Dist: coverage==7.6.
|
|
30
|
+
Requires-Dist: coverage==7.6.9; extra == "testing"
|
|
31
31
|
Requires-Dist: flake8==7.1.1; extra == "testing"
|
|
32
32
|
Requires-Dist: mypy==1.13.0; extra == "testing"
|
|
33
|
-
Requires-Dist: numpy==2.
|
|
33
|
+
Requires-Dist: numpy==2.2.0; extra == "testing"
|
|
34
34
|
Requires-Dist: pytest==8.3.4; extra == "testing"
|
|
35
35
|
Requires-Dist: pytest-cov==6.0.0; extra == "testing"
|
|
36
36
|
Requires-Dist: tox==4.23.2; extra == "testing"
|
|
37
37
|
|
|
38
|
-
[](https://github.com/jakob-bagterp/timer-for-python/releases/latest)
|
|
39
39
|
[](https://www.python.org)
|
|
40
40
|
[](https://github.com/jakob-bagterp/timer-for-python/blob/master/LICENSE.md)
|
|
41
41
|
[](https://codecov.io/gh/jakob-bagterp/timer-for-python)
|
|
@@ -1,28 +1,28 @@
|
|
|
1
1
|
timer/__init__.py,sha256=X7Z5wKwhKDIzSnmtYR5Img5iGp13-vfgMWSWGFfHGq4,158
|
|
2
2
|
timer/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
|
-
timer/version.py,sha256=
|
|
3
|
+
timer/version.py,sha256=Xgjv2rs6ze7JmN4zbN9X8Jfx0kUus9UjcBUsukS92pA,80
|
|
4
4
|
timer/constant/__init__.py,sha256=0e0W-lRc5xtJ9LSsEyq4o6zKckaRweAvrntmFE1gisE,67
|
|
5
5
|
timer/constant/decimals.py,sha256=9SKe-IbFIbXY-Mps6bSYBqaWuxLiRlsa3rNmT_oDbiM,53
|
|
6
6
|
timer/constant/various.py,sha256=v_UX49eVff3JnXs_VgS0luKkCc3iIH_EUalPxNt8-YQ,117
|
|
7
7
|
timer/controller/__init__.py,sha256=lN-rF2nX4pCZ-Z8l7kJTlk4mmsInfLZ52NY4S6Wf6A0,1662
|
|
8
8
|
timer/decorator/__init__.py,sha256=juqd9PbXs4yg45zMJ7BHAOPQjb7sgEbWE9InBtGZhfo,24
|
|
9
|
-
timer/decorator/function.py,sha256=
|
|
10
|
-
timer/error/__init__.py,sha256=
|
|
9
|
+
timer/decorator/function.py,sha256=M5EI68cxE8atPgPoEdjXWDKuzJ25cLZ0vDjrft1JOzk,3553
|
|
10
|
+
timer/error/__init__.py,sha256=I0WE1idFHucWU66AsPTg5hqDCuzd1l1Bo5V4cLFCivE,1183
|
|
11
11
|
timer/helper/__init__.py,sha256=kYe7PVpjG7UeamvAvohgiIleefsGslx1tJvuNO5_oSQ,117
|
|
12
|
-
timer/helper/decimals.py,sha256=
|
|
12
|
+
timer/helper/decimals.py,sha256=5S4Ab1P3yABUjhW0BLgjJMSH9jizbSNXgsvHoWWnZbE,1569
|
|
13
13
|
timer/helper/output.py,sha256=KeI1L3I3cRjlTQKRFKMsG-j8_OGSEJEH0Ipc2KbQC8Q,1714
|
|
14
14
|
timer/helper/time_fractions.py,sha256=5mjbiy-LmChZDRF_gg-jxJAZOLdJxIv8oOfLBgviVB0,1083
|
|
15
15
|
timer/helper/thread/__init__.py,sha256=sIXJTI05fLVUT71DbiBf0axFpxOn0L9sRe23KUOC8Gw,471
|
|
16
|
-
timer/helper/thread/list.py,sha256=
|
|
16
|
+
timer/helper/thread/list.py,sha256=19A85nXIB_gyt-wsNHNoIsfxRwbWMuH9DrgGf8daYCg,1477
|
|
17
17
|
timer/model/__init__.py,sha256=m-K3jgLBqKUwBNJByKeLiGQCzCVZC07N12_SmkbGxyg,145
|
|
18
18
|
timer/model/elapsed_time_fractions.py,sha256=UXfABMQ14kgsEHjQmef2PKMgtdqKF9zPyrmA9_TQHrg,282
|
|
19
|
-
timer/model/thread_item.py,sha256=
|
|
19
|
+
timer/model/thread_item.py,sha256=7i7oH2eghF5K_k4Oed-i6to85VoPlJw3_g0U5ilB_VY,199
|
|
20
20
|
timer/model/time_fractions.py,sha256=yweResJFS7NutBoLWZYKC1f7GIemwWVIiEc04xsxOno,1298
|
|
21
|
-
timer/model/timer.py,sha256=
|
|
22
|
-
timer/model/timer_base.py,sha256=
|
|
21
|
+
timer/model/timer.py,sha256=NWUPg0InWuOruwXNx1P6wgGzj6s9_PqKCDhhp8qXqM4,7144
|
|
22
|
+
timer/model/timer_base.py,sha256=RV0KelcP_nvMhVbBR3kC3q1dnvckAHUp4BxgJGrDiJo,1001
|
|
23
23
|
timer/model/abc/mkdocstrings.py,sha256=3JrQVkW0D4YW7hOVOr9Jd35gH7-Z5o8y0bNjQz1EAzw,350
|
|
24
|
-
timer_for_python-0.9.
|
|
25
|
-
timer_for_python-0.9.
|
|
26
|
-
timer_for_python-0.9.
|
|
27
|
-
timer_for_python-0.9.
|
|
28
|
-
timer_for_python-0.9.
|
|
24
|
+
timer_for_python-0.9.3.dist-info/LICENSE.md,sha256=bV6mZhoYf6kHYGgsG3ArjpF_tjOweaI7QZKRvmzkUMI,1085
|
|
25
|
+
timer_for_python-0.9.3.dist-info/METADATA,sha256=-Oy76uZjgiGAL5oiy-vHAHLDHexfvIjYYjW-CPOlMzs,7103
|
|
26
|
+
timer_for_python-0.9.3.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
|
|
27
|
+
timer_for_python-0.9.3.dist-info/top_level.txt,sha256=mbqaJ4u4Wh38RQxq5PFpTOu6h1EcSHjwI2bqdDYCRAI,6
|
|
28
|
+
timer_for_python-0.9.3.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|