ubelt.util_time module

This is util_time, it contains functions for handling time related code that I wish there was standard library support for. Currently there is only one function.

The timestamp() is less interesting than the previous two methods, but I have found it useful to have a function that quickly returns an iso8601 timestamp without much fuss.

Timerit is gone! Use the standalone and separate module timerit.Timerit But the But the :class:`ubelt.util_timer.Timer still exists here.

ubelt.util_time.timestamp(method='iso8601')[source]

Make an iso8601 timestamp suitable for use in filenames

Parameters

method (str, default=’iso8601’) – type of timestamp

Returns

stamp

Return type

str

Example

>>> import ubelt as ub
>>> stamp = ub.timestamp()
>>> print('stamp = {!r}'.format(stamp))
stamp = ...-...-...T...
class ubelt.util_time.Timer(label='', verbose=None, newline=True)[source]

Bases: object

Measures time elapsed between a start and end point. Can be used as a with-statement context manager, or using the tic/toc api.

Parameters
  • label (str, default=’’) – identifier for printing

  • verbose (int, default=None) – verbosity flag, defaults to True if label is given

  • newline (bool, default=True) – if False and verbose, print tic and toc on the same line

Variables
  • ~Timer.elapsed (float) – number of seconds measured by the context manager

  • ~Timer.tstart (float) – time of last tic reported by self._time()

Example

>>> # Create and start the timer using the context manager
>>> import math
>>> timer = Timer('Timer test!', verbose=1)
>>> with timer:
>>>     math.factorial(10)
>>> assert timer.elapsed > 0
tic('Timer test!')
...toc('Timer test!')=...

Example

>>> # Create and start the timer using the tic/toc interface
>>> timer = Timer().tic()
>>> elapsed1 = timer.toc()
>>> elapsed2 = timer.toc()
>>> elapsed3 = timer.toc()
>>> assert elapsed1 <= elapsed2
>>> assert elapsed2 <= elapsed3
tic()[source]

starts the timer

toc()[source]

stops the timer