supertimer
Contextmanager to print or log execution time of code blocks
Original url: https://github.com/mariushelf/supertimer
Etymology
This package provides a timer. But the name timer was already taken.
So I needed a new name. Inspired by my recently freshly flamed up
love for the good old Super Nintendo, I thought that this timer could
as well be super.
Usage
Use as a context manager
To log the duration of a code block:
from supertimer import timer
import time
with timer("Sleeping a bit"):
time.sleep(2)
This will log:
Sleeping a bit starting at 2020-12-14 18:34:54.403371
Sleeping a bit finished successfully at 2020-12-14 18:34:56.404208 after 0:00:02.000837.
Use as a decorator
from supertimer import timer
import time
@timer("Sleeping a bit")
def sleep_a_bit():
time.sleep(2)
sleep_a_bit()
This will log the same message as the context manager each time the decorated function is called:
Sleeping a bit starting at 2020-12-14 18:34:54.403371
Sleeping a bit finished successfully at 2020-12-14 18:34:56.404208 after 0:00:02.000837.
Configuring the output method
By default, the output is logged at loglevel DEBUG.
The loglevel can be changed with the loglevel parameter. Printing to stdout can be
activated by setting the print parameter to True. Logging can be disabled by
setting log to False:
with timer(loglevel=logging.INFO):
# logging at loglevel INFO, no printing
...
with timer(print=True, log=False):
# just printing, no logging
...
Changing the logger
The logger can be configured:
import logging
logger = logging.getLogger("my.custom.logger")
with timer(logger=logger):
do_something()
If no logger is provided, a logger named supertimer is used.
Convenience classes
There are convenience classes which are preconfigured for a certain loglevel or just printing:
print_timerdebug_timerinfo_timer
Configuring defaults
All constructor arguments have a default_.* class attribute counterpart which
specify defaults in case the arguments are omitted.
For example, to change the default loglevel to WARNING one could do:
timer.default_loglevel = logging.WARNING
with timer("Sleep warning"):
# log timings with loglevel `WARNING`
time.sleep(2)
with timer("Sleep debug", loglevel=logging.DEBUG):
# log timings with loglevel `DEBUG`
time.sleep(2)
How time is measured
By default, the start and end time are taken with datetime.dateime.now. The duration
is calculated as the difference of start and end time, resulting in a
datetime.timedelta object.
The timer function can be overridden:
import timeit
with timer(timer_func=timeit.default_timer):
...
The timer_func parameter expects a callable that returns a value which supports the
minus operation when called without an argument.
License
Changelog
0.4.0
- timer can now be used as a decorator
- global default configuration
- additional
logparameter - documentation
- change name of default logger to
supertimer
0.3.0
- convenience classes
print_timer,debug_timerandinfo_timer - make timer function configurable
0.2.0
- mention success or error after execution
0.1.0
- First release
Author
Marius Helf (helfsmarius@gmail.com)
Metadata
Release files for supertimer 0.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| supertimer-0.4.1.tar.gz | 18.8 kB | Without provenance | Details for supertimer-0.4.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| supertimer-0.4.1-py3-none-any.whl | Python 3 | none | any | Details for supertimer-0.4.1-py3-none-any.whl |
Total release size: 23.7 kB
Release files / supertimer-0.4.1.tar.gz
| Download URL | supertimer-0.4.1.tar.gz |
|---|---|
| Size | 18.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
607d6557c2f785452257a290825bc01d10c6fabdd91dd6a4ebe429b09fcd7672
|
|
BLAKE2b-256 checksum How to use checksums |
a815dbf931e19060f3575c0467312690017b616af4ae008ec86a1db0cd786a6c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Release files / supertimer-0.4.1-py3-none-any.whl
| Download URL | supertimer-0.4.1-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
21ff1bf7040463dda46f10d8d1c540d4a5b6c6d258dbd5d0c686b748a582a405
|
|
BLAKE2b-256 checksum How to use checksums |
d89cf03bbb9e9425488fad63844ea8b35e5d88947d47a5c0f1bc87757f795629
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Security for supertimer 0.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.