boltons
boltons is a collection of over 250 pure-Python utilities designed to fill gaps in Python's standard library. It provides high-performance data structures and convenient functions for common tasks across various domains like file systems, iteration, caching, and time management. The library maintains an active development status, with major releases occurring roughly annually, interspersed with minor updates bringing enhancements and bug fixes. [1, 4, 5, 6, 8, 11]
Warnings
- breaking Version 24.0.0 dropped support for Python 2. boltons is now Python 3 only (3.7+). If you are on Python 2, you must pin your dependency to `boltons<24.0.0`.
- deprecated In version 25.0.0, the use of `datetime.utcnow()` was replaced internally due to its deprecation in Python 3.12. While this was an internal change, direct usage of `utcnow()` in user code, especially when interacting with boltons' `timeutils`, should be updated to `datetime.now(timezone.utc)` for future compatibility and best practice.
- gotcha Due to its nature as a collection of self-contained utilities with no external dependencies, boltons supports 'vendorization' of individual modules. If the full library is too large for your project, or for tighter integration, you can copy specific modules (e.g., `fileutils.py`) directly into your project.
Install
-
pip install boltons
Imports
- OrderedMultiDict
from boltons.dictutils import OrderedMultiDict
- remap
from boltons.iterutils import remap
- LRU
from boltons.cacheutils import LRU
- mkdir_p
from boltons.fileutils import mkdir_p
Quickstart
from boltons.dictutils import OrderedMultiDict
# Initialize the OrderedMultiDict
omd = OrderedMultiDict()
# Add items - supports multiple values for a single key
omd['fruit'] = 'apple'
omd['fruit'] = 'banana'
omd['vegetable'] = 'carrot'
# Access all values for a key
print(f"All fruits: {omd['fruit']}")
# Expected output: All fruits: ['apple', 'banana']
# Get the first value for a key
print(f"First fruit: {omd.get_first('fruit')}")
# Expected output: First fruit: apple
# Iterate through all key-value pairs (maintaining insertion order)
print("\nAll items:")
for key, value in omd.items():
print(f"{key}: {value}")
# Expected output:
# All items:
# fruit: apple
# fruit: banana
# vegetable: carrot