Contextmanager to print or log execution time of code blocks
Project description
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)
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file supertimer-0.4.1.tar.gz.
File metadata
- Download URL: supertimer-0.4.1.tar.gz
- Upload date:
- Size: 18.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
607d6557c2f785452257a290825bc01d10c6fabdd91dd6a4ebe429b09fcd7672
|
|
| MD5 |
d8b9b4973642b58311ebee19b7c71dbc
|
|
| BLAKE2b-256 |
a815dbf931e19060f3575c0467312690017b616af4ae008ec86a1db0cd786a6c
|
File details
Details for the file supertimer-0.4.1-py3-none-any.whl.
File metadata
- Download URL: supertimer-0.4.1-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21ff1bf7040463dda46f10d8d1c540d4a5b6c6d258dbd5d0c686b748a582a405
|
|
| MD5 |
187e586c529d3b4a7b8e8d3127c55ffe
|
|
| BLAKE2b-256 |
d89cf03bbb9e9425488fad63844ea8b35e5d88947d47a5c0f1bc87757f795629
|