You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I love where. What about another similar function, that takes predicates instead of values? Not sure what to call it, but it behaves like this:
deffilterwhere(mappings, **preds):
""" Like `where` but instead of value equality checks, use predicate functions. .. doctest :: >>> mappings = [{'foo': 'a', 'bar': 16}, ... {'foo': 'b', 'bar': 32}, ... {'foo': 'd', 'bar': 128, '': ''}, ... {'foo': 'e', 'bar': 123, 'baz': 'quux'}, ... {'foo': 'c', 'bar': 6400}, ... {'foo': 'f', 'bar': float("inf")},] >>> x = filterwhere(mappings, bar=lambda bar: bar < 100) >>> assert x == [{'foo': 'a', 'bar': 16}, {'foo': 'b', 'bar': 32}] >>> y = filterwhere( ... mappings, bar=lambda n: n % 2 != 0 and n < 1000) >>> assert y == [{'foo': 'e', 'bar': 123, 'baz': 'quux'}] :param mappings: A sequence of mappings, for example a list of dicts. :param preds: Any number of predicate functions. Each mapping must pass `pred(mapping[key_of_mapping])` for each keyword argument pair of `key_of_mapping=pred`. :return: A sequence of mappings. """match=lambdam: all(pred(m[k]) fork, predinpreds.items())
returnfilter(match, mappings)
The text was updated successfully, but these errors were encountered:
I like such little neat things too. However, there is good solution for such situation in python - list comprehensions. So, even where is a stretch. I try to limit funcy scope at some level which makes me resistant to adding new things like this.
So I won't add it, sorry. I will still leave this issue open in case it will attract more interest. Then I'll probably reconsider.
I love
where
. What about another similar function, that takes predicates instead of values? Not sure what to call it, but it behaves like this:The text was updated successfully, but these errors were encountered: