Skip to content

query

mlte/store/common/query.py

Query and filtering functionality for store operations.

AllFilter

Bases: Filter

A filter that matches all entries.

Source code in mlte/store/query.py
102
103
104
105
106
107
108
109
class AllFilter(Filter):
    """A filter that matches all entries."""

    type: Literal[FilterType.ALL] = FilterType.ALL
    """An identifier for the filter type."""

    def match(self, _: Filterable) -> bool:
        return True

type = FilterType.ALL class-attribute instance-attribute

An identifier for the filter type.

AndFilter

Bases: CompositeFilter

A generic filter that implements a logical AND of filters.

Source code in mlte/store/query.py
180
181
182
183
184
185
186
187
class AndFilter(CompositeFilter):
    """A generic filter that implements a logical AND of filters."""

    type: Literal[FilterType.AND] = FilterType.AND
    """An identifier for the filter type."""

    def match(self, item: Filterable) -> bool:
        return all(filter.match(item) for filter in self.filters)

type = FilterType.AND class-attribute instance-attribute

An identifier for the filter type.

CompositeFilter

Bases: Filter

Definition of a composite filter interface.

Source code in mlte/store/query.py
40
41
42
43
class CompositeFilter(Filter):
    """Definition of a composite filter interface."""

    filters: List[SupportedFilter]

Filter

Bases: BaseModel

Definition of a filter interface.

Source code in mlte/store/query.py
30
31
32
33
34
35
36
37
class Filter(BaseModel):
    """Definition of a filter interface."""

    @abstractmethod
    def match(self, item: Filterable) -> bool:
        raise NotImplementedError(
            "Can't call match without a specific implementation."
        )

FilterType

Bases: LowercaseStrEnum

An enumeration over filter types.

Source code in mlte/store/query.py
89
90
91
92
93
94
95
96
97
98
99
class FilterType(LowercaseStrEnum):
    """An enumeration over filter types."""

    IDENTIFIER = "identifier"
    TYPE = "type"
    TAG = "tag"
    PROPERTY = "property"
    ALL = "all"
    NONE = "none"
    AND = "and"
    OR = "or"

Filterable

Bases: BaseModel

Definition of a filterable interface.

Source code in mlte/store/query.py
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
class Filterable(BaseModel):
    """Definition of a filterable interface."""

    @abstractmethod
    def get_identifier(self) -> str:
        """Returns the identifier for filtering."""
        raise NotImplementedError(
            "Can't call get id without a specific implementation."
        )

    @abstractmethod
    def get_type(self) -> Any:
        """Returns the class-specific type for filtering."""
        raise NotImplementedError(
            "Can't call get type without a specific implementation."
        )

    def get_property(self, property_name: str) -> Any:
        """Returns the given property."""
        try:
            value = getattr(self, property_name)
            return value
        except Exception:
            raise errors.ErrorNotFound(
                f"Property '{property_name}' is not part of the model."
            )

    def get_tags(self, property_name: str) -> List[Any]:
        """Returns the given tags."""
        value = self.get_property(property_name)
        if type(value) is not list:
            raise RuntimeError(
                f"Property {property_name} does not contain a list of tags."
            )
        else:
            return value

get_identifier() abstractmethod

Returns the identifier for filtering.

Source code in mlte/store/query.py
49
50
51
52
53
54
@abstractmethod
def get_identifier(self) -> str:
    """Returns the identifier for filtering."""
    raise NotImplementedError(
        "Can't call get id without a specific implementation."
    )

get_property(property_name)

Returns the given property.

Source code in mlte/store/query.py
63
64
65
66
67
68
69
70
71
def get_property(self, property_name: str) -> Any:
    """Returns the given property."""
    try:
        value = getattr(self, property_name)
        return value
    except Exception:
        raise errors.ErrorNotFound(
            f"Property '{property_name}' is not part of the model."
        )

get_tags(property_name)

Returns the given tags.

Source code in mlte/store/query.py
73
74
75
76
77
78
79
80
81
def get_tags(self, property_name: str) -> List[Any]:
    """Returns the given tags."""
    value = self.get_property(property_name)
    if type(value) is not list:
        raise RuntimeError(
            f"Property {property_name} does not contain a list of tags."
        )
    else:
        return value

get_type() abstractmethod

Returns the class-specific type for filtering.

Source code in mlte/store/query.py
56
57
58
59
60
61
@abstractmethod
def get_type(self) -> Any:
    """Returns the class-specific type for filtering."""
    raise NotImplementedError(
        "Can't call get type without a specific implementation."
    )

IdentifierFilter

Bases: Filter

A filter that matches an item's identifier.

Source code in mlte/store/query.py
122
123
124
125
126
127
128
129
130
131
132
class IdentifierFilter(Filter):
    """A filter that matches an item's identifier."""

    type: Literal[FilterType.IDENTIFIER] = FilterType.IDENTIFIER
    """An identifier for the filter type."""

    id: str
    """The identifier to match."""

    def match(self, item: Filterable) -> bool:
        return bool(item.get_identifier() == self.id)

id instance-attribute

The identifier to match.

type = FilterType.IDENTIFIER class-attribute instance-attribute

An identifier for the filter type.

NoneFilter

Bases: Filter

A filter that matches no entries.

Source code in mlte/store/query.py
112
113
114
115
116
117
118
119
class NoneFilter(Filter):
    """A filter that matches no entries."""

    type: Literal[FilterType.NONE] = FilterType.NONE
    """An identifier for the filter type."""

    def match(self, _: Filterable) -> bool:
        return False

type = FilterType.NONE class-attribute instance-attribute

An identifier for the filter type.

OrFilter

Bases: CompositeFilter

A generic filter that implements a logical OR of filters.

Source code in mlte/store/query.py
190
191
192
193
194
195
196
197
class OrFilter(CompositeFilter):
    """A generic filter that implements a logical OR of filters."""

    type: Literal[FilterType.OR] = FilterType.OR
    """An identifier for the filter type."""

    def match(self, item: Filterable) -> bool:
        return any(filter.match(item) for filter in self.filters)

type = FilterType.OR class-attribute instance-attribute

An identifier for the filter type.

PropertyFilter

Bases: Filter

A filter that matches a given property.

Source code in mlte/store/query.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
class PropertyFilter(Filter):
    """A filter that matches a given property."""

    type: Literal[FilterType.PROPERTY] = FilterType.PROPERTY
    """An identifier for the filter type."""

    name: str
    """The name of the property."""

    value: Any
    """The property to match."""

    def match(self, item: Filterable) -> bool:
        return bool(self.value in item.get_property(self.name))

name instance-attribute

The name of the property.

type = FilterType.PROPERTY class-attribute instance-attribute

An identifier for the filter type.

value instance-attribute

The property to match.

Query

Bases: BaseModel

A Query object represents a query over entries.

Source code in mlte/store/query.py
200
201
202
203
204
class Query(BaseModel):
    """A Query object represents a query over entries."""

    filter: SupportedFilter = AllFilter()
    """The filter that is applied to implement the query."""

filter = AllFilter() class-attribute instance-attribute

The filter that is applied to implement the query.

TagFilter

Bases: Filter

A filter that matches a given tag, from a field with a list of tags.

Source code in mlte/store/query.py
148
149
150
151
152
153
154
155
156
157
158
159
160
161
class TagFilter(Filter):
    """A filter that matches a given tag, from a field with a list of tags."""

    type: Literal[FilterType.TAG] = FilterType.TAG
    """An identifier for the filter type."""

    name: str
    """The name of the property with the tags."""

    value: Any
    """The property to match."""

    def match(self, item: Filterable) -> bool:
        return self.value in item.get_tags(self.name)

name instance-attribute

The name of the property with the tags.

type = FilterType.TAG class-attribute instance-attribute

An identifier for the filter type.

value instance-attribute

The property to match.

TypeFilter

Bases: Filter

A filter that matches an item's type.

Source code in mlte/store/query.py
135
136
137
138
139
140
141
142
143
144
145
class TypeFilter(Filter):
    """A filter that matches an item's type."""

    type: Literal[FilterType.TYPE] = FilterType.TYPE
    """An identifier for the filter type."""

    item_type: Any
    """The type to match."""

    def match(self, item: Filterable) -> bool:
        return bool(item.get_type() == self.item_type)

item_type instance-attribute

The type to match.

type = FilterType.TYPE class-attribute instance-attribute

An identifier for the filter type.