State Persistence
pamiq_core.state_persistence.PersistentStateMixin ¶
Mixin class for objects with persistable state.
This mixin provides the ability to save and load state. Classes that
inherit from this mixin must implement save_state()
and
load_state()
.
save_state ¶
pamiq_core.state_persistence.StateStore ¶
Class to save and load multiple persistable objects at once.
This class saves the state of each registered object to the specified directory. It is also possible to restore the state from the directory.
PARAMETER | DESCRIPTION |
---|---|
states_dir
|
Root path to the directory where states are saved
TYPE:
|
state_name_format
|
Format for the subdirectory name (defaults to timestamp)
TYPE:
|
Source code in src/pamiq_core/state_persistence.py
register ¶
Register a persistable object with a unique name.
PARAMETER | DESCRIPTION |
---|---|
name
|
Unique name to identify the state
TYPE:
|
state
|
Object implementing PersistentStateMixin
TYPE:
|
RAISES | DESCRIPTION |
---|---|
KeyError
|
If |
Source code in src/pamiq_core/state_persistence.py
save_state ¶
Save the all states of registered objects.
RETURNS | DESCRIPTION |
---|---|
Path
|
Path to the directory where the states are saved
TYPE:
|
RAISES | DESCRIPTION |
---|---|
FileExistsError
|
If the directory ( |
Source code in src/pamiq_core/state_persistence.py
load_state ¶
Restores the state from the state_path
directory.
PARAMETER | DESCRIPTION |
---|---|
state_path
|
Path to the directory where the state is saved
TYPE:
|
RAISES | DESCRIPTION |
---|---|
FileNotFoundError
|
If the specified path does not exist |
Source code in src/pamiq_core/state_persistence.py
pamiq_core.state_persistence.PeriodicSaveCondition ¶
Save state condition based on periodic time intervals.
This condition triggers state saving at regular time intervals.
Initializes PeriodicSaveCondition.
PARAMETER | DESCRIPTION |
---|---|
interval
|
Time interval in seconds between state saves.
TYPE:
|
Source code in src/pamiq_core/state_persistence.py
__call__ ¶
Check if interval has elapsed and state should be saved.
pamiq_core.state_persistence.StatesKeeper ¶
Bases: ABC
Abstract base class for managing and cleaning up saved state directories.
This class provides a framework for implementing different state retention
policies. Subclasses must implement the select_removal_states
method to
define which states should be removed during cleanup.
Initialize the StatesKeeper with a logger.
Source code in src/pamiq_core/state_persistence.py
append ¶
Appends the state path from StateStore output.
PARAMETER | DESCRIPTION |
---|---|
path
|
Output of StateStore.save_state() in ControlThread.
TYPE:
|
select_removal_states
abstractmethod
¶
Select which state directories should be removed during cleanup.
This method must be implemented by subclasses to define their specific retention policy.
RETURNS | DESCRIPTION |
---|---|
Iterable[Path]
|
An iterable of Path objects representing state directories to remove. |
Source code in src/pamiq_core/state_persistence.py
cleanup ¶
Remove state directories selected for removal.
Calls select_removal_states
to determine which directories to remove,
then deletes them from the filesystem.
RETURNS | DESCRIPTION |
---|---|
list[Path]
|
List of paths that were removed. |
Source code in src/pamiq_core/state_persistence.py
pamiq_core.state_persistence.LatestStatesKeeper ¶
Bases: StatesKeeper
Keeps a fixed number of most recent state directories by removing older ones.
This class implements a retention policy that keeps only the N most recent state directories based on their modification time.
Initialize the LatestStatesKeeper.
PARAMETER | DESCRIPTION |
---|---|
states_dir
|
Directory where states are stored.
TYPE:
|
max_keep
|
Maximum number of state directories to keep.
TYPE:
|
state_name_pattern
|
Glob pattern to match state directories.
TYPE:
|
Source code in src/pamiq_core/state_persistence.py
select_removal_states ¶
Select state directories to remove based on the retention policy.
RETURNS | DESCRIPTION |
---|---|
Iterable[Path]
|
An iterable of Path objects representing state directories to remove. |