Skip to content

Commit

Permalink
feat: where_attr
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveitaly committed Dec 12, 2023
1 parent f9e3417 commit 0d46df3
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions funcy/colls.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,9 @@ def invoke(objects, name, *args, **kwargs):
"""Yields results of the obj.name(*args, **kwargs)
for each object in objects."""
return map(methodcaller(name, *args, **kwargs), objects)

def where_attr(objects, **cond):
"""Iterates over objects containing attributes which satisfy all pairs in cond."""
items = cond.items()
match = lambda obj: all(hasattr(obj, k) and getattr(obj, k) == v for k, v in items)
return filter(match, objects)

0 comments on commit 0d46df3

Please sign in to comment.