Timer name in printed message
mlisovyi opened this issue · comments
It would be good to be able to have the timer name in the printed message. Either by default or have some special parameter or special formatting parameter. At the moment printouts from multiple timers can be confusing.
codetiming==1.1.0 from pypi
@mlisovyi Thanks for the suggestion.
This sounds like a good idea. We just need to be a bit careful, so we don't break backwards compatibility, since currently the formatting of text
expects only one parameter.
I think it should be quite doable though, by implementing a FormattedTimer
that by default formats like the usual float, but that we can attach custom properties to.
I'll have a look at implementing this in the coming days.
By the way, a somewhat cumbersome workaround is to add the name manually to text
when initializing the timer:
t = Timer(name=name, text=f"{name} finished in {{:.2f}} seconds")
@mlisovyi Thanks again for the suggestion.
I've just published v1.2.0 with support for attributes. With this latest version, you will be able to do things like:
from codetiming import Timer
with Timer(name="NamedTimer", text="{name}: {seconds:.2f} seconds"):
...
Currently, the attributes name
, milliseconds
, seconds
, and minutes
are made available.
Awesome thanks!
The proposed workarount in the previous version has a downside, that i have to add text string explicitly into the timer. in my used case, i had several timers in the script to track performance of different code blocks. And what i wanted to do is
timer_arg = {'logger': log.info, 'text': 'Some formatted string'}
...
with Timer('Timer_1', **timer_args):
...
with Timer('Timer_2', **timer_args):
...
This way i do not need to adjust the text string in multiple places. So I'm very happy to try out the new released version
The nw release works like a charm! Thanks for prompt update.
Great! Happy to hear that 🎉
Thanks for checking back in with the feedback! 😄