codetiming icon indicating copy to clipboard operation
codetiming copied to clipboard

Include {name} in default format string

Open Evidlo opened this issue 2 years ago • 0 comments

A small quality-of-life improvement would be if the default format string included the name attribute by default.

>>> t = Timer('foobar')
>>> t.start()
>>> time.sleep(1)
>>> t.stop()
foobar elapsed time: 1.000 seconds
>>> t = Timer()
>>> t.start()
>>> time.sleep(1)
>>> t.stop()
Elapsed time: 1.000 seconds

This might be achieved with the following __post_init__ function:

    def __post_init__(self):
        if self.name is None:
            self.text = "Elapsed time: {:0.4f} seconds"
        else:
            self.text = "{name} elapsed time {:0.4f} seconds"

Alternatively, name could just have a non-None default value (e.g. "Timer" or "Elapsed").

Evidlo avatar Feb 21 '24 02:02 Evidlo