Skip to content

treex.Filter

Mixin that adds a .filter() method to the class.

Source code in treeo/mixins.py
class Filter:
    """
    Mixin that adds a `.filter()` method to the class.
    """

    def filter(
        self: A,
        *filters: api.Filter,
        inplace: bool = False,
        flatten_mode: tp.Union[api.FlattenMode, str, None] = None,
    ) -> A:
        """
        `filter` is a wrapper over `treeo.filter` that passes `self` as the first argument.

        Arguments:
            *filters: Types to filter by, membership is determined by `issubclass`, or
                callables that take in a `FieldInfo` and return a `bool`.
            inplace: If `True`, the input `obj` is mutated and returned.
            flatten_mode: Sets a new `FlattenMode` context for the operation.

        Returns:
            A new pytree with the filtered fields. If `inplace` is `True`, `obj` is returned.
        """
        return api.filter(self, *filters, inplace=inplace, flatten_mode=flatten_mode)

filter(self, *filters, *, inplace=False, flatten_mode=None)

filter is a wrapper over treeo.filter that passes self as the first argument.

Parameters:

Name Type Description Default
*filters Union[Type[Any], Callable[[FieldInfo], bool]]

Types to filter by, membership is determined by issubclass, or callables that take in a FieldInfo and return a bool.

()
inplace bool

If True, the input obj is mutated and returned.

False
flatten_mode Union[treeo.tree.FlattenMode, str]

Sets a new FlattenMode context for the operation.

None

Returns:

Type Description
~A

A new pytree with the filtered fields. If inplace is True, obj is returned.

Source code in treeo/mixins.py
def filter(
    self: A,
    *filters: api.Filter,
    inplace: bool = False,
    flatten_mode: tp.Union[api.FlattenMode, str, None] = None,
) -> A:
    """
    `filter` is a wrapper over `treeo.filter` that passes `self` as the first argument.

    Arguments:
        *filters: Types to filter by, membership is determined by `issubclass`, or
            callables that take in a `FieldInfo` and return a `bool`.
        inplace: If `True`, the input `obj` is mutated and returned.
        flatten_mode: Sets a new `FlattenMode` context for the operation.

    Returns:
        A new pytree with the filtered fields. If `inplace` is `True`, `obj` is returned.
    """
    return api.filter(self, *filters, inplace=inplace, flatten_mode=flatten_mode)