psychos.core.Interval#

class psychos.core.Interval(duration: float, on_overtime: Literal['ignore', 'warning', 'exception'] = 'warning', sleep_interval: float = 0.8, hog_period: float = 0.02, start_time: float | None = None)[source]#

Bases: object

A class to handle time intervals, including support for context management to ensure precise timing and handling of remaining time.

Parameters:
  • duration (float) – The duration of the interval in seconds.

  • on_overtime (Literal["ignore", "warning", "exception"], default="warning") – Specifies what to do if the elapsed time exceeds the duration: - “ignore”: Do nothing. - “warning”: Raise a warning if the interval is exceeded. - “exception”: Raise an exception if the interval is exceeded.

  • sleep_interval (float, default=0.8) – The sleep interval for how long the function sleeps in the wait period. This controls the frequency of event dispatching.

  • hog_period (float, default=0.02) – The hog period is the duration in the final part of the wait where continuous checking is done for more precise timing.

  • start_time (Optional[float], default=None) – If provided, the interval will use this as the start time, otherwise it will default to the current time using time().

  • usage (Example)

  • -------------

  • interval (>>> interval.wait() # Wait for the remaining time of the)

  • Interval(5) (>>> with)

  • ... (>>> # Do some more work)

  • interval

  • timing (>>> # Using the class in a context manager to ensure)

  • ...

  • seconds (>>> interval.wait() # Wait for the remaining time to reach 5)

  • timing

  • Interval(5)

  • time.sleep(3) (>>>)

  • time (>>> # Exiting the 'with' block will automatically wait for the remaining)

__init__(duration: float, on_overtime: Literal['ignore', 'warning', 'exception'] = 'warning', sleep_interval: float = 0.8, hog_period: float = 0.02, start_time: float | None = None)[source]#

Methods

__init__(duration[, on_overtime, ...])

remaining()

Get the remaining time of the interval.

reset()

Reset the start time to the current timestamp.

wait()

Wait for the remaining time of the interval.

remaining() float[source]#

Get the remaining time of the interval.

Returns:

The remaining time in seconds.

Return type:

float

reset() None[source]#

Reset the start time to the current timestamp.

wait() None[source]#

Wait for the remaining time of the interval. If the interval has already passed, handle it based on the on_overtime parameter.

Raises:
  • RuntimeError: – If on_overtime is set to “exception” and the interval has already passed.

  • Warning: – If on_overtime is set to “warning” and the interval has already passed.