timer-for-python 0.7.18__py3-none-any.whl → 0.9.0__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/__init__.py CHANGED
@@ -1,5 +1,5 @@
1
- __all__ = ["benchmark_timer", "Timer"]
1
+ __all__ = ["function_timer", "Timer"]
2
2
 
3
- from .decorator.benchmark import benchmark_timer
3
+ from .decorator.function import function_timer
4
4
  from .model.timer import Timer
5
5
  from .version import __version__ # noqa
@@ -0,0 +1 @@
1
+ __all__: list[str] = []
@@ -0,0 +1,64 @@
1
+ import functools
2
+ from collections.abc import Callable
3
+ from typing import Any
4
+
5
+ from ..model.abc.mkdocstrings import MkDocstringsWrapper_ABC
6
+ from ..model.timer import Timer
7
+
8
+
9
+ def function_timer(thread: str | None = None, decimals: int = 2) -> Callable[..., Any]:
10
+ """Function decorator to measure the performance of a function."""
11
+
12
+ def decorator(function: Callable[..., Any]) -> Callable[..., Any]:
13
+ @functools.wraps(function)
14
+ def wrapper(*args: Any, **kwargs: Any) -> Any:
15
+ thread_name = thread if thread else function.__name__
16
+ with Timer(thread=thread_name, decimals=decimals):
17
+ return function(*args, **kwargs)
18
+ return wrapper
19
+ return decorator
20
+
21
+
22
+ class MkDocstringsWrapper(MkDocstringsWrapper_ABC):
23
+ def function_timer(self, thread: str | None = None, decimals: int = 2) -> Callable[..., Any]:
24
+ """Function decorator to measure the performance of a function.
25
+
26
+ Args:
27
+ thread (str | None, optional): Option to start new thread. By default, the thread name is the function name.
28
+ decimals (int | None, optional): Option to define decimals for output. Minimum `0` (for no decimals) and maximum `9`. If `None`, default is `2` decimals. May be overruled in certain cases due to [humanised output](../user-guide/humanised-output.md).
29
+
30
+ Example:
31
+ Basic usage:
32
+
33
+ ```python linenums="1" hl_lines="3"
34
+ from timer import function_timer
35
+
36
+ @function_timer()
37
+ def test_function():
38
+ # Insert your code here
39
+
40
+ test_function()
41
+ ```
42
+
43
+ How it appears in the terminal:
44
+
45
+ <pre><code>% Elapsed time: 12.34 seconds for thread <span class="fg-green">TEST_FUNCTION</span></code></pre>
46
+
47
+ With custom thread name and decimals:
48
+
49
+ ```python linenums="1" hl_lines="3"
50
+ from timer import function_timer
51
+
52
+ @function_timer(thread="custom", decimals=5)
53
+ def test_function():
54
+ # Insert your code here
55
+
56
+ test_function()
57
+ ```
58
+
59
+ How it appears in the terminal:
60
+
61
+ <pre><code>% Elapsed time: 0.12345 seconds for thread <span class="fg-green">CUSTOM</span></code></pre>
62
+ """
63
+
64
+ return function_timer(thread=thread, decimals=decimals)
timer/helper/output.py CHANGED
@@ -6,28 +6,29 @@ from ..model.time_fractions import TimeFractions
6
6
 
7
7
  def message(thread: str, fractions: TimeFractions, decimals: int) -> None:
8
8
  try:
9
- text_intro = f"Elapsed time{'' if helper.thread.is_none(thread) else f' (thread {Color.GREEN}{thread}{Color.OFF})'}:"
9
+ intro = "Elapsed time:"
10
+ thread_info = "" if helper.thread.is_none(thread) else f" for thread {Color.GREEN}{thread}{Color.OFF}"
10
11
  time = fractions.time
11
12
  if time.days > 0:
12
13
  # Format: 1d 2h 3m 4s
13
- print(f"{text_intro} {time.days}d {time.hours}h {time.minutes}m {fractions.seconds_rounded()}s")
14
+ print(f"{intro} {time.days}d {time.hours}h {time.minutes}m {fractions.seconds_rounded()}s{thread_info}")
14
15
  elif time.hours > 0:
15
16
  # Format: 1h 2m 3s
16
- print(f"{text_intro} {time.hours}h {time.minutes}m {fractions.seconds_rounded()}s")
17
+ print(f"{intro} {time.hours}h {time.minutes}m {fractions.seconds_rounded()}s{thread_info}")
17
18
  elif time.minutes > 0:
18
19
  # Format: 62.34 seconds (1m 2s)
19
- print(f"{text_intro} {fractions.count_minutes_to_seconds():.{decimals}f} seconds ({time.minutes}m {fractions.seconds_rounded()}s)")
20
+ print(f"{intro} {fractions.count_minutes_to_seconds():.{decimals}f} seconds ({time.minutes}m {fractions.seconds_rounded()}s){thread_info}")
20
21
  elif time.seconds > 0:
21
22
  # Format: 0.123456789 seconds
22
- print(f"{text_intro} {fractions.count_seconds_to_float():.{decimals}f} seconds")
23
+ print(f"{intro} {fractions.count_seconds_to_float():.{decimals}f} seconds{thread_info}")
23
24
  elif time.milliseconds > 0:
24
25
  # Format: 123.45 milliseconds
25
- print(f"{text_intro} {fractions.count_milliseconds_to_float():.{decimals}f} milliseconds")
26
+ print(f"{intro} {fractions.count_milliseconds_to_float():.{decimals}f} milliseconds{thread_info}")
26
27
  elif time.microseconds > 0:
27
28
  # Format: 234.56 microseconds
28
- print(f"{text_intro} {fractions.count_microseconds_to_float():.{decimals}f} microseconds")
29
+ print(f"{intro} {fractions.count_microseconds_to_float():.{decimals}f} microseconds{thread_info}")
29
30
  else:
30
31
  # Format: 345 nanoseconds
31
- print(f"{text_intro} {time.nanoseconds} nanoseconds")
32
+ print(f"{intro} {time.nanoseconds} nanoseconds{thread_info}")
32
33
  except Exception:
33
34
  error.message_for_action("in the Timer's output message module", thread)
@@ -0,0 +1,10 @@
1
+ from abc import ABC
2
+
3
+
4
+ class MkDocstringsWrapper_ABC(ABC):
5
+ """Abstract base class used to documentation standalone functions now that MkDocstrings (as far as I know) only support reference of functions within classes.
6
+
7
+ Should not used for other purposes than documentation."""
8
+
9
+ def __init__(self) -> None:
10
+ pass # pragma: no cover
timer/model/timer.py CHANGED
@@ -62,12 +62,12 @@ class Timer(TimerBase):
62
62
  ```python linenums="1" hl_lines="3"
63
63
  from timer import Timer
64
64
 
65
- timer = Timer(thread="my_thread", decimals=5)
65
+ timer = Timer(thread="custom", decimals=5)
66
66
  timer.start()
67
67
 
68
68
  # Insert your code here
69
69
 
70
- timer.stop(thread="my_thread")
70
+ timer.stop(thread="custom")
71
71
  ```
72
72
 
73
73
  Or with a `with` statement as [context manager](../user-guide/context-manager.md):
@@ -75,15 +75,13 @@ class Timer(TimerBase):
75
75
  ```python linenums="1" hl_lines="3"
76
76
  from timer import Timer
77
77
 
78
- with Timer(thread="my_thread", decimals=5):
78
+ with Timer(thread="custom", decimals=5):
79
79
  # Insert your code here
80
80
  ```
81
81
 
82
82
  As before, the terminal will output the same result in both cases:
83
83
 
84
- ```text title=""
85
- Elapsed time: 0.12345 seconds for thread MY_THREAD
86
- ```
84
+ <pre><code>% Elapsed time: 0.12345 seconds for thread <span class="fg-green">CUSTOM</span></code></pre>
87
85
  """
88
86
 
89
87
  if not self._lock_init: # Ensure that initialisation of the lists only runs the first time.
@@ -125,7 +123,7 @@ class Timer(TimerBase):
125
123
  timer.stop()
126
124
  ```
127
125
 
128
- Terminal output example:
126
+ How it appears in the terminal:
129
127
 
130
128
  ```text title=""
131
129
  Elapsed time: 12.34 seconds
@@ -137,18 +135,16 @@ class Timer(TimerBase):
137
135
  from timer import Timer
138
136
 
139
137
  timer = Timer()
140
- timer.start(thread="my_thread", decimals=5)
138
+ timer.start(thread="custom", decimals=5)
141
139
 
142
140
  # Insert your code here
143
141
 
144
- timer.stop(thread="my_thread")
142
+ timer.stop(thread="custom")
145
143
  ```
146
144
 
147
- Terminal output example:
145
+ How it appears in the terminal:
148
146
 
149
- ```text title=""
150
- Elapsed time: 0.12345 seconds for thread MY_THREAD
151
- ```
147
+ <pre><code>% Elapsed time: 0.12345 seconds for thread <span class="fg-green">CUSTOM</span></code></pre>
152
148
  """
153
149
 
154
150
  try:
@@ -177,7 +173,7 @@ class Timer(TimerBase):
177
173
  timer.stop()
178
174
  ```
179
175
 
180
- Terminal output example:
176
+ How it appears in the terminal:
181
177
 
182
178
  ```text title=""
183
179
  Elapsed time: 12.34 seconds
@@ -189,18 +185,16 @@ class Timer(TimerBase):
189
185
  from timer import Timer
190
186
 
191
187
  timer = Timer()
192
- timer.start(thread="my_thread", decimals=5)
188
+ timer.start(thread="custom", decimals=5)
193
189
 
194
190
  # Insert your code here
195
191
 
196
- timer.stop(thread="my_thread")
192
+ timer.stop(thread="custom")
197
193
  ```
198
194
 
199
- Terminal output example:
195
+ How it appears in the terminal:
200
196
 
201
- ```text title=""
202
- Elapsed time: 0.12345 seconds for thread MY_THREAD
203
- ```
197
+ <pre><code>% Elapsed time: 0.12345 seconds for thread <span class="fg-green">CUSTOM</span></code></pre>
204
198
  """
205
199
 
206
200
  try:
timer/version.py CHANGED
@@ -1,2 +1,2 @@
1
- __version_info__ = (0, 7, 18)
1
+ __version_info__ = (0, 9, 0)
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.7.18
3
+ Version: 0.9.0
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/
@@ -25,21 +25,21 @@ Classifier: Operating System :: OS Independent
25
25
  Requires-Python: >=3.10
26
26
  Description-Content-Type: text/markdown
27
27
  License-File: LICENSE.md
28
- Requires-Dist: colorist==1.8.1
28
+ Requires-Dist: colorist==1.8.3
29
29
  Provides-Extra: testing
30
- Requires-Dist: coverage==7.6.4; extra == "testing"
30
+ Requires-Dist: coverage==7.6.8; 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.1.2; extra == "testing"
34
- Requires-Dist: pytest==8.3.3; extra == "testing"
35
- Requires-Dist: pytest-cov==5.0.0; extra == "testing"
33
+ Requires-Dist: numpy==2.1.3; extra == "testing"
34
+ Requires-Dist: pytest==8.3.4; extra == "testing"
35
+ Requires-Dist: pytest-cov==6.0.0; extra == "testing"
36
36
  Requires-Dist: tox==4.23.2; extra == "testing"
37
37
 
38
- [![Latest version](https://img.shields.io/static/v1?label=version&message=0.7.18&color=yellowgreen)](https://github.com/jakob-bagterp/timer-for-python/releases/latest)
38
+ [![Latest version](https://img.shields.io/static/v1?label=version&message=0.9.0&color=yellowgreen)](https://github.com/jakob-bagterp/timer-for-python/releases/latest)
39
39
  [![Python 3.10 | 3.11 | 3.12 | 3.13+](https://img.shields.io/static/v1?label=python&message=3.10%20|%203.11%20|%203.12%20|%203.13%2B&color=blueviolet)](https://www.python.org)
40
40
  [![MIT license](https://img.shields.io/static/v1?label=license&message=MIT&color=blue)](https://github.com/jakob-bagterp/timer-for-python/blob/master/LICENSE.md)
41
41
  [![Codecov](https://codecov.io/gh/jakob-bagterp/timer-for-python/branch/master/graph/badge.svg?token=P4IT8WQO0R)](https://codecov.io/gh/jakob-bagterp/timer-for-python)
42
- [![CodeQL](https://github.com/jakob-bagterp/timer-for-python/actions/workflows/codeql.yml/badge.svg)](https://github.com/jakob-bagterp/timer-for-python/actions/workflows/codeql.yml)
42
+ [![CodeQL](https://github.com/jakob-bagterp/timer-for-python/actions/workflows/github-code-scanning/codeql/badge.svg)](https://github.com/jakob-bagterp/timer-for-python/actions/workflows/github-code-scanning/codeql)
43
43
  [![Test](https://github.com/jakob-bagterp/timer-for-python/actions/workflows/test.yml/badge.svg)](https://github.com/jakob-bagterp/timer-for-python/actions/workflows/test.yml)
44
44
  [![Downloads](https://static.pepy.tech/badge/timer-for-python)](https://pepy.tech/project/timer-for-python)
45
45
 
@@ -74,12 +74,12 @@ with Timer():
74
74
  ```
75
75
 
76
76
  #### Decorator
77
- Or use the `benchmark_timer` as a function decorator:
77
+ Or use the `function_timer` as a function decorator:
78
78
 
79
79
  ```python
80
- from timer import benchmark_timer
80
+ from timer import function_timer
81
81
 
82
- @benchmark_timer
82
+ @function_timer()
83
83
  def test_function():
84
84
  # Insert your code here
85
85
 
@@ -90,7 +90,7 @@ test_function()
90
90
 
91
91
  ### Core Features
92
92
  #### Decimals
93
- Instead of the default value of `2` for `decimals``, you can set the output precision up to `9` in the `decimals` argument:
93
+ Instead of the default value of `2` for `decimals``, you can set the output precision up to `9` in the `decimals` parameter:
94
94
 
95
95
  ```python
96
96
  timer = Timer()
@@ -104,7 +104,7 @@ timer.stop() # Output example: 0.12345 seconds
104
104
  #### Multiple Threads
105
105
  Imagine that you want to troubleshoot which parts of your code are performing better or worse. Or do you want to split-test the performance of different methods? Timer for Python is a quick, easy way to get the job done.
106
106
 
107
- To measure the performance of multiple blocks of code, use the `thread` argument to name different threads:
107
+ To measure the performance of multiple blocks of code, use the `thread` parameter to name different threads:
108
108
 
109
109
  ```python
110
110
  timer = Timer()
@@ -138,9 +138,12 @@ with Timer(thread="A")
138
138
  # Output example: 6.78 seconds for thread A
139
139
  ```
140
140
 
141
- # Thank You for Supporting
142
- ## Donate
143
- This module is free to use. And if you like it, feel free to [buy me a coffee](https://github.com/sponsors/jakob-bagterp).
141
+ ## Become a Sponsor 🏅
142
+ If you find this project helpful, please consider supporting its development. Your donations will help keep it alive and growing. Every contribution, no matter the size, makes a difference.
143
+
144
+ [Donate on GitHub Sponsors](https://github.com/sponsors/jakob-bagterp)
145
+
146
+ Thank you for your support! 🙌
144
147
 
145
148
  ## Contribute
146
149
  If you have suggestions or changes to the module, feel free to add to the code and create a [pull request](https://github.com/jakob-bagterp/timer-for-python/pulls).
@@ -1,15 +1,16 @@
1
- timer/__init__.py,sha256=VP-n29fPZdottjrmQYnt4cYVH5Dq6Bx3CQ5-dlDPK9E,161
1
+ timer/__init__.py,sha256=X7Z5wKwhKDIzSnmtYR5Img5iGp13-vfgMWSWGFfHGq4,158
2
2
  timer/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- timer/version.py,sha256=hkXI-amSC7n6JYVSzG8Yh9h0XvzI5WKPPyiXaP9byFM,81
3
+ timer/version.py,sha256=4k7YilT_zjqlhLysJxekabcGUToaX9CsuJXhbUvhXkA,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
- timer/decorator/benchmark.py,sha256=HgZZpXyqZ4SB992LNEYeH--k4krLBkstgvih3l40Hco,378
8
+ timer/decorator/__init__.py,sha256=juqd9PbXs4yg45zMJ7BHAOPQjb7sgEbWE9InBtGZhfo,24
9
+ timer/decorator/function.py,sha256=nrLO8xoKO9JVVfnoTQhTv-lkfW-4rgd2SGckHM8p2xc,2351
9
10
  timer/error/__init__.py,sha256=H63gq-IevWXo95V5-MqqUzZhmciyvGfT7QizebFxgQA,1181
10
11
  timer/helper/__init__.py,sha256=kYe7PVpjG7UeamvAvohgiIleefsGslx1tJvuNO5_oSQ,117
11
12
  timer/helper/decimals.py,sha256=4FnEfWla-cyk12IYVdm7Me_T2FT6eAIft_uhMUmj4mo,1568
12
- timer/helper/output.py,sha256=6hHHg6tTiFYpf5ckxIwimmHsQJHQ6QEFywfeSAjrROA,1641
13
+ timer/helper/output.py,sha256=KeI1L3I3cRjlTQKRFKMsG-j8_OGSEJEH0Ipc2KbQC8Q,1714
13
14
  timer/helper/time_fractions.py,sha256=5mjbiy-LmChZDRF_gg-jxJAZOLdJxIv8oOfLBgviVB0,1083
14
15
  timer/helper/thread/__init__.py,sha256=sIXJTI05fLVUT71DbiBf0axFpxOn0L9sRe23KUOC8Gw,471
15
16
  timer/helper/thread/list.py,sha256=jFpbwvwFbvDHVOkz780RRYo03m5t6RW9Tfd-XnlCchE,1474
@@ -17,10 +18,11 @@ timer/model/__init__.py,sha256=m-K3jgLBqKUwBNJByKeLiGQCzCVZC07N12_SmkbGxyg,145
17
18
  timer/model/elapsed_time_fractions.py,sha256=UXfABMQ14kgsEHjQmef2PKMgtdqKF9zPyrmA9_TQHrg,282
18
19
  timer/model/thread_item.py,sha256=4c6cOAvTuy1Yi30oG46nHM84Bgu3c5aj7vTKjPyVQE8,212
19
20
  timer/model/time_fractions.py,sha256=yweResJFS7NutBoLWZYKC1f7GIemwWVIiEc04xsxOno,1298
20
- timer/model/timer.py,sha256=2_zTd3IzSnQ_hKDSazJdNlTVAbmm1p7fhIrpJHx_uw4,7017
21
+ timer/model/timer.py,sha256=PVE6DfyAdJJabB6RBDMEreUhIjkdpHcnNyEo4Mm5Bag,7048
21
22
  timer/model/timer_base.py,sha256=SjDT1qu3wR9c89VmIpScXEDWOI0My5mbboLD6A7jir0,557
22
- timer_for_python-0.7.18.dist-info/LICENSE.md,sha256=bV6mZhoYf6kHYGgsG3ArjpF_tjOweaI7QZKRvmzkUMI,1085
23
- timer_for_python-0.7.18.dist-info/METADATA,sha256=7aIxqad6Z7uwlu73q6bpelSEgs_Cv3Voadrp11L45gY,6909
24
- timer_for_python-0.7.18.dist-info/WHEEL,sha256=eOLhNAGa2EW3wWl_TU484h7q1UNgy0JXjjoqKoxAAQc,92
25
- timer_for_python-0.7.18.dist-info/top_level.txt,sha256=mbqaJ4u4Wh38RQxq5PFpTOu6h1EcSHjwI2bqdDYCRAI,6
26
- timer_for_python-0.7.18.dist-info/RECORD,,
23
+ timer/model/abc/mkdocstrings.py,sha256=3JrQVkW0D4YW7hOVOr9Jd35gH7-Z5o8y0bNjQz1EAzw,350
24
+ timer_for_python-0.9.0.dist-info/LICENSE.md,sha256=bV6mZhoYf6kHYGgsG3ArjpF_tjOweaI7QZKRvmzkUMI,1085
25
+ timer_for_python-0.9.0.dist-info/METADATA,sha256=-_Ly32cIPCi5gA1ztRZMsb8ZjkE8_pkp7wWWrGi3eh0,7103
26
+ timer_for_python-0.9.0.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
27
+ timer_for_python-0.9.0.dist-info/top_level.txt,sha256=mbqaJ4u4Wh38RQxq5PFpTOu6h1EcSHjwI2bqdDYCRAI,6
28
+ timer_for_python-0.9.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.44.0)
2
+ Generator: setuptools (75.6.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,14 +0,0 @@
1
- import functools
2
- from collections.abc import Callable
3
- from typing import Any
4
-
5
- from ..model.timer import Timer
6
-
7
-
8
- def benchmark_timer(function: Callable[..., Any]) -> Callable[..., Any]:
9
- @functools.wraps(function)
10
- def wrapper(*args: Any, **kwargs: Any) -> Any:
11
- with Timer(thread=function.__name__):
12
- return function(*args, **kwargs)
13
-
14
- return wrapper