state¶
Serializable object state persistence utilities.
This module provides the :class:StateBase mixin which allows subclasses to
save/restore their state via _get_state/_set_state hooks, plus :func:to_state
and :func:from_state functions for pickling state to files. Subclasses can
contribute additional state via _store_state/_load_state keyed by class.
Example
import os
import tempfile
import py_misc_utils.state as st
class Session(st.StateBase):
def __init__(self, user):
self.user = user
tdir = tempfile.mkdtemp()
st.to_state(Session('alice'), os.path.join(tdir, 'session.state'))
sess = st.from_state(Session, os.path.join(tdir, 'session.state'))
assert sess.user == 'alice'
StateBase
¶
Mixin providing pickle-based state save/restore for subclasses.
Subclasses override _get_state/_set_state to customize serialization and use _store_state/_load_state to contribute class-namespaced extra state.
Example
import py_misc_utils.state as st
class Session(st.StateBase):
def __init__(self, user, token):
self.user = user
self.token = token
def _get_state(self, state):
state['user'] = self.user
state['token_len'] = len(self.token)
return state
def _set_state(self, state):
self.user = state['user']
self.token = '*' * state['token_len']
sess = Session('alice', 'secret')
state = sess._get_state({})
assert state == {'user': 'alice', 'token_len': 6}, state
sess2 = Session.__new__(Session)
sess2._set_state(state)
assert sess2.user == 'alice' and sess2.token == '******'
Note
State entries contributed via _store_state live in the SB_STATE instance attribute, which is not part of the state dictionary returned by the default _get_state. For such entries to be persisted, either call _store_state before to_state() serializes the object, or override _get_state to merge the SB_STATE contents (e.g. with state.update(self.dict)) into the returned dictionary.
Source code in py_misc_utils/state.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | |
to_state(obj, path)
¶
Persist an object's state to a file via pickle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
The StateBase-derived object to serialize. |
required | |
path
|
Filesystem path to write the state to. |
required |
Example
import os
import tempfile
import py_misc_utils.state as st
class Session(st.StateBase):
def __init__(self, user, retries=3):
self.user = user
self.retries = retries
tdir = tempfile.mkdtemp()
st.to_state(Session('bob'), os.path.join(tdir, 'sess.state'))
assert st.from_state(Session, os.path.join(tdir, 'sess.state')).user == 'bob'
Source code in py_misc_utils/state.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
from_state(cls, path, **kwargs)
¶
Restore an object of cls from persisted state, overriding with kwargs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cls
|
The class to instantiate. |
required | |
path
|
Filesystem path to read the state from. |
required | |
**kwargs
|
Overrides applied to the loaded state before restore. |
{}
|
Returns:
| Type | Description |
|---|---|
|
A new instance of cls with its state restored. |
Example
import os
import tempfile
import py_misc_utils.state as st
class Session(st.StateBase):
def __init__(self, user, retries=3):
self.user = user
self.retries = retries
tdir = tempfile.mkdtemp()
st.to_state(Session('bob', retries=5), os.path.join(tdir, 'sess.state'))
# kwargs override the persisted top-level state entries.
sess = st.from_state(Session, os.path.join(tdir, 'sess.state'), retries=1)
assert (sess.user, sess.retries) == ('bob', 1), (sess.user, sess.retries)
Source code in py_misc_utils/state.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |