test_recipes.py 820 B

123456789101112131415161718192021222324252627
  1. from toolz import first, identity, countby, partitionby
  2. def iseven(x):
  3. return x % 2 == 0
  4. def test_countby():
  5. assert countby(iseven, [1, 2, 3]) == {True: 1, False: 2}
  6. assert countby(len, ['cat', 'dog', 'mouse']) == {3: 2, 5: 1}
  7. assert countby(0, ('ab', 'ac', 'bc')) == {'a': 2, 'b': 1}
  8. def test_partitionby():
  9. assert list(partitionby(identity, [])) == []
  10. vowels = "aeiou"
  11. assert (list(partitionby(vowels.__contains__, "abcdefghi")) ==
  12. [("a",), ("b", "c", "d"), ("e",), ("f", "g", "h"), ("i",)])
  13. assert (list(map(first,
  14. partitionby(identity,
  15. [1, 1, 1, 2, 3, 3, 2, 2, 3]))) ==
  16. [1, 2, 3, 2, 3])
  17. assert ''.join(map(first,
  18. partitionby(identity, "Khhhaaaaannnnn!!!!"))) == 'Khan!'