toolz
toolz is a Python library providing a collection of utility functions for iterators, functions, and dictionaries, extending Python's standard `itertools` and `functools`. It promotes a functional programming style with features like composable, pure, and lazy operations. The current version is 1.1.0, and while the project is alive and maintained for critical bug fixes and Python version bumps, it is generally considered 'inactive' by its maintainers, who view it as mostly complete.
Warnings
- gotcha Many functions, especially within `itertoolz`, are 'lazy operators' and return iterators. Their results are not computed until explicitly consumed (e.g., by wrapping with `list()`, `tuple()`, or iterating over them). Expecting immediate list-like results without explicit conversion can lead to unexpected behavior or empty outputs.
- gotcha `toolz` has a high-performance Cython-based counterpart called `cytoolz`. If performance is critical, `cytoolz` is a drop-in replacement that offers the same API with C-optimized speed.
- breaking As of version 1.1.0, `toolz` officially dropped support for Python 3.8 and PyPy 3.8.
- deprecated The `toolz` project is described as 'alive but inactive' by its maintainers. While critical bug fixes and Python version bumps are addressed, active feature development and review of new contributions are not a primary focus, as the library is considered 'mostly complete'.
Install
-
pip install toolz
Imports
- compose
from toolz import compose
- frequencies
from toolz import frequencies
- map
from toolz.curried import map
- groupby
from toolz.itertoolz import groupby
Quickstart
from toolz import compose, frequencies
from toolz.curried import map
def stem(word):
""" Stem word to primitive form """
return word.lower().rstrip(",.!:;'-\"").lstrip("'\"")
wordcount = compose(frequencies, map(stem), str.split)
sentence = "This cat jumped over this other cat!"
result = wordcount(sentence)
print(result)
# Expected: {'this': 2, 'cat': 2, 'jumped': 1, 'over': 1, 'other': 1}