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: {2, 4}, False: {1, 3}}
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 test_reduce_by_init(): assert reduceby(iseven, add, [1, 2, 3, 4]) == {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} assert reduceby(iseven, add, [1, 2, 3, 4], no_default2) == { True: 2 + 4, False: 1 + 3 }