def filter_to_set(x: Filter) -> Set[str]: """Converts a Filter into a set of collections, fails on the infinite set. Args: x: a filter (boolean, string, or list of strings). Returns: The input filter represented as a set of strings. """ assert x is not True and not isinstance(x, DenyList), 'Infinite set' if x is False: return set() if isinstance(x, str): return set([x]) if isinstance(x, Iterable): return set(x) raise errors.InvalidFilterError(x)
def in_filter(filter_like: Filter, col: str) -> bool: """Checks whether a filter can be applied to a collection. Used for both collections and rng sequence filters. Args: filter_like: a filter (either a boolean, a string, or a list of strings) for a collection. col: a collection, which is a string identifying a dictionary of data, for instance "params" or "batch_stats". Returns: True if either `filter_like` is True, equal to `col`, or a sequence containing `col`. """ if isinstance(filter_like, str): return col == filter_like if isinstance(filter_like, Container): return col in filter_like if isinstance(filter_like, bool): return filter_like raise errors.InvalidFilterError(filter_like)