bundles / scipy 1.17.1 / scipy / _lib / array_api_extra / _lib / _utils / _helpers / pickle_flatten
function
scipy._lib.array_api_extra._lib._utils._helpers:pickle_flatten
source: /scipy/_lib/array_api_extra/_lib/_utils/_helpers.py :371
Signature
def pickle_flatten ( obj : object , cls : type[T] | tuple[type[T], ...] ) → tuple[list[T], FlattenRest] Summary
Use the pickle machinery to extract objects out of an arbitrary container.
Extended Summary
Unlike regular pickle.dumps, this function always succeeds.
Parameters
obj: objectThe object to pickle.
cls: type | tuple[type, ...]One or multiple classes to extract from the object. The instances of these classes inside
objwill not be pickled.
Returns
instances: list[cls]All instances of
clsfound insideobj(not pickled).: restOpaque object containing the pickled bytes plus all other objects where
__reduce__/__reduce_ex__is either not implemented or raised. These are unpickleable objects, types, modules, and functions.This object is typically hashable save for fairly exotic objects that are neither pickleable nor hashable.
This object is pickleable if everything except
instanceswas pickleable in the input object.
Examples
class A: def __repr__(self): return "<A>" class NS: def __repr__(self): return "<NS>" def __reduce__(self): assert False, "not serializable" obj = {1: A(), 2: [A(), NS(), A()]} instances, rest = pickle_flatten(obj, A) instances✓
pickle_unflatten(instances, rest)
⚠pickle_unflatten(["foo", "bar", "baz"], rest)
⚠See also
- pickle_unflatten
Reverse function.
Aliases
-
scipy.differentiate.xpx.testing.pickle_flatten