def test_reduceby(): data = [1, 2, 3, 4, 5] iseven = lambda x: x % 2 == 0 assert reduceby(iseven, add, data, 0) == {False: 9, True: 6} assert reduceby(iseven, mul, data, 1) == {False: 15, True: 8} projects = [{ 'name': 'build roads', 'state': 'CA', 'cost': 1000000 }, { 'name': 'fight crime', 'state': 'IL', 'cost': 100000 }, { 'name': 'help farmers', 'state': 'IL', 'cost': 2000000 }, { 'name': 'help farmers', 'state': 'CA', 'cost': 200000 }] assert reduceby(lambda x: x['state'], lambda acc, x: acc + x['cost'], projects, 0) == { 'CA': 1200000, 'IL': 2100000 } assert reduceby('state', lambda acc, x: acc + x['cost'], projects, 0) == { 'CA': 1200000, 'IL': 2100000 }
def test_reduceby(): data = [1, 2, 3, 4, 5] iseven = lambda x: x % 2 == 0 assert reduceby(iseven, add, data, 0) == {False: 9, True: 6} assert reduceby(iseven, mul, data, 1) == {False: 15, True: 8} projects = [{'name': 'build roads', 'state': 'CA', 'cost': 1000000}, {'name': 'fight crime', 'state': 'IL', 'cost': 100000}, {'name': 'help farmers', 'state': 'IL', 'cost': 2000000}, {'name': 'help farmers', 'state': 'CA', 'cost': 200000}] assert reduceby(lambda x: x['state'], lambda acc, x: acc + x['cost'], projects, 0) == {'CA': 1200000, 'IL': 2100000}
def test_reduce_by_callable_default(): def set_add(s, i): s.add(i) return s assert reduceby(iseven, set_add, [1, 2, 3, 4, 1, 2], set) == \ {True: set([2, 4]), False: set([1, 3])}
def positions(it: Seq[_H]) -> Map[_H, Tuple[int, ...]]: """ Collect positions of (non-unique) tuple of items in original sequence. >>> positions(['a', 'b', 'c']) {'a': (0,), 'b': (1,), 'c': (2,)} >>> positions(['a', 'b', 'a']) {'a': (0, 2), 'b': (1,)} >>> positions([]) {} Function can operate on sequences of arbitrary :class:`Hashable` items. >>> positions([('a', True), ('b', False), ('c', True)]) {('a', True): (0,), ('b', False): (1,), ('c', True): (2,)} >>> positions([('a', True), ('b', False), ('a', True)]) {('a', True): (0, 2), ('b', False): (1,)} """ def item(idx_item: Tuple[int, _H]) -> _H: _, item_ = idx_item return item_ def collect_idx(acc: Tuple[int, ...], idx_item: Tuple[int, _H]) -> Tuple[int, ...]: idx, _ = idx_item return (*acc, idx) def _empty() -> Tuple[int, ...]: return tuple() return reduceby(item, collect_idx, enumerate(it), init=_empty)
def test_reduce_by_callable_default(): def set_add(s, i): s.add(i) return s assert reduceby(iseven, set_add, [1, 2, 3, 4, 1, 2], set) == { True: {2, 4}, False: {1, 3}, }
def test_reduceby(): data = [1, 2, 3, 4, 5] iseven = lambda x: x % 2 == 0 assert reduceby(iseven, add, data, 0) == {False: 9, True: 6} assert reduceby(iseven, mul, data, 1) == {False: 15, True: 8} projects = [ { "name": "build roads", "state": "CA", "cost": 1000000 }, { "name": "fight crime", "state": "IL", "cost": 100000 }, { "name": "help farmers", "state": "IL", "cost": 2000000 }, { "name": "help farmers", "state": "CA", "cost": 200000 }, ] assert reduceby(lambda x: x["state"], lambda acc, x: acc + x["cost"], projects, 0) == { "CA": 1200000, "IL": 2100000 } assert reduceby("state", lambda acc, x: acc + x["cost"], projects, 0) == { "CA": 1200000, "IL": 2100000, }
def order_by( it: Iterable[E], by: Seq[K], key: Callable[[E], Optional[K]] = identity # type: ignore ) -> Iterable[Optional[E]]: """ Collect given elements `it` and order them in order given by keys `by`. A key can be obtained from an entity using function `key`. Key sequence `by` is assumed to be a super-set of keys from `it`, missing entries will be indicated by `None`s in the output. >>> list(order_by(['a', 'c'], by=['c', 'b', 'a'])) ['c', None, 'a'] >>> list(order_by([], by=['a', 'b'])) [None, None] >>> list(order_by([{'id': 'a'}, {'id_x': 'c'}], ... by=['c', 'b', 'a'], key=lambda e: e.get('id'))) [None, None, {'id': 'a'}] Duplicate item occurrences will preserve order of entities with corresponding key. >>> list(order_by([{'id': 'a', 'data': 1}, {'id': 'a', 'data': 2}], ... by=['a', 'b', 'a'], key=lambda e: e.get('id'))) [{'id': 'a', 'data': 1}, None, {'id': 'a', 'data': 2}] As a side-effect, original iterable is consumed by this operation. >>> it = iter(['a', 'c']) >>> _ = list(order_by(it, by=['c', 'b', 'a'])) >>> next(it) Traceback (most recent call last): ... StopIteration """ groups = reduceby(key, MutIter.add, it, init=MutIter) for item in by: yield next(groups.get(item, MutIter()), None)
def test_reduceby(): data = [1, 2, 3, 4, 5] iseven = lambda x: x % 2 == 0 assert reduceby(iseven, add, data, 0) == {False: 9, True: 6} assert reduceby(iseven, mul, data, 1) == {False: 15, True: 8} projects = [ {"name": "build roads", "state": "CA", "cost": 1000000}, {"name": "fight crime", "state": "IL", "cost": 100000}, {"name": "help farmers", "state": "IL", "cost": 2000000}, {"name": "help farmers", "state": "CA", "cost": 200000}, ] assert reduceby(lambda x: x["state"], lambda acc, x: acc + x["cost"], projects, 0) == {"CA": 1200000, "IL": 2100000} assert reduceby("state", lambda acc, x: acc + x["cost"], projects, 0) == {"CA": 1200000, "IL": 2100000} assert reduceby(["state"], lambda acc, x: acc + x["cost"], projects, 0) == {("CA",): 1200000, ("IL",): 2100000} assert reduceby(["state", "state"], lambda acc, x: acc + x["cost"], projects, 0) == { ("CA", "CA"): 1200000, ("IL", "IL"): 2100000, }
def test_reduce_by_init(): assert reduceby(iseven, add, [1, 2, 3, 4]) == {True: 2 + 4, False: 1 + 3} assert reduceby(iseven, add, [1, 2, 3, 4], no_default2) == {True: 2 + 4, False: 1 + 3}
def test_reduce_by_init(): assert reduceby(iseven, add, [1, 2, 3, 4]) == {True: 2 + 4, False: 1 + 3}