spekk.trees.core.filter#
- spekk.trees.core.filter(tree: Mapping[Any, Mapping[Any, Tree] | Sequence[Tree] | Any] | Sequence[Mapping[Any, Tree] | Sequence[Tree] | Any] | Any, is_leaf: Callable[[Mapping[Any, Mapping[Any, Tree] | Sequence[Tree] | Any] | Sequence[Mapping[Any, Tree] | Sequence[Tree] | Any] | Any], bool], predicate: Callable[[Mapping[Any, Mapping[Any, Tree] | Sequence[Tree] | Any] | Sequence[Mapping[Any, Tree] | Sequence[Tree] | Any] | Any], bool], path: tuple = ()) Mapping[Any, Mapping[Any, Tree] | Sequence[Tree] | Any] | Sequence[Mapping[Any, Tree] | Sequence[Tree] | Any] | Any[source]#
Remove all subtrees for which the predicate returns
False.>>> tree = {"a": [1, {"b": 2}, 3], "c": 4, "d": 5} >>> is_leaf = lambda x: isinstance(x, int) >>> filter(tree, is_leaf, lambda x: x % 2 == 1 if is_leaf(x) else True) {'a': [1, {}, 3], 'd': 5}