{ } Raw JSON

bundles / scipy latest / 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 : object

The object to pickle.

cls : type | tuple[type, ...]

One or multiple classes to extract from the object. The instances of these classes inside obj will not be pickled.

Returns

instances : list[cls]

All instances of cls found inside obj (not pickled).

: rest

Opaque 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 instances was 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)
This can be also used to swap inner objects; the only constraint is that the number of objects in and out must be the same:
pickle_unflatten(["foo", "bar", "baz"], rest)

See also

pickle_unflatten

Reverse function.

Aliases

  • scipy.differentiate.xpx.testing.pickle_flatten