Skip to content

Commit

Permalink
Add summarize
Browse files Browse the repository at this point in the history
  • Loading branch information
jaraco committed Jul 12, 2023
1 parent 053092f commit 76dbcd2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
14 changes: 14 additions & 0 deletions jaraco/itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,3 +1242,17 @@ def check(*window):

match_indexes = more_itertools.locate(seq, check, window_size=len(cand))
return next(match_indexes, None)


def summarize(items: Iterable, **bin_checks):
"""
>>> is_str = lambda item: isinstance(item, str)
>>> is_int = lambda item: isinstance(item, int)
>>> summarize(['a', 'b', 20], strings=is_str, ints=is_int)
{'strings': 2, 'ints': 1}
"""
counters = {name: itertools.count() for name in bin_checks}
for item, check in itertools.product(items, bin_checks):
if bin_checks[check](item):
next(counters[check])
return {name: next(counter) for name, counter in counters.items()}
1 change: 1 addition & 0 deletions newsfragments/+dd17dc06.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added ``summarize``.

0 comments on commit 76dbcd2

Please sign in to comment.