pickle_wrap¶
Lazy (de)serialization wrapper for objects whose modules may not be loaded.
This module provides :func:wrap and :func:unwrap to convert non-serializable
module objects into :class:PickleWrap instances that defer actual loading
until the target module is available. Useful for delaying deserialization of
objects from optional dependencies.
PickleWrap
¶
A lazily-loaded pickle container for objects from unloaded modules.
Source code in py_misc_utils/pickle_wrap.py
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 193 194 195 196 197 | |
wrapped_class()
¶
Return the qualified class name of the wrapped object.
Returns:
| Type | Description |
|---|---|
|
A string like 'module.ClassName'. |
Source code in py_misc_utils/pickle_wrap.py
180 181 182 183 184 185 186 | |
load(pickle_module=pickle)
¶
Deserialize the wrapped object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pickle_module
|
The pickle module used for deserialization. |
pickle
|
Returns:
| Type | Description |
|---|---|
|
The deserialized object. |
Source code in py_misc_utils/pickle_wrap.py
188 189 190 191 192 193 194 195 196 197 | |
add_known_module(modname)
¶
Register a module as known (i.e. no wrapping needed for its objects).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
modname
|
The module name to add (its root module is registered). |
required |
Source code in py_misc_utils/pickle_wrap.py
25 26 27 28 29 30 31 | |
wrap(obj, pickle_module=pickle)
¶
Recursively wrap objects whose modules are not yet known/loaded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
The object to wrap (lists, dicts, and arbitrary objects are traversed). |
required | |
pickle_module
|
The pickle module to use for serialization. |
pickle
|
Returns:
| Type | Description |
|---|---|
|
The wrapped object tree, with PickleWrap instances where needed. |
Source code in py_misc_utils/pickle_wrap.py
139 140 141 142 143 144 145 146 147 148 149 | |
unwrap(obj, pickle_module=pickle)
¶
Recursively unwrap PickleWrap instances by loading their wrapped data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
The object tree containing PickleWrap instances. |
required | |
pickle_module
|
The pickle module to use for deserialization. |
pickle
|
Returns:
| Type | Description |
|---|---|
|
The unwrapped object tree, with loaded objects replacing PickleWrap. |
Source code in py_misc_utils/pickle_wrap.py
152 153 154 155 156 157 158 159 160 161 162 | |