Skip to content

query

Query and filtering functionality for store operations.

AllFilter

Bases: Filter

A filter that matches all entries.

Source code in mlte/store/query.py
 97
 98
 99
100
101
102
103
104
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
178
179
180
181
182
183
184
185
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
36
37
38
39
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
26
27
28
29
30
31
32
33
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
84
85
86
87
88
89
90
91
92
93
94
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
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
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."
        )

    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. Tags are a property of type list."""
        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
45
46
47
48
49
50
@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
58
59
60
61
62
63
64
65
66
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. Tags are a property of type list.

Source code in mlte/store/query.py
68
69
70
71
72
73
74
75
76
def get_tags(self, property_name: str) -> List[Any]:
    """Returns the given tags. Tags are a property of type list."""
    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()

Returns the class-specific type for filtering.

Source code in mlte/store/query.py
52
53
54
55
56
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
117
118
119
120
121
122
123
124
125
126
127
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 check against."""

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

id instance-attribute

The identifier to check against.

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
107
108
109
110
111
112
113
114
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
188
189
190
191
192
193
194
195
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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."""

    property: Any
    """The property check against."""

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

name instance-attribute

The name of the property.

property instance-attribute

The property check against.

type = FilterType.PROPERTY class-attribute instance-attribute

An identifier for the filter type.

Query

Bases: BaseModel

A Query object represents a query over entries.

Source code in mlte/store/query.py
198
199
200
201
202
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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."""

    tag: str
    """The tag to check against."""

    def match(self, item: Filterable) -> bool:
        lower_tags = [tag.lower() for tag in item.get_tags(self.name)]
        return any([self.tag.lower() in tag for tag in lower_tags])

name instance-attribute

The name of the property with the tags.

tag instance-attribute

The tag to check against.

type = FilterType.TAG class-attribute instance-attribute

An identifier for the filter type.

TypeFilter

Bases: Filter

A filter that matches an item's type.

Source code in mlte/store/query.py
130
131
132
133
134
135
136
137
138
139
140
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: str
    """The type to check against."""

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

item_type instance-attribute

The type to check against.

type = FilterType.TYPE class-attribute instance-attribute

An identifier for the filter type.