mlair.helpers.time_tracking

Track time either as decorator or explicit.

Module Contents

Classes

TimeTrackingWrapper

Wrapper implementation of TimeTracking class.

TimeTracking

Track time to measure execution time.

class mlair.helpers.time_tracking.TimeTrackingWrapper(func)

Wrapper implementation of TimeTracking class.

Use this implementation easily as decorator for functions, classes and class methods. Implement a custom function and decorate it for automatic time measure.

@TimeTrackingWrapper
def sleeper():
    print("start")
    time.sleep(1)
    print("end")

>>> sleeper()
start
end
INFO: foo finished after 00:00:01 (hh:mm:ss)
__call__(self, *args, **kwargs)

Start time tracking.

__get__(self, instance, cls)

Create bound method object and supply self argument to the decorated method.

class mlair.helpers.time_tracking.TimeTracking(start=True, name='undefined job', logging_level=logging.INFO, log_on_enter=False)

Bases: object

Track time to measure execution time.

Time tracking automatically starts on initialisation and ends by calling stop method. Duration can always be shown by printing the time tracking object or calling get_current_duration. It is possible to start and stop time tracking by hand like

time = TimeTracking(start=True)  # start=True is default and not required to set
do_something()
time.stop(get_duration=True)

A more comfortable way is to use TimeTracking in a with statement like:

with TimeTracking():
    do_something()

The only disadvantage of the latter implementation is, that the duration is logged but not returned.

_start(self)None

Start time tracking.

_end(self)None

Stop time tracking.

_duration(self)float

Get duration in seconds.

__repr__(self)str

Display current passed time.

run(self)None

Start time tracking.

stop(self, get_duration=False) → Optional[float]

Stop time tracking.

Will raise an error if time tracking was already stopped. :param get_duration: return passed time if enabled.

Returns

duration if enabled or None

duration(self)float

Return duration in seconds.

__enter__(self)

Context manager.

__exit__(self, exc_type, exc_val, exc_tb)None

Stop time tracking on exit and log info about passed time.