Pytools is a comprehensive collection of utilities designed to augment the Python standard library, offering a diverse set of tools for various programming needs. It includes functionalities for mathematical operations, persistent key-value stores, graph algorithms, and object array handling. Maintained by Andreas Kloeckner, it serves primarily as a dependency for his other software packages but provides valuable utilities for direct use. The library is actively developed, with frequent releases, currently at version 2026.1.
pip install pytoolsVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use `pytools.memoize.memoize_method` to cache the results of an expensive method, preventing redundant computations for the same inputs. Subsequent calls with identical arguments will return the cached value instantly.
Review release notes for specific migration paths or alternatives if using older versions of the `Tag` system.
Migrate logging-related imports and usage to `logpyle` by installing it separately (`pip install logpyle`) and updating import paths.
Consult the official documentation (e.g., for specific submodule APIs) rather than expecting intuitive top-level functions for all tasks.
Ensure your project's environment uses Python 3.10 or a more recent compatible version.
Install the library using pip: ```bash pip install pytools ```
Import `gcd` from `pytools.arithmetic` or access it as `pytools.arithmetic.gcd`: ```python import pytools.arithmetic result = pytools.arithmetic.gcd(10, 15) # Or from pytools.arithmetic import gcd result = gcd(10, 15) ```
To 'modify' a `frozendict`, create a new one with the desired changes, typically by merging it with another dictionary or reconstructing it:
```python
from pytools.immutable_collection import frozendict
d = frozendict({"a": 1, "b": 2})
# To add/change an item (returns a new frozendict)
new_d = d.update({"c": 3})
print(new_d) # frozendict({'a': 1, 'b': 2, 'c': 3})
# To 'remove' an item (construct a new frozendict)
new_d_without_b = frozendict({k: v for k, v in d.items() if k != 'b'})
print(new_d_without_b) # frozendict({'a': 1})
```Ensure the directory where the persistent dictionary file is stored has appropriate write permissions for the executing user, or choose a different, writable path for the data file:
```python
from pytools import persistent_dict
# Option 1: Ensure '/tmp/' is writable
d = persistent_dict("/tmp/mypersistentdict.dat")
# Option 2: Use a user-specific writable directory
import os
user_data_dir = os.path.join(os.path.expanduser('~'), '.pytools_data')
os.makedirs(user_data_dir, exist_ok=True)
d = persistent_dict(os.path.join(user_data_dir, "mypersistentdict.dat"))
```