toolz

raw JSON →
1.1.0 verified Tue May 12 auth: no python install: verified quickstart: verified maintenance

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.

pip install toolz
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.
fix Always wrap lazy function calls with `list()`, `tuple()`, or iterate over them if an immediate, concrete collection is required, e.g., `my_list = list(toolz.itertoolz.filter(predicate, sequence))`.
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.
fix To use the faster implementation, `pip install cytoolz`. Ensure `cytoolz` is installed in your environment, and `toolz` functions will often defer to the Cython versions if available.
breaking As of version 1.1.0, `toolz` officially dropped support for Python 3.8 and PyPy 3.8.
fix Upgrade your Python environment to Python 3.9 or newer. For projects requiring Python 3.8, use an older `toolz` version (e.g., 1.0.0 or earlier).
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'.
fix Be aware that new features or significant API changes are unlikely. Rely on the existing, stable API. Contribute bug fixes or compatibility updates if needed, but major feature proposals might not be integrated.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.03s 18.4M
3.10 alpine (musl) - - 0.04s 18.4M
3.10 slim (glibc) wheel 1.5s 0.02s 19M
3.10 slim (glibc) - - 0.02s 19M
3.11 alpine (musl) wheel - 0.07s 20.4M
3.11 alpine (musl) - - 0.07s 20.4M
3.11 slim (glibc) wheel 1.7s 0.06s 21M
3.11 slim (glibc) - - 0.05s 21M
3.12 alpine (musl) wheel - 0.06s 12.2M
3.12 alpine (musl) - - 0.07s 12.2M
3.12 slim (glibc) wheel 1.5s 0.06s 13M
3.12 slim (glibc) - - 0.06s 13M
3.13 alpine (musl) wheel - 0.06s 11.9M
3.13 alpine (musl) - - 0.06s 11.8M
3.13 slim (glibc) wheel 1.5s 0.07s 12M
3.13 slim (glibc) - - 0.06s 12M
3.9 alpine (musl) wheel - 0.03s 17.9M
3.9 alpine (musl) - - 0.03s 17.9M
3.9 slim (glibc) wheel 1.8s 0.03s 18M
3.9 slim (glibc) - - 0.03s 18M

This example demonstrates how to build a word counting function using `compose`, `frequencies`, and the curried `map` from `toolz` to process a sentence.

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}