from typing import Any, Callable, Dict, Iterable, List, Union from server.utils.processor.core import Processor def apply_filter_processors(filters: Iterable[Union[Callable, Processor]], item: Dict[str, Any]) -> List[bool]: """Apply each filter to passed item and return collected result(s) of boolean(s) to a list Args: filters: processors that check the item and return List of True or False values item: item to be checked Returns List of boolean values -> each value for each filter processor """ result = [] for p in filters: args = (item,) if isinstance(p, Processor): result.append(p.func(*args, *p.args, **p.kwargs)) else: result.append(p(*args)) return result def filter_items( items: List[Dict[str, Any]], filters: Iterable[Union[Callable, Processor]], occurrence: Callable = all ) -> List[Dict[str, Any]]: """Filter items by applied filters that check the item by occurrence if many filters are passed Args: items: List of Dict items to filter based on passed filters filters: processors that check the item and return List of True or False values occurrence: builtin function - one of [all, any] to check filters results Returns: List of filtered Dicts """ return [item for item in items if occurrence(apply_filter_processors(filters, item))]