Skip to content

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
class PickleWrap:
  """A lazily-loaded pickle container for objects from unloaded modules."""

  # Pickled qualified class name and data of the wrapped object.

  def __init__(self, obj, pickle_module=pickle):
    """Initialize the PickleWrap with an object and pickle module.

    Args:
      obj: The object to wrap.
      pickle_module: The pickle module used for serialization.
    """
    self._class = iu.qual_name(obj)
    self._data = pickle_module.dumps(obj)

  def wrapped_class(self):
    """Return the qualified class name of the wrapped object.

    Returns:
      A string like 'module.ClassName'.
    """
    return self._class

  def load(self, pickle_module=pickle):
    """Deserialize the wrapped object.

    Args:
      pickle_module: The pickle module used for deserialization.

    Returns:
      The deserialized object.
    """
    return pickle_module.loads(self._data)

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
def wrapped_class(self):
  """Return the qualified class name of the wrapped object.

  Returns:
    A string like 'module.ClassName'.
  """
  return self._class

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
def load(self, pickle_module=pickle):
  """Deserialize the wrapped object.

  Args:
    pickle_module: The pickle module used for deserialization.

  Returns:
    The deserialized object.
  """
  return pickle_module.loads(self._data)

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
def add_known_module(modname):
  """Register a module as known (i.e. no wrapping needed for its objects).

  Args:
    modname: The module name to add (its root module is registered).
  """
  KNOWN_MODULES.add(cu.root_module(modname))

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
def wrap(obj, pickle_module=pickle):
  """Recursively wrap objects whose modules are not yet known/loaded.

  Args:
    obj: The object to wrap (lists, dicts, and arbitrary objects are traversed).
    pickle_module: The pickle module to use for serialization.

  Returns:
    The wrapped object tree, with PickleWrap instances where needed.
  """
  return _wrap(obj, pickle_module)

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
def unwrap(obj, pickle_module=pickle):
  """Recursively unwrap PickleWrap instances by loading their wrapped data.

  Args:
    obj: The object tree containing PickleWrap instances.
    pickle_module: The pickle module to use for deserialization.

  Returns:
    The unwrapped object tree, with loaded objects replacing PickleWrap.
  """
  return _unwrap(obj, pickle_module)