Serialization

The serialization module provides utilities for handling Numba objects and other problematic types during pickling.

Serialize Base

_SerializeBase()

Mixin class providing serialization utilities for objects with Numba dependencies. Handles conversion of problematic objects to serializable formats during pickling and reconstruction during unpickling.

class hiten.algorithms.types.serialization._SerializeBase[source]

Bases: object

Mixin class providing serialization utilities for objects with Numba dependencies.

This class provides methods to handle serialization of objects that may contain Numba-compiled functions, typed containers, or other objects that cannot be pickled directly. It converts problematic objects to serializable formats during pickling and handles reconstruction during unpickling.

The main methods are: - _make_serializable(): Converts objects to pickle-safe formats - __getstate__(): Handles object state serialization - __setstate__(): Handles object state deserialization

Notes

This class is designed to be used as a mixin with other base classes. It handles the common case where objects contain Numba-typed containers (like numba.typed.List) that cannot be pickled directly due to internal _nrt_python._MemInfo objects.

Examples

>>> class MyClass(_SerializeBase):
...     def __init__(self):
...         from numba.typed import List
...         self.data = List()
...         self.data.append([1, 2, 3])
>>>
>>> obj = MyClass()
>>> # This will now work without pickling errors
>>> import pickle
>>> pickled = pickle.dumps(obj)
>>> restored = pickle.loads(pickled)
__getstate__()[source]

Get state for pickling, handling Numba objects properly.

This method creates a dictionary containing the object’s state that can be safely pickled. It processes all attributes through _make_serializable() to handle Numba objects and other problematic types.

Returns:

Dictionary containing the serializable state of the object

Return type:

dict[str, Any]

__setstate__(state)[source]

Set state after unpickling, reconstructing Numba objects if needed.

This method restores the object’s state from a dictionary created by __getstate__(). It handles the reconstruction of objects that were converted during serialization.

Parameters:

state (dict[str, Any]) – Dictionary containing the serializable state of the object

Return type:

None