hio.help.timing

hio.help.timing module

Module Contents

exception hio.help.timing.TimerError

Bases: hio.hioing.HioError

Generic Timer Errors. Usage: raise TimerError(“error message”).

exception hio.help.timing.RetroTimerError

Bases: TimerError

Error due to real time being retrograded before start time of timer. Usage: raise RetroTimerError(“error message”).

class hio.help.timing.Timer(duration=0.0, start=None, **kwa)

Bases: hio.hioing.Mixin

Class to manage real elaspsed time using time module.

Attribute notes:
  • ._start is start tyme in seconds

  • ._stop is stop tyme in seconds

Property notes:
  • .duration is float time duration in seconds of timer from ._start to ._stop

  • .elaspsed is float time elasped in seconds since ._start

  • .remaining is float time remaining in seconds until ._stop

  • .expired is boolean, True if expired, False otherwise, i.e. time >= ._stop

Method notes:
  • start() starts timer at current time

  • restart() restarts timer at last ._stop so no time lost

property duration

duration property getter, .duration = ._stop - ._start .duration is float duration tyme

property elapsed

elapsed time property getter, Returns elapsed time in seconds (fractional) since ._start.

property remaining

remaining time property getter, Returns remaining time in seconds (fractional) before ._stop.

property expired

expired property

Returns True if timer has expired, False otherwise. time.time() >= ._stop,

start(duration=None, start=None)

Starts Timer of duration secs at start time start secs. If duration not provided then uses current duration If start not provided then starts at current time.time()

restart(duration=None)

Lossless restart of Timer at start = ._stop for duration if provided, Otherwise current duration. No time lost. Useful to extend Timer so no time lost

class hio.help.timing.MonoTimer(duration=0.0, start=None, retro=True)

Bases: Timer

Class to manage real elaspsed time using time module but with monotonically increating time guarantee in spite of system time being retrograded.

If the system clock is retrograded (moved back in time) while the timer is running then time.time() could move to before the start time. MonoTimer detects this retrograde and if retro is True then retrogrades the start and stop times back Otherwise it raises a TimerRetroError. MonoTimer is not able to detect a prograded clock (moved forward in time)

._start is start time in seconds
._stop  is stop time in seconds
._last is last measured time in seconds with retrograde handling
.retro is boolean If True retrograde ._start and ._stop when time is retrograded.
Properties:

.duration is float time duration in seconds of timer from ._start to ._stop .elaspsed is float time elasped in seconds since ._start .remaining is float time remaining in seconds until ._stop .expired is boolean True if expired, False otherwise, i.e. time >= ._stop .latest is float latest measured time in seconds with retrograte handling

- start

start timer at current time and return start time

- restart

restart timer at last ._stop with no time lost, returns start time

property elapsed

elapsed time property getter, Returns elapsed time in seconds (fractional) since ._start.

property remaining

remaining time property getter, Returns remaining time in seconds (fractional) before ._stop.

property expired

Returns True if timer has expired, False otherwise. .latest >= ._stop,

property latest

latest measured time property getter, Returns latest measured time in seconds adjusted for retrograded system time.

class hio.help.timing.AsyncTimer(duration=0.0, start=None, **kwa)

Bases: Timer

Class to manage real elaspsed time using asyncio event loop time. Namely asyncio.get_event_loop().time()

._start is start tyme in seconds
._stop  is stop tyme in seconds
Properties:

.duration is float time duration in seconds of timer from ._start to ._stop .elaspsed is float time elasped in seconds since ._start .remaining is float time remaining in seconds until ._stop .expired is boolean, True if expired, False otherwise, i.e. time >= ._stop

- start

start timer at current time

- restart

restart timer at last ._stop so no time lost

property duration

duration property getter, .duration = ._stop - ._start .duration is float duration tyme

property elapsed

elapsed time property getter, Returns elapsed time in seconds (fractional) since ._start.

property remaining

remaining time property getter, Returns remaining time in seconds (fractional) before ._stop.

property expired

expired property

Returns True if timer has expired, False otherwise. time.time() >= ._stop,

start(duration=None, start=None)

Starts Timer of duration secs at start time start secs. If duration not provided then uses current duration If start not provided then starts at current time.time()

restart(duration=None)

Lossless restart of Timer at start = ._stop for duration if provided, Otherwise current duration. No time lost. Useful to extend Timer so no time lost

hio.help.timing.nowUTC()

Returns timezone aware datetime of current UTC time Convenience function that allows monkeypatching in tests to mock time

hio.help.timing.nowIso8601()

Returns timezone aware datetime of current UTC time in RFC-3339 profile of ISO 8601 format. Uses now(timezone.utc)

YYYY-MM-DDTHH:MM:SS.ffffff+HH:MM[:SS[.ffffff]] .strftime(‘%Y-%m-%dT%H:%M:%S.%f%z’) ‘2020-08-22T17:50:09.988921+00:00’ Assumes TZ aware For nanosecond use instead attotime or datatime64 in pandas or numpy

hio.help.timing.toIso8601(dt=None)

Returns str datetime dt in RFC-3339 profile of ISO 8601 format. Converts datetime object dt to ISO 8601 formt If dt is missing use now(timezone.utc)

YYYY-MM-DDTHH:MM:SS.ffffff+HH:MM[:SS[.ffffff]] .strftime(‘%Y-%m-%dT%H:%M:%S.%f%z’) ‘2020-08-22T17:50:09.988921+00:00’ Assumes TZ aware For nanosecond use instead attotime or datatime64 in pandas or numpy

hio.help.timing.fromIso8601(dts)

Returns datetime object from RFC-3339 profile of ISO 8601 format str or bytes. Converts dts from ISO 8601 format to datetime object

YYYY-MM-DDTHH:MM:SS.ffffff+HH:MM[:SS[.ffffff]] .strftime(‘%Y-%m-%dT%H:%M:%S.%f%z’) ‘2020-08-22T17:50:09.988921+00:00’ Assumes TZ aware For nanosecond use instead attotime or datatime64 in pandas or numpy