:py:mod:`mlair.helpers.time_tracking` ===================================== .. py:module:: mlair.helpers.time_tracking .. autoapi-nested-parse:: Track time either as decorator or explicit. Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: mlair.helpers.time_tracking.TimeTrackingWrapper mlair.helpers.time_tracking.TimeTracking .. py:class:: 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. .. code-block:: python @TimeTrackingWrapper def sleeper(): print("start") time.sleep(1) print("end") >>> sleeper() start end INFO: foo finished after 00:00:01 (hh:mm:ss) .. py:method:: __call__(self, *args, **kwargs) Start time tracking. .. py:method:: __get__(self, instance, cls) Create bound method object and supply self argument to the decorated method. .. py:class:: TimeTracking(start=True, name='undefined job', logging_level=logging.INFO, log_on_enter=False) Bases: :py:obj:`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 .. code-block:: python 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: .. code-block:: python with TimeTracking(): do_something() The only disadvantage of the latter implementation is, that the duration is logged but not returned. .. py:method:: _start(self) -> None Start time tracking. .. py:method:: _end(self) -> None Stop time tracking. .. py:method:: _duration(self) -> float Get duration in seconds. .. py:method:: __repr__(self) -> str Display current passed time. .. py:method:: run(self) -> None Start time tracking. .. py:method:: 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. :return: duration if enabled or None .. py:method:: duration(self) -> float Return duration in seconds. .. py:method:: __enter__(self) Context manager. .. py:method:: __exit__(self, exc_type, exc_val, exc_tb) -> None Stop time tracking on exit and log info about passed time.