deserialize#

Deserialize into a PyMC-Marketing object.

This is a two step process:

  1. Determine if the data is of the correct type.

  2. Deserialize the data into a python object for PyMC-Marketing.

This is used to deserialize JSON data into PyMC-Marketing objects throughout the package.

Examples#

Make use of the already registered PyMC-Marketing deserializers:

from pymc_marketing.deserialize import deserialize

prior_class_data = {
    "dist": "Normal",
    "kwargs": {"mu": 0, "sigma": 1}
}
prior = deserialize(prior_class_data)
# Prior("Normal", mu=0, sigma=1)

Register custom class deserialization:

from pymc_marketing.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"]),
)

Deserialize data into that custom class:

from pymc_marketing.deserialize import deserialize

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

Functions

deserialize(data)

Deserialize a dictionary into a Python object.

register_deserialization(is_type, deserialize)

Register an arbitrary deserialization.

Classes

Deserializer(is_type, deserialize)

Object to store information required for deserialization.

Exceptions

DeserializableError(data)

Error raised when data cannot be deserialized.