An ultra-simple timer decorator, part of the jazzy utilities series.
Project description
jazzy-timer
Installation:
pip install jazzy-timer
#1 in the "jazzy" utilities series by Edward Jazzhands. Currently the only one in the series. More to come...
A very simple timer that will return a tuple consisting of:
- The result of the function call
- The result of the timer (as a float)
This makes it much easier to format it to whatever you want, or use it for statistics:
- Send the two variables to different outputs (ie. send elapsed time to log output)
- Toggle displaying elapsed time on/off without removing the decorator.
- Compare times with other functions using math operators.
- Store the elapsed time in a database and sort ascending/descending.
- etc
Why?
I found that the other simple timer libraries on PyPI do not work this way, and feel more convulted to use. This version is designed to be ultra-simple, and easier to integrate with other log systems you might be using.
Examples:
Basic usage:
from jazzy-timer import timer
@timer
def func_to_be_timed():
return something
result, elapsed_time = func_to_be_timed() # returns a tuple
print(f"Result of function call: {result}") # use function results as normal
logging.info("Ran func_to_be_timed with elapsed time: %s", elapsed_time) # print elapsed time elsewhere
You can also set the timer precision (default is 4 decimal places). Timer can be used with or without this additional argument:
@timer(precision=2)
def func_to_be_timed():
return something
If the function itself returns a tuple, you'll have a nested tuple:
@timer
def test_function(time_to_sleep):
time.sleep(time_to_sleep)
return "some result", 33
result_tuple, elapsed_time = test_function(0.543)
print(f"Result of function call: {result_tuple} | Elapsed time: {elapsed_time} seconds")
OUTPUT:
Result of function call: ('some result', 33) | Elapsed time: 0.5436 seconds
In this scenario, simply unpack the nested tuple in a separate step.
Thats it! There's nothing else to memorize. There's also no dependencies, it only uses standard library.
All utilities in the "jazzy" series follow the Radical Simplicity design philosophy.
Project details
Release history Release notifications | RSS feed
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
Hashes for jazzy_timer-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08202fcaee2eb24bf4a8fb24914e8f068bfc66152881edd5a5a2a6cc63caaea7 |
|
MD5 | f3420d05019d93338cb3cf6809594b97 |
|
BLAKE2b-256 | ee991eba1b44a58f41b43c1f2ff6f294c42c70005a8bc51f714c062bbbc867a4 |