Skip to content

condition

mlte/validation/condition.py

The interface for measurement validation.

Condition

The Condition class defines the interface for measurement validators.

Source code in mlte/spec/condition.py
 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
 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
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
class Condition:
    """
    The Condition class defines the interface for measurement validators.
    """

    @typing.no_type_check
    def __init__(
        self,
        name: str,
        arguments: List[Any],
        validator: Validator,
        value_class: Optional[str] = None,
    ):
        """
        Initialize a Condition instance.

        :param name: The name of the name method, for documenting purposes.
        :param arguments: The list of arguments/thresholds passed to the callable.
        :param validator: The Validator that implements validation.
        :param value_class: The full module + class name of the Value that generated this condition, if any.
        """

        self.name: str = name
        """The human-readable identifier for the name method."""

        self.arguments: List[Any] = arguments
        """The threshold arguments used when creating the condition."""

        self.validator: Validator = validator
        """The validator that implements validation."""

        self.value_class: Optional[str] = value_class
        """Value type class where this Condition came from, if any."""

    def __call__(self, value: Value) -> Result:
        """
        Invoke the validation

        :param value: The value of measurement evaluation

        :return: The result of measurement validation
        """
        return self.validator.validate(value)._with_evidence_metadata(
            value.metadata
        )

    @staticmethod
    def build_condition(
        bool_exp: Optional[Callable[[Any], bool]] = None,
        success: Optional[str] = None,
        failure: Optional[str] = None,
        info: Optional[str] = None,
    ) -> Condition:
        """Creates a Condition using the provided test, extracting context info from the method that called us."""
        # Get method info, passing our caller as argument.
        curr_frame = inspect.currentframe()
        caller_function = curr_frame.f_back if curr_frame is not None else None
        method_info = FunctionInfo.get_function_info(caller_function)

        # Build the validator. We can't really check at this point if the bool_exp actually returns a bool.
        validator = Validator.build_validator(
            bool_exp=bool_exp,
            success=success,
            failure=failure,
            info=info,
            caller_function=caller_function,
        )

        condition: Condition = Condition(
            method_info.function_name,
            method_info.arguments,
            validator,
            method_info.function_class,
        )
        return condition

    def to_model(self) -> ConditionModel:
        """
        Returns this condition as a model.

        :return: The serialized model object.
        """
        return ConditionModel(
            name=self.name,
            arguments=self.arguments,
            validator=self.validator.to_model(),
            value_class=self.value_class,
        )

    @classmethod
    def from_model(cls, model: ConditionModel) -> Condition:
        """
        Deserialize a Condition from a model.

        :param model: The model.

        :return: The deserialized Condition
        """
        condition: Condition = Condition(
            model.name,
            model.arguments,
            Validator.from_model(model.validator),
            model.value_class,
        )
        return condition

    def __str__(self) -> str:
        """Return a string representation of Condition."""
        return f"{self.name} ({self.arguments}) from {self.value_class}"

    # -------------------------------------------------------------------------
    # Equality Testing
    # -------------------------------------------------------------------------

    def __eq__(self, other: object) -> bool:
        """Compare Condition instances for equality."""
        if not isinstance(other, Condition):
            return False
        reference: Condition = other
        return self.to_model() == reference.to_model()

arguments = arguments instance-attribute

The threshold arguments used when creating the condition.

name = name instance-attribute

The human-readable identifier for the name method.

validator = validator instance-attribute

The validator that implements validation.

value_class = value_class instance-attribute

Value type class where this Condition came from, if any.

__call__(value)

Invoke the validation

Parameters:

Name Type Description Default
value Value

The value of measurement evaluation

required

Returns:

Type Description
Result

The result of measurement validation

Source code in mlte/spec/condition.py
54
55
56
57
58
59
60
61
62
63
64
def __call__(self, value: Value) -> Result:
    """
    Invoke the validation

    :param value: The value of measurement evaluation

    :return: The result of measurement validation
    """
    return self.validator.validate(value)._with_evidence_metadata(
        value.metadata
    )

__eq__(other)

Compare Condition instances for equality.

Source code in mlte/spec/condition.py
134
135
136
137
138
139
def __eq__(self, other: object) -> bool:
    """Compare Condition instances for equality."""
    if not isinstance(other, Condition):
        return False
    reference: Condition = other
    return self.to_model() == reference.to_model()

__init__(name, arguments, validator, value_class=None)

Initialize a Condition instance.

Parameters:

Name Type Description Default
name str

The name of the name method, for documenting purposes.

required
arguments List[Any]

The list of arguments/thresholds passed to the callable.

required
validator Validator

The Validator that implements validation.

required
value_class Optional[str]

The full module + class name of the Value that generated this condition, if any.

None
Source code in mlte/spec/condition.py
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
@typing.no_type_check
def __init__(
    self,
    name: str,
    arguments: List[Any],
    validator: Validator,
    value_class: Optional[str] = None,
):
    """
    Initialize a Condition instance.

    :param name: The name of the name method, for documenting purposes.
    :param arguments: The list of arguments/thresholds passed to the callable.
    :param validator: The Validator that implements validation.
    :param value_class: The full module + class name of the Value that generated this condition, if any.
    """

    self.name: str = name
    """The human-readable identifier for the name method."""

    self.arguments: List[Any] = arguments
    """The threshold arguments used when creating the condition."""

    self.validator: Validator = validator
    """The validator that implements validation."""

    self.value_class: Optional[str] = value_class
    """Value type class where this Condition came from, if any."""

__str__()

Return a string representation of Condition.

Source code in mlte/spec/condition.py
126
127
128
def __str__(self) -> str:
    """Return a string representation of Condition."""
    return f"{self.name} ({self.arguments}) from {self.value_class}"

build_condition(bool_exp=None, success=None, failure=None, info=None) staticmethod

Creates a Condition using the provided test, extracting context info from the method that called us.

Source code in mlte/spec/condition.py
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
91
92
93
94
@staticmethod
def build_condition(
    bool_exp: Optional[Callable[[Any], bool]] = None,
    success: Optional[str] = None,
    failure: Optional[str] = None,
    info: Optional[str] = None,
) -> Condition:
    """Creates a Condition using the provided test, extracting context info from the method that called us."""
    # Get method info, passing our caller as argument.
    curr_frame = inspect.currentframe()
    caller_function = curr_frame.f_back if curr_frame is not None else None
    method_info = FunctionInfo.get_function_info(caller_function)

    # Build the validator. We can't really check at this point if the bool_exp actually returns a bool.
    validator = Validator.build_validator(
        bool_exp=bool_exp,
        success=success,
        failure=failure,
        info=info,
        caller_function=caller_function,
    )

    condition: Condition = Condition(
        method_info.function_name,
        method_info.arguments,
        validator,
        method_info.function_class,
    )
    return condition

from_model(model) classmethod

Deserialize a Condition from a model.

Parameters:

Name Type Description Default
model ConditionModel

The model.

required

Returns:

Type Description
Condition

The deserialized Condition

Source code in mlte/spec/condition.py
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@classmethod
def from_model(cls, model: ConditionModel) -> Condition:
    """
    Deserialize a Condition from a model.

    :param model: The model.

    :return: The deserialized Condition
    """
    condition: Condition = Condition(
        model.name,
        model.arguments,
        Validator.from_model(model.validator),
        model.value_class,
    )
    return condition

to_model()

Returns this condition as a model.

Returns:

Type Description
ConditionModel

The serialized model object.

Source code in mlte/spec/condition.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def to_model(self) -> ConditionModel:
    """
    Returns this condition as a model.

    :return: The serialized model object.
    """
    return ConditionModel(
        name=self.name,
        arguments=self.arguments,
        validator=self.validator.to_model(),
        value_class=self.value_class,
    )