Debian Codegraph @

PKG-INFO

raw
1 Metadata-Version: 2.1
2 Name: codetiming
3 Version: 1.4.0
4 Summary: A flexible, customizable timer for your Python code.
5 Keywords: timer,class,contextmanager,decorator
6 Author-email: Geir Arne Hjelle <[email protected]>, Real Python <[email protected]>
7 Requires-Python: >=3.6
8 Description-Content-Type: text/markdown
9 Classifier: Development Status :: 5 - Production/Stable
10 Classifier: Intended Audience :: Developers
11 Classifier: License :: OSI Approved :: MIT License
12 Classifier: Natural Language :: English
13 Classifier: Operating System :: MacOS
14 Classifier: Operating System :: Microsoft
15 Classifier: Operating System :: POSIX :: Linux
16 Classifier: Programming Language :: Python :: 3.6
17 Classifier: Programming Language :: Python :: 3.7
18 Classifier: Programming Language :: Python :: 3.8
19 Classifier: Programming Language :: Python :: 3.9
20 Classifier: Programming Language :: Python :: 3.10
21 Classifier: Programming Language :: Python :: 3.11
22 Classifier: Topic :: Education
23 Classifier: Topic :: Software Development :: Libraries :: Python Modules
24 Classifier: Topic :: System :: Monitoring
25 Classifier: Typing :: Typed
26 Requires-Dist: dataclasses; python_version < '3.7'
27 Requires-Dist: black ; extra == "dev"
28 Requires-Dist: bump2version ; extra == "dev"
29 Requires-Dist: flake8 ; extra == "dev"
30 Requires-Dist: flit ; extra == "dev"
31 Requires-Dist: interrogate ; extra == "dev"
32 Requires-Dist: isort ; extra == "dev"
33 Requires-Dist: mypy ; extra == "dev"
34 Requires-Dist: black ; extra == "test"
35 Requires-Dist: interrogate ; extra == "test"
36 Requires-Dist: pytest ; extra == "test"
37 Requires-Dist: pytest-cov ; extra == "test"
38 Requires-Dist: tox ; extra == "test"
39 Project-URL: Homepage, https://realpython.com/python-timer
40 Project-URL: Source Code, https://github.com/realpython/codetiming
41 Project-URL: Tutorial, https://realpython.com/python-timer
42 Provides-Extra: dev
43 Provides-Extra: test
44
45 [![Python Timer Functions: Three Ways to Monitor Your Code](https://files.realpython.com/media/Three-Ways-to-Time-Your-Code_Watermarked.8d561fcc7a35.jpg)](https://realpython.com/python-timer)
46
47 # `codetiming` - A flexible, customizable timer for your Python code
48
49 [![Latest version](https://img.shields.io/pypi/v/codetiming)](https://pypi.org/project/codetiming/)
50 [![Python versions](https://img.shields.io/pypi/pyversions/codetiming)](https://pypi.org/project/codetiming/)
51 [![Downloads](https://img.shields.io/pypi/dd/codetiming)](https://pypi.org/project/codetiming/)
52 [![Tests](https://img.shields.io/github/workflow/status/realpython/codetiming/unit_tests?label=tests)](https://github.com/realpython/codetiming/actions)
53 [![Checked with mypy](http://www.mypy-lang.org/static/mypy_badge.svg)](http://mypy-lang.org/)
54 [![Interrogate DocStrings](https://raw.githubusercontent.com/realpython/codetiming/main/interrogate_badge.svg)](https://interrogate.readthedocs.io/)
55 [![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
56 [![MIT license](https://img.shields.io/pypi/l/codetiming)](https://mit-license.org/)
57
58 Install `codetiming` from PyPI:
59
60 ```
61 $ python -m pip install codetiming
62 ```
63
64 The source code is [available on GitHub](https://github.com/realpython/codetiming).
65
66 For a complete tutorial on `codetiming`, see [Python Timer Functions: Three Ways to Monitor Your Code](https://realpython.com/python-timer) on [Real Python](https://realpython.com/).
67
68 ## Basic Usage
69
70 You can use `codetiming.Timer` in several different ways:
71
72 1. As a **class**:
73
74 ```python
75 t = Timer(name="class")
76 t.start()
77 # Do something
78 t.stop()
79 ```
80
81 2. As a **context manager**:
82
83 ```python
84 with Timer(name="context manager"):
85 # Do something
86 ```
87
88 3. As a **decorator**:
89
90 ```python
91 @Timer(name="decorator")
92 def stuff():
93 # Do something
94 ```
95
96
97 ## Arguments
98
99 `Timer` accepts the following arguments when it's created. All arguments are optional:
100
101 - **`name`:** An optional name for your timer
102 - **`text`:** The text that's shown when your timer ends. It should contain a `{}` placeholder that will be filled by the elapsed time in seconds (default: `"Elapsed time: {:.4f} seconds"`)
103 - **`initial_text`:** Show text when your timer starts. You may provide the string to be logged or `True` to show the default text `"Timer {name} started"` (default: `False`)
104 - **`logger`:** A function/callable that takes a string argument and will report the elapsed time when the logger is stopped (default: `print()`)
105
106 You can turn off explicit reporting of the elapsed time by setting `logger=None`.
107
108 In the template text, you can also use explicit attributes to refer to the `name` of the timer or log the elapsed time in `milliseconds`, `seconds` (the default), or `minutes`. For example:
109
110 ```python
111 t1 = Timer(name="NamedTimer", text="{name}: {minutes:.1f} minutes")
112 t2 = Timer(text="Elapsed time: {milliseconds:.0f} ms")
113 ```
114
115 Note that the strings used by `text` are **not** f-strings. Instead, they are used as templates that will be populated using `.format()` behind the scenes. If you want to combine the `text` template with an f-string, you need to use double braces for the template values:
116
117 ```python
118 t = Timer(text=f"{__file__}: {{:.4f}}")
119 ```
120
121 `text` is also allowed to be a callable like a function or a class. If `text` is a callable, it is expected to require one argument: the number of seconds elapsed. It should return a text string that will be logged using logger:
122
123 ```python
124 t = Timer(text=lambda secs: f"{secs / 86400:.0f} days")
125 ```
126
127 This allows you to use third-party libraries like [`humanfriendly`](https://pypi.org/project/humanfriendly/) to do the text formatting:
128
129 ```python
130 from humanfriendly import format_timespan
131
132 t1 = Timer(text=format_timespan)
133 t2 = Timer(text=lambda secs: f"Elapsed time: {format_timespan(secs)}")
134 ```
135
136 You may include a text that should be logged when the timer starts by setting `initial_text`:
137
138 ```python
139 t = Timer(initial_text="And so it begins ...")
140 ```
141
142 You can also set `initial_text=True` to use a default initial text.
143
144
145 ## Capturing the Elapsed Time
146
147 When using `Timer` as a class, you can capture the elapsed time when calling `.stop()`:
148
149 ```python
150 elapsed_time = t.stop()
151 ```
152
153 You can also find the last measured elapsed time in the `.last` attribute. The following code will have the same effect as the previous example:
154
155 ```python
156 t.stop()
157 elapsed_time = t.last
158 ```
159
160
161 ## Named Timers
162
163 Named timers are made available in the class dictionary `Timer.timers`. The elapsed time will accumulate if the same name or same timer is used several times. Consider the following example:
164
165 ```pycon
166 >>> import logging
167 >>> from codetiming import Timer
168
169 >>> t = Timer("example", text="Time spent: {:.2f}", logger=logging.warning)
170
171 >>> t.start()
172 >>> t.stop()
173 WARNING:root:Time spent: 3.58
174 3.5836678670002584
175
176 >>> with t:
177 ... _ = list(range(100_000_000))
178 ...
179 WARNING:root:Time spent: 1.73
180
181 >>> Timer.timers
182 {'example': 5.312697440000193}
183 ```
184
185 The example shows how you can redirect the timer output to the logging module. Note that the elapsed time spent in the two different uses of `t` has been accumulated in `Timer.timers`.
186
187 You can also get simple statistics about your named timers. Continuing from the example above:
188
189 ```pycon
190 >>> Timer.timers.max("example")
191 3.5836678670002584
192
193 >>> Timer.timers.mean("example")
194 2.6563487200000964
195
196 >>> Timer.timers.stdev("example")
197 1.311427314335879
198 ```
199
200 `timers` support `.count()`, `.total()`, `.min()`, `.max()`, `.mean()`, `.median()`, and `.stdev()`.
201
202
203 ## Acknowledgments
204
205 `codetiming` is based on a similar module initially developed for the [Midgard Geodesy library](https://kartverket.github.io/midgard/) at the [Norwegian Mapping Authority](https://www.kartverket.no/en/).