Skip to content

string

An Evidence instance for a string value.

String

Bases: Evidence

String implements the Evidence interface for a single real value.

Source code in mlte/evidence/types/string.py
 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
 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
class String(Evidence):
    """
    String implements the Evidence interface for a single real value.
    """

    def __init__(self, value: str):
        """
        Initialize a String instance.
        :param value: The string value
        """
        assert isinstance(value, str), "Argument must be `string`."

        super().__init__()

        self.value = value
        """The attribute to store the string in."""

    def to_model(self) -> ArtifactModel:
        """
        Convert a string value artifact to its corresponding model.
        :return: The artifact model
        """
        return self._to_artifact_model(
            value_model=StringValueModel(string=self.value)
        )

    @classmethod
    def from_model(cls, model: BaseModel) -> String:
        """
        Convert a string value model to its corresponding artifact.
        :param model: The model representation
        :return: The string value
        """
        body = cls._check_proper_types(model, EvidenceType.STRING)
        return String(value=body.value.string).with_metadata(body.metadata)  # type: ignore

    @classmethod
    def contains(
        cls, substring: str, success: str = "", failure: str = ""
    ) -> Validator:
        """Checks if the given string is in this one."""
        bool_exp: Callable[[String], bool] = (
            lambda value: substring in value.value
        )
        validator: Validator = Validator.build_validator(
            bool_exp=bool_exp,
            success=success,
            failure=failure,
            default_success=f"Substring '{substring}' is contained in the string value.",
            default_failure=f"Substring '{substring}' is not contained in the string value.",
            input_types=[String],
        )
        return validator

    @classmethod
    def equal_to(
        cls, other_string: str, success: str = "", failure: str = ""
    ) -> Validator:
        """Checks if the given string is the same as this one in value."""
        bool_exp: Callable[[String], bool] = (
            lambda value: other_string == value.value
        )
        validator: Validator = Validator.build_validator(
            bool_exp=bool_exp,
            success=success,
            failure=failure,
            default_success=f"String '{other_string}' is equal to the internal string value.",
            default_failure=f"String '{other_string}' is not equal to the internal string value.",
            input_types=[String],
        )
        return validator

    # Overriden.
    @classmethod
    def load(cls, identifier: typing.Optional[str] = None) -> String:
        evidence = super().load(identifier)
        return typing.cast(String, evidence)

    def __str__(self) -> str:
        """Return a string representation of the value."""
        return f"{self.value}"

    def __eq__(self, other: object) -> bool:
        """Comparison between String values."""
        if not isinstance(other, String):
            return False
        return self._equal(other)

value = value instance-attribute

The attribute to store the string in.

__eq__(other)

Comparison between String values.

Source code in mlte/evidence/types/string.py
 99
100
101
102
103
def __eq__(self, other: object) -> bool:
    """Comparison between String values."""
    if not isinstance(other, String):
        return False
    return self._equal(other)

__init__(value)

Initialize a String instance.

Parameters:

Name Type Description Default
value str

The string value

required
Source code in mlte/evidence/types/string.py
22
23
24
25
26
27
28
29
30
31
32
def __init__(self, value: str):
    """
    Initialize a String instance.
    :param value: The string value
    """
    assert isinstance(value, str), "Argument must be `string`."

    super().__init__()

    self.value = value
    """The attribute to store the string in."""

__str__()

Return a string representation of the value.

Source code in mlte/evidence/types/string.py
95
96
97
def __str__(self) -> str:
    """Return a string representation of the value."""
    return f"{self.value}"

contains(substring, success='', failure='') classmethod

Checks if the given string is in this one.

Source code in mlte/evidence/types/string.py
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
@classmethod
def contains(
    cls, substring: str, success: str = "", failure: str = ""
) -> Validator:
    """Checks if the given string is in this one."""
    bool_exp: Callable[[String], bool] = (
        lambda value: substring in value.value
    )
    validator: Validator = Validator.build_validator(
        bool_exp=bool_exp,
        success=success,
        failure=failure,
        default_success=f"Substring '{substring}' is contained in the string value.",
        default_failure=f"Substring '{substring}' is not contained in the string value.",
        input_types=[String],
    )
    return validator

equal_to(other_string, success='', failure='') classmethod

Checks if the given string is the same as this one in value.

Source code in mlte/evidence/types/string.py
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
@classmethod
def equal_to(
    cls, other_string: str, success: str = "", failure: str = ""
) -> Validator:
    """Checks if the given string is the same as this one in value."""
    bool_exp: Callable[[String], bool] = (
        lambda value: other_string == value.value
    )
    validator: Validator = Validator.build_validator(
        bool_exp=bool_exp,
        success=success,
        failure=failure,
        default_success=f"String '{other_string}' is equal to the internal string value.",
        default_failure=f"String '{other_string}' is not equal to the internal string value.",
        input_types=[String],
    )
    return validator

from_model(model) classmethod

Convert a string value model to its corresponding artifact.

Parameters:

Name Type Description Default
model BaseModel

The model representation

required

Returns:

Type Description
String

The string value

Source code in mlte/evidence/types/string.py
43
44
45
46
47
48
49
50
51
@classmethod
def from_model(cls, model: BaseModel) -> String:
    """
    Convert a string value model to its corresponding artifact.
    :param model: The model representation
    :return: The string value
    """
    body = cls._check_proper_types(model, EvidenceType.STRING)
    return String(value=body.value.string).with_metadata(body.metadata)  # type: ignore

to_model()

Convert a string value artifact to its corresponding model.

Returns:

Type Description
ArtifactModel

The artifact model

Source code in mlte/evidence/types/string.py
34
35
36
37
38
39
40
41
def to_model(self) -> ArtifactModel:
    """
    Convert a string value artifact to its corresponding model.
    :return: The artifact model
    """
    return self._to_artifact_model(
        value_model=StringValueModel(string=self.value)
    )