Skip to content

base

mlte/value/base.py

The base class for MLTE value extensions.

NOTE: The Value implementation in this module (base) should not be confused with the Value implementation in the artifact module. The Value implementation in artifact is a proper MLTE artifact, it should be used for all "internal" types part of the MLTE value system. The Value implementation in this module is meant to be extended by users of MLTE to enrich the value system with their own value types; it provides the link between the statically-typed MLTE value system and dynamic extensions.

ValueBase

Bases: Value

The base class for MLTE value extensions.

Source code in mlte/value/base.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class ValueBase(artifact.Value, metaclass=abc.ABCMeta):
    """The base class for MLTE value extensions."""

    @classmethod
    def __subclasshook__(cls, subclass):
        """Define the interface for all Value subclasses."""
        return meta.has_callables(subclass, "serialize", "deserialize")

    @abc.abstractmethod
    def serialize(self) -> Dict[str, Any]:
        """
        Serialize the value to a JSON-compatible dictionary.
        :return: The dictionary representation
        """
        raise NotImplementedError("ValueBase.serialize()")

    @classmethod
    @abc.abstractmethod
    def deserialize(
        cls, metadata: EvidenceMetadata, data: Dict[str, Any]
    ) -> ValueBase:
        """
        Deserialize a Value instance from serialized representation.
        :param metadata: Evidence metadata associated with the value
        :param data: The serialized representation
        :return: The deserialized value
        """
        raise NotImplementedError("ValueBase.deserialize()")

    def to_model(self) -> ArtifactModel:
        """
        Serialize a value to its corresponding model.
        :return: The artifact model
        """
        return ArtifactModel(
            header=self.build_artifact_header(),
            body=ValueModel(
                metadata=self.metadata,
                value_class=self.get_class_path(),
                value=OpaqueValueModel(
                    data=self.serialize(),
                ),
            ),
        )

    @classmethod
    def from_model(cls, model: ArtifactModel) -> ValueBase:  # noqa[override]
        """
        Deserialize a value from its corresponding model.
        :param model: The artifact model
        :return: The deserialized artifact
        """
        assert (
            model.body.artifact_type == ArtifactType.VALUE
        ), "Broken precondition."
        assert (
            model.body.value.value_type == ValueType.OPAQUE
        ), "Broken precondition."
        return cls.deserialize(model.body.metadata, model.body.value.data)

    def __str__(self) -> str:
        """Return a string representation, by default serializing it."""
        return f"{self.serialize()}"

__str__()

Return a string representation, by default serializing it.

Source code in mlte/value/base.py
88
89
90
def __str__(self) -> str:
    """Return a string representation, by default serializing it."""
    return f"{self.serialize()}"

__subclasshook__(subclass) classmethod

Define the interface for all Value subclasses.

Source code in mlte/value/base.py
31
32
33
34
@classmethod
def __subclasshook__(cls, subclass):
    """Define the interface for all Value subclasses."""
    return meta.has_callables(subclass, "serialize", "deserialize")

deserialize(metadata, data) abstractmethod classmethod

Deserialize a Value instance from serialized representation.

Parameters:

Name Type Description Default
metadata EvidenceMetadata

Evidence metadata associated with the value

required
data Dict[str, Any]

The serialized representation

required

Returns:

Type Description
ValueBase

The deserialized value

Source code in mlte/value/base.py
44
45
46
47
48
49
50
51
52
53
54
55
@classmethod
@abc.abstractmethod
def deserialize(
    cls, metadata: EvidenceMetadata, data: Dict[str, Any]
) -> ValueBase:
    """
    Deserialize a Value instance from serialized representation.
    :param metadata: Evidence metadata associated with the value
    :param data: The serialized representation
    :return: The deserialized value
    """
    raise NotImplementedError("ValueBase.deserialize()")

from_model(model) classmethod

Deserialize a value from its corresponding model.

Parameters:

Name Type Description Default
model ArtifactModel

The artifact model

required

Returns:

Type Description
ValueBase

The deserialized artifact

Source code in mlte/value/base.py
73
74
75
76
77
78
79
80
81
82
83
84
85
86
@classmethod
def from_model(cls, model: ArtifactModel) -> ValueBase:  # noqa[override]
    """
    Deserialize a value from its corresponding model.
    :param model: The artifact model
    :return: The deserialized artifact
    """
    assert (
        model.body.artifact_type == ArtifactType.VALUE
    ), "Broken precondition."
    assert (
        model.body.value.value_type == ValueType.OPAQUE
    ), "Broken precondition."
    return cls.deserialize(model.body.metadata, model.body.value.data)

serialize() abstractmethod

Serialize the value to a JSON-compatible dictionary.

Returns:

Type Description
Dict[str, Any]

The dictionary representation

Source code in mlte/value/base.py
36
37
38
39
40
41
42
@abc.abstractmethod
def serialize(self) -> Dict[str, Any]:
    """
    Serialize the value to a JSON-compatible dictionary.
    :return: The dictionary representation
    """
    raise NotImplementedError("ValueBase.serialize()")

to_model()

Serialize a value to its corresponding model.

Returns:

Type Description
ArtifactModel

The artifact model

Source code in mlte/value/base.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
def to_model(self) -> ArtifactModel:
    """
    Serialize a value to its corresponding model.
    :return: The artifact model
    """
    return ArtifactModel(
        header=self.build_artifact_header(),
        body=ValueModel(
            metadata=self.metadata,
            value_class=self.get_class_path(),
            value=OpaqueValueModel(
                data=self.serialize(),
            ),
        ),
    )