Filter Operators

Contains these FilterOperator classes:

  • And
  • Or

FilterOperator

Base class for filter operators which can be used to specify the search query

Source code in src/alpha/infra/models/filter_operators.py
class FilterOperator:
    """Base class for filter operators which can be used to specify the
    search query
    """

    def __init__(self, *search_filters: SearchFilter | Self) -> None:
        """Instantiate the filter operator by storing the search filter
        objects
        """
        self.search_filters: Iterable[SearchFilter | Self] = search_filters

    @property
    def filter_operator(
        self,
    ) -> Callable[[ColumnElement[bool]], ColumnElement[bool]]:
        """Returns a filter operator

        Returns
        -------
        Callable[[ColumnElement[bool]], ColumnElement[bool]]
            Filter operator

        Raises
        ------
        NotImplementedError
            When called directly
        """
        raise NotImplementedError(
            "The FilterOperator class cannot be used directly. "
            "Use the And or Or classes instead."
        )

    def filter(self, query: Query[Any]) -> Query[Any]:
        """Applies the search filters on the query by using the filter operator

        Parameters
        ----------
        query
            Query object

        Returns
        -------
        Query
            Query object with filters applied
        """
        filters = [f.filter_statement for f in self.search_filters]  # type: ignore
        return query.filter(self.filter_operator(*filters))  # type: ignore

filter_operator property
filter_operator

Returns a filter operator

Returns:
  • Callable[[ColumnElement[bool]], ColumnElement[bool]]

    Filter operator

Raises:
  • NotImplementedError

    When called directly

Methods:

__init__
__init__(*search_filters)

Instantiate the filter operator by storing the search filter objects

Source code in src/alpha/infra/models/filter_operators.py
def __init__(self, *search_filters: SearchFilter | Self) -> None:
    """Instantiate the filter operator by storing the search filter
    objects
    """
    self.search_filters: Iterable[SearchFilter | Self] = search_filters
filter
filter(query)

Applies the search filters on the query by using the filter operator

Parameters:
  • query (Query[Any]) –

    Query object

Returns:
  • Query

    Query object with filters applied

Source code in src/alpha/infra/models/filter_operators.py
def filter(self, query: Query[Any]) -> Query[Any]:
    """Applies the search filters on the query by using the filter operator

    Parameters
    ----------
    query
        Query object

    Returns
    -------
    Query
        Query object with filters applied
    """
    filters = [f.filter_statement for f in self.search_filters]  # type: ignore
    return query.filter(self.filter_operator(*filters))  # type: ignore

And

Bases: FilterOperator

FilterOperator which can be used to explicitly specify an 'and' statement to apply behaviore of SearchFilter objects which is comparable to AND in SQL.

Source code in src/alpha/infra/models/filter_operators.py
class And(FilterOperator):
    """FilterOperator which can be used to explicitly specify an 'and'
    statement to apply behaviore of SearchFilter objects which is comparable
    to AND in SQL.
    """

    @property
    def filter_operator(
        self,
    ) -> Callable[[ColumnElement[bool]], ColumnElement[bool]]:
        """Returns the 'and' filter operator

        Returns
        -------
        Callable[[ColumnElement[bool]], ColumnElement[bool]]
            'and' filter operator
        """
        return and_

filter_operator property
filter_operator

Returns the 'and' filter operator

Returns:
  • Callable[[ColumnElement[bool]], ColumnElement[bool]]

    'and' filter operator

Or

Bases: FilterOperator

FilterOperator which can be used to explicitly specify an 'or' statement to apply behaviore of SearchFilter objects which is comparable to OR in SQL.

Source code in src/alpha/infra/models/filter_operators.py
class Or(FilterOperator):
    """FilterOperator which can be used to explicitly specify an 'or'
    statement to apply behaviore of SearchFilter objects which is comparable
    to OR in SQL.
    """

    @property
    def filter_operator(
        self,
    ) -> Callable[[ColumnElement[bool]], ColumnElement[bool]]:
        """Returns the 'or' filter operator

        Returns
        -------
        Callable[[ColumnElement[bool]], ColumnElement[bool]]
            'or' filter operator
        """
        return or_

filter_operator property
filter_operator

Returns the 'or' filter operator

Returns:
  • Callable[[ColumnElement[bool]], ColumnElement[bool]]

    'or' filter operator