Time
pamiq_core.time ¶
PAMIQ-Core Time Module.
This module provides a custom implementation of time-related functions with time acceleration and pause/resume features. It wraps the standard Python time module and allows for consistent time control across the system.
Key features
- Time acceleration: Adjust the speed of time passage in the system.
- Pause/Resume: Ability to pause and resume the flow of time in the system.
- Thread-safe: All operations are protected by locks for use in multi-threaded environments.
- Compatible API: Provides familiar time functions like sleep, time, perf_counter, and monotonic.
- Original time functions: Provides access to the original time functions with 'fixed_' prefix.
Usage
from pamiq_core import time
# Get current time (affected by time scale and pause)
current_time = time.time()
# Get current time (not affected by time scale or pause)
fixed_current_time = time.fixed_time()
# Sleep for 1 second (affected by time scale and pause)
time.sleep(1)
# Sleep for 1 second (not affected by time scale or pause)
time.fixed_sleep(1)
# Set time scale (e.g., 2x speed)
time.set_time_scale(2.0)
# Get current time scale
current_scale = time.get_time_scale()
# Pause time
time.pause()
# Resume time
time.resume()
Note: This module is designed for use within the system and may not be suitable for general-purpose time management.
TimeController ¶
Bases: PersistentStateMixin
Source code in src/pamiq_core/time.py
TimeControllerState ¶
Bases: TypedDict
Time controller state for restarting the system.
is_paused ¶
time ¶
Return the current time in seconds since the epoch.
This function is affected by the current time scale and pause state.
RETURNS | DESCRIPTION |
---|---|
float
|
The current time in seconds since the epoch.
TYPE:
|
Source code in src/pamiq_core/time.py
perf_counter ¶
Return the value (in fractional seconds) of a performance counter.
This function is affected by the current time scale and pause state.
RETURNS | DESCRIPTION |
---|---|
float
|
The current value of the performance counter.
TYPE:
|
Source code in src/pamiq_core/time.py
monotonic ¶
Return the value (in fractional seconds) of a monotonic time counter.
This function is affected by the current time scale and pause state.
RETURNS | DESCRIPTION |
---|---|
float
|
The current value of the monotonic time counter.
TYPE:
|
Source code in src/pamiq_core/time.py
set_time_scale ¶
Set the time scale for the system.
PARAMETER | DESCRIPTION |
---|---|
time_scale
|
The new time scale. Must be greater than 0.
TYPE:
|
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If time_scale is not greater than 0. |
Source code in src/pamiq_core/time.py
get_time_scale ¶
Get the current time scale of the system.
RETURNS | DESCRIPTION |
---|---|
float
|
The current time scale.
TYPE:
|
sleep ¶
Suspend execution for the given number of seconds.
This function is affected by the current time scale and pause state.
PARAMETER | DESCRIPTION |
---|---|
secs
|
The number of seconds to sleep.
TYPE:
|
Source code in src/pamiq_core/time.py
pause ¶
resume ¶
state_dict ¶
Return the time controller state.
RETURNS | DESCRIPTION |
---|---|
TimeControllerState
|
State information that can reproduce the current time flow progression of the system.
TYPE:
|
Source code in src/pamiq_core/time.py
load_state_dict ¶
Loads states.
When a state is loaded, the system time starts from the time the state was retrieved.
PARAMETER | DESCRIPTION |
---|---|
state_dict
|
The dict which contains state values.
TYPE:
|
Source code in src/pamiq_core/time.py
save_state ¶
Save the current TimeController state to disk.
PARAMETER | DESCRIPTION |
---|---|
path
|
Base path where the state will be saved. Will be appended with .pkl extension.
TYPE:
|
Source code in src/pamiq_core/time.py
load_state ¶
Load TimeController state from disk.
PARAMETER | DESCRIPTION |
---|---|
path
|
Base path where the state was previously saved. Will be appended with .pkl extension.
TYPE:
|
Source code in src/pamiq_core/time.py
get_global_time_controller ¶
Retrieves the global instance of TimeController.
RETURNS | DESCRIPTION |
---|---|
TimeController
|
Global instance of TimeController |