register_deserialization#

pymc_extras.deserialize.register_deserialization(is_type: Callable[[Any], bool], deserialize: Callable[[Any], Any]) None[source]#

Register an arbitrary deserialization.

Use the deserialize() function to then deserialize data using all registered deserialize functions.

Parameters:
  • is_type (Callable[[Any], bool]) – Function to determine if the data is of the correct type.

  • deserialize (Callable[[dict], Any]) – Function to deserialize the data of that type.

Examples

Register a custom class deserialization:

from pymc_extras.deserialize import register_deserialization

class MyClass:
    def __init__(self, value: int):
        self.value = value

    def to_dict(self) -> dict:
        # Example of what the to_dict method might look like.
        return {"value": self.value}


register_deserialization(
    is_type=lambda data: data.keys() == {"value"} and isinstance(data["value"], int),
    deserialize=lambda data: MyClass(value=data["value"]),
)

Use that custom class deserialization:

from pymc_extras.deserialize import deserialize

data = {"value": 42}
obj = deserialize(data)
assert isinstance(obj, MyClass)