dpdani / cereggii

Thread synchronization utilities for Python
Apache License 2.0
9 stars 0 forks source link

Add `AtomicDict.reduce()`, `AtomicDict.aggregate()` #24

Open dpdani opened 4 months ago

dpdani commented 4 months ago

Replicating collections.Counter would be made easier by the addition of aggregation methods:

from cereggii import AtomicDict, NOT_FOUND

d = AtomicDict()

d.aggregate("spam", lambda current_value: 1 if current_value is NOT_FOUND else current_value + 1)

batch = [
    ("spam", 1),
    ("foo", 20),
]

def count(key, current, new):
    if current is NOT_FOUND:
        return 1
    return current + new

d.reduce(batch, count)