Name | timering JSON |
Version |
1.0.2
JSON |
| download |
home_page | |
Summary | The only timing library you'll ever need. |
upload_time | 2023-06-06 18:51:42 |
maintainer | |
docs_url | None |
author | ruzickbelle |
requires_python | >=3.8 |
license | BSD 3-Clause License Copyright (c) 2023, ruzickbelle Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
keywords |
timer
timing
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# timing
**timing** is a Python timing library providing timer functionality.
This library is one of many providing timers, but aims to:
* incorporate all patterns for timing execution times
* of functions and code blocks in any situation
* while being minimalistic in complexity but powerful nonetheless.
Some of the library's features are:
* It is compatible with Python 3.8+,
* fully typed, allowing your IDE to provide useful completions,
* tested using `pytest` and
* well documented by docstrings
Example program:
```python
import time
from timing import Timer
timer = Timer(callback=lambda x: print(f"Took {x:.2f} seconds."))
with timer:
print("Executing step 1...")
time.sleep(1)
with timer:
print("Executing step 2...")
time.sleep(2)
```
Output:
```
Executing step 1...
Took 1.00 seconds.
Executing step 2...
Took 2.00 seconds.
```
## Installation
**timing** can be installed using
```bash
$ python -m pip install timering
```
Note that on some systems, the Python 3 executable is called `python3` instead.
## [Release Notes](https://github.com/ruzickbelle/python-timing/blob/main/CHANGELOG.md)
## Supported Patterns
### Start and Stop
Create a timer, start and stop it:
```python
>>> from timing import Timer
>>> timer = Timer()
>>> timer.start()
>>> timer.stop()
2.442877164
```
Note that `timing.start()` creates a new `Timer`, starts and returns it directly.
### Context
Create a timer and measure the execution duration of a context:
```python
>>> with timer:
... time.sleep(1)
...
>>> timer.get()
1.001198509
```
### Measure Functions
Create a timer and measure a given function:
```python
>>> import time
>>> timer.measure(lambda: time.sleep(2))
>>> timer.get()
2.002377642
```
Note that `timing.measure()` creates a new `Timer` and measures the given function with it. To retrieve the result, pass
a `callback` to `timing.measure()` or use `timing.prevtimer.get()` if you are certain it wasn't replaced since the start
of your `timing.measure()` call.
### Decorator Pattern / Wrapping Functions
Create a timer and wrap a function to measure its execution duration:
```python
>>> timed_func = timer.wrap(lambda: time.sleep(3))
>>> timed_func()
>>> timer.get()
3.003535439
>>> # alternatively
>>> @timer.wrap
... def timed_func():
... time.sleep(2)
...
>>> timed_func()
>>> timer.get()
2.002481228
```
Note that there is also a `timing.wrap()` method available.
### Decorator Generator Pattern
To use Python's `@decorator` pattern with arguments, libraries often provide functionality for generating decorators:
```python
>>> @timer.wrap()
... def timed_func():
... time.sleep(1)
...
>>> timed_func()
>>> timer.get()
1.001287624
```
Note that there is also a `timing.wrap()` method available.
## Configuration
The `Timer` constructor accepts a `unit` used for the returned results and a `callback` called with the result everytime
the timer stops. Every method also accepts the applicable arguments to override them once.
**Be careful** when calling `stop()` with a `unit` when a `callback` is defined as the `callback` will be called with
the result in the new unit and not the one given to the constructor.
## Contributing / Feedback
I happily accept feedback and pull requests.
Some ideas:
* Should I add support for registering multiple callbacks with different units?
Raw data
{
"_id": null,
"home_page": "",
"name": "timering",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "timer,timing",
"author": "ruzickbelle",
"author_email": "",
"download_url": "https://files.pythonhosted.org/packages/a2/03/e37d515698ae7f3582c81dd1246f0cbb6f58c96bff5e340a6be0b3244190/timering-1.0.2.tar.gz",
"platform": null,
"description": "# timing\n**timing** is a Python timing library providing timer functionality.\n\nThis library is one of many providing timers, but aims to:\n* incorporate all patterns for timing execution times\n* of functions and code blocks in any situation\n* while being minimalistic in complexity but powerful nonetheless.\n\nSome of the library's features are:\n* It is compatible with Python 3.8+,\n* fully typed, allowing your IDE to provide useful completions,\n* tested using `pytest` and\n* well documented by docstrings\n\nExample program:\n```python\nimport time\nfrom timing import Timer\n\ntimer = Timer(callback=lambda x: print(f\"Took {x:.2f} seconds.\"))\nwith timer:\n print(\"Executing step 1...\")\n time.sleep(1)\nwith timer:\n print(\"Executing step 2...\")\n time.sleep(2)\n```\nOutput:\n```\nExecuting step 1...\nTook 1.00 seconds.\nExecuting step 2...\nTook 2.00 seconds.\n```\n\n\n## Installation\n\n**timing** can be installed using\n```bash\n$ python -m pip install timering\n```\n\nNote that on some systems, the Python 3 executable is called `python3` instead.\n\n\n## [Release Notes](https://github.com/ruzickbelle/python-timing/blob/main/CHANGELOG.md)\n\n\n## Supported Patterns\n\n### Start and Stop\n\nCreate a timer, start and stop it:\n\n```python\n>>> from timing import Timer\n>>> timer = Timer()\n>>> timer.start()\n>>> timer.stop()\n2.442877164\n```\n\nNote that `timing.start()` creates a new `Timer`, starts and returns it directly.\n\n\n### Context\n\nCreate a timer and measure the execution duration of a context:\n\n```python\n>>> with timer:\n... time.sleep(1)\n...\n>>> timer.get()\n1.001198509\n```\n\n\n### Measure Functions\n\nCreate a timer and measure a given function:\n\n```python\n>>> import time\n>>> timer.measure(lambda: time.sleep(2))\n>>> timer.get()\n2.002377642\n```\n\nNote that `timing.measure()` creates a new `Timer` and measures the given function with it. To retrieve the result, pass\na `callback` to `timing.measure()` or use `timing.prevtimer.get()` if you are certain it wasn't replaced since the start\nof your `timing.measure()` call.\n\n\n### Decorator Pattern / Wrapping Functions\n\nCreate a timer and wrap a function to measure its execution duration:\n\n```python\n>>> timed_func = timer.wrap(lambda: time.sleep(3))\n>>> timed_func()\n>>> timer.get()\n3.003535439\n>>> # alternatively\n>>> @timer.wrap\n... def timed_func():\n... time.sleep(2)\n...\n>>> timed_func()\n>>> timer.get()\n2.002481228\n```\n\nNote that there is also a `timing.wrap()` method available.\n\n\n### Decorator Generator Pattern\n\nTo use Python's `@decorator` pattern with arguments, libraries often provide functionality for generating decorators:\n\n```python\n>>> @timer.wrap()\n... def timed_func():\n... time.sleep(1)\n...\n>>> timed_func()\n>>> timer.get()\n1.001287624\n```\n\nNote that there is also a `timing.wrap()` method available.\n\n\n## Configuration\n\nThe `Timer` constructor accepts a `unit` used for the returned results and a `callback` called with the result everytime\nthe timer stops. Every method also accepts the applicable arguments to override them once.\n\n**Be careful** when calling `stop()` with a `unit` when a `callback` is defined as the `callback` will be called with\nthe result in the new unit and not the one given to the constructor.\n\n\n## Contributing / Feedback\n\nI happily accept feedback and pull requests.\n\nSome ideas:\n* Should I add support for registering multiple callbacks with different units?\n",
"bugtrack_url": null,
"license": "BSD 3-Clause License Copyright (c) 2023, ruzickbelle Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ",
"summary": "The only timing library you'll ever need.",
"version": "1.0.2",
"project_urls": {
"Changelog": "https://github.com/ruzickbelle/python-timing/blob/main/CHANGELOG.md",
"Repository": "https://github.com/ruzickbelle/python-timing"
},
"split_keywords": [
"timer",
"timing"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "23621a61aaa95773173c817e6bf054c7532a28a3d0c791494509278bbfb80590",
"md5": "f832a8878b5c4e606d4ceeff2d429831",
"sha256": "12e34d1bd832b7b8a8d29e3a772161f630776c3b40d56c73052030993bd3b8dd"
},
"downloads": -1,
"filename": "timering-1.0.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "f832a8878b5c4e606d4ceeff2d429831",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 7075,
"upload_time": "2023-06-06T18:51:40",
"upload_time_iso_8601": "2023-06-06T18:51:40.958893Z",
"url": "https://files.pythonhosted.org/packages/23/62/1a61aaa95773173c817e6bf054c7532a28a3d0c791494509278bbfb80590/timering-1.0.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "a203e37d515698ae7f3582c81dd1246f0cbb6f58c96bff5e340a6be0b3244190",
"md5": "11db484e2ac77f89b29f63f66da7079f",
"sha256": "78b0d0f243dcf3f82b1745d7ce5844011dc68ef275e60282e43490458ad4e730"
},
"downloads": -1,
"filename": "timering-1.0.2.tar.gz",
"has_sig": false,
"md5_digest": "11db484e2ac77f89b29f63f66da7079f",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 6815,
"upload_time": "2023-06-06T18:51:42",
"upload_time_iso_8601": "2023-06-06T18:51:42.346008Z",
"url": "https://files.pythonhosted.org/packages/a2/03/e37d515698ae7f3582c81dd1246f0cbb6f58c96bff5e340a6be0b3244190/timering-1.0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-06 18:51:42",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ruzickbelle",
"github_project": "python-timing",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "timering"
}