aiodec
Project description
aiodec
Decorators for asyncio
astopwatch
The astopwatch decorator is used in the following way:
from aiodec import astopwatch
@astopwatch
async def blah(x, y):
return x + y
What does it do? This simple decorator will emit logs with the following message:
INFO:aiodec:Time taken: 0.0003 seconds
Not terribly special. Yet. You can also customize the log message:
from aiodec import astopwatch
@astopwatch(message_template='Time cost was $time_ sec', fmt='%.1g')
async def blah(x, y):
return x + y
This outputs log messages with the following message:
INFO:aiodec:Time cost was 3e-4 sec
Two things: first, the template parameter used for the time cost is called $time_; second, you can customize the formatting of the seconds value. However, it can also do something a lot more interesting: it can include parameters from the wrapped function in the message:
from aiodec import astopwatch
@astopwatch(message_template='x=$x y=$y | $time_ seconds')
async def blah(x, y=2):
return x + y
loop.run_until_complete(blah(1))
This outputs log messages with the following message:
INFO:aiodec:x=1 y=2 | 0.0003 seconds
Magic! Note that positional args and keyword args and default values are all handled correctly.
As you saw earlier, in addition to the function parameters, the special $time_ parameter will also be available. The other extra fields are:
$name_, which contains the __name__ of the wrapped function, and
$qualname_, which contains the __qualname__ of the wrapped function.
These three template parameters have a trailing underscore, to avoid collisions with any parameter names.
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
Hashes for aiodec-2018.8.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63283768d4222c542c87a87b22c1f54d8011c3abb4a082d14e1f406857c6326b |
|
MD5 | ee32bf301390331be751cb204e54a93f |
|
BLAKE2b-256 | 80715511d69374d0dac48d0f9cd5733fc18c50cc9bda786d627b2a2ec1d54252 |