Skip to content

serializable

Serializable interface.

Serializable

Bases: ABC

Interface to define classes that can be converted into a Pydantic model.

Source code in mlte/model/serializable.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Serializable(ABC):
    """
    Interface to define classes that can be converted into a Pydantic model.
    """

    @classmethod
    def __subclasshook__(cls, subclass):
        return meta.has_callables(subclass, "to_model", "from_model")

    @abstractmethod
    def to_model(self) -> BaseModel:
        """Serialize an Serializable to its corresponding model."""
        raise NotImplementedError(
            "to_model() not implemented for abstract Serializable."
        )

    @classmethod
    @abstractmethod
    def from_model(cls, model: BaseModel) -> Any:
        """Deserialize an Serializable from its corresponding model."""
        raise NotImplementedError(
            "from_model() not implemented for abstract Serializable."
        )

    def __json__(self):
        """Hack method to make Serializable serializable to JSON if importing json-fix before json.dumps."""
        return self.to_model().to_json()

    def _equal(a: Serializable, b: Serializable) -> bool:
        """
        Compare Serializable instances for equality.

        :param a: Input instance
        :param b: Input instance
        :return: `True` if `a` and `b` are equal, `False` otherwise
        """
        return a.to_model() == b.to_model()

    def __eq__(self, other: object) -> bool:
        """Test instance for equality."""
        if not isinstance(other, Serializable):
            return False
        return self._equal(other)

__eq__(other)

Test instance for equality.

Source code in mlte/model/serializable.py
52
53
54
55
56
def __eq__(self, other: object) -> bool:
    """Test instance for equality."""
    if not isinstance(other, Serializable):
        return False
    return self._equal(other)

__json__()

Hack method to make Serializable serializable to JSON if importing json-fix before json.dumps.

Source code in mlte/model/serializable.py
38
39
40
def __json__(self):
    """Hack method to make Serializable serializable to JSON if importing json-fix before json.dumps."""
    return self.to_model().to_json()

from_model(model) abstractmethod classmethod

Deserialize an Serializable from its corresponding model.

Source code in mlte/model/serializable.py
30
31
32
33
34
35
36
@classmethod
@abstractmethod
def from_model(cls, model: BaseModel) -> Any:
    """Deserialize an Serializable from its corresponding model."""
    raise NotImplementedError(
        "from_model() not implemented for abstract Serializable."
    )

to_model() abstractmethod

Serialize an Serializable to its corresponding model.

Source code in mlte/model/serializable.py
23
24
25
26
27
28
@abstractmethod
def to_model(self) -> BaseModel:
    """Serialize an Serializable to its corresponding model."""
    raise NotImplementedError(
        "to_model() not implemented for abstract Serializable."
    )