mmeutils.timer package

Module contents

Code Timing Class by RealPython

This is based on https://realpython.com/python-timer/ with minor modifications.

class mmeutils.timer.Timer(name: str | None = None, logger: Callable[[str], None] | None = None, text: str = 'Elapsed time: {:0.4f} seconds')

Bases: ContextDecorator

Times your code using a class, context manager, or decorator.

Parameters:
  • name (str) – Only required when using multiple timers in parallel.

  • text (str) – Format string for the result message when a timer stops.

  • logger (Callable[[str], None]) – You could set this to print. The callback function to receive the result message when a timer stops.

To summarize, you can use Timer in three different ways:

As a class:

t = Timer(name="class")
t.start()
# Do something
t.stop()

As a context manager:

with Timer(name="context manager"):
    # Do something

As a decorator:

@Timer(name="decorator")
def stuff():
    # Do something

This kind of Python timer is mainly useful for monitoring the time that your code spends at individual key code blocks or functions.

logger: Callable[[str], None] | None = None

The callback function receiving the result message

name: str | None = None

Name of the timer when using multiple timers

start() None

Starts a new timer.

stop() float

Stops the timer and reports the elapsed time.

text: str = 'Elapsed time: {:0.4f} seconds'

The format string for the result message

timers: ClassVar[Dict[str, float]] = {}

A class dictionary of named timers that can run in parallel

exception mmeutils.timer.TimerError

Bases: Exception

A custom exception type used to report errors within the Timer class.