15 KiB
title, library_name, pypi_package, category, python_compatibility, last_updated, official_docs, official_repository, maintenance_status
| title | library_name | pypi_package | category | python_compatibility | last_updated | official_docs | official_repository | maintenance_status |
|---|---|---|---|---|---|---|---|---|
| Boltons: Pure-Python Standard Library Extensions | boltons | boltons | utilities | 3.7+ | 2025-11-02 | https://boltons.readthedocs.io | https://github.com/mahmoud/boltons | active |
Boltons: Pure-Python Standard Library Extensions
Overview
boltons should be builtins.
Boltons is a collection of over 230 BSD-licensed, pure-Python utilities designed to extend Python's standard library with functionality that is conspicuously missing. Created and maintained by @mahmoud (Mahmoud Hashemi), it provides battle-tested implementations of commonly needed utilities without any external dependencies.
Core Value Proposition
- Zero Dependencies: Pure-Python with no external requirements
- Module Independence: Each module can be vendored individually
- Battle-Tested: 6,765+ stars, tested against Python 3.7-3.13 and PyPy3
- Standard Library Philosophy: Follows stdlib design principles
- Production Ready: Used in production by numerous projects
Problem Space
Boltons solves the "reinventing the wheel" problem for common utilities that should be in the standard library but aren't. Without boltons, developers repeatedly write custom implementations for:
- LRU caches with better APIs than
functools.lru_cache - Chunked and windowed iteration patterns
- Atomic file operations
- Advanced dictionary types (OrderedMultiDict)
- Enhanced traceback formatting and debugging
- Recursive data structure traversal
- File system utilities beyond
shutil
What Would Be Reinventing the Wheel
Using boltons prevents rewriting:
- Custom LRU cache implementations with size limits and TTL
- Iteration utilities like
chunked(),windowed(),unique() - Atomic file write operations (write-to-temp, rename)
- Enhanced
namedtuplewith defaults and mutation - Traceback extraction and formatting utilities
- URL parsing and manipulation beyond
urllib.parse - Table formatting for 2D data
Design Principles
Per @boltons/docs/architecture.rst, each "bolton" must:
- Be pure-Python and self-contained: No C extensions, minimal dependencies
- Perform a common task: Address frequently needed functionality
- Mitigate stdlib insufficiency: Fill gaps in the standard library
- Follow stdlib practices: Balance best practice with pragmatism
- Include documentation: At least one doctest, links to related tools
Key Modules
1. cacheutils - Advanced Caching [@context7]
Better caching than functools.lru_cache:
from boltons.cacheutils import LRU, cached, cachedmethod
# LRU cache with size limit
cache = LRU(max_size=256)
cache['user:123'] = user_data
# Decorator with custom cache backend
@cached(cache={})
def fibonacci(n):
if n < 2:
return n
return fibonacci(n-1) + fibonacci(n-2)
# Threshold counter - only track frequently occurring items
from boltons.cacheutils import ThresholdCounter
tc = ThresholdCounter(threshold=0.1)
tc.update([2] * 10) # Only remembers items > 10% frequency
When to use: Need size-limited caches, TTL expiration, or custom eviction policies.
2. iterutils - Enhanced Iteration [@context7]
Powerful iteration utilities beyond itertools:
from boltons.iterutils import (
chunked, chunked_iter, # Split into chunks
windowed, windowed_iter, # Sliding windows
unique, unique_iter, # Deduplicate preserving order
one, first, same, # Reduction utilities
remap, get_path, # Recursive data structure traversal
backoff, # Exponential backoff with jitter
pairwise # Overlapping pairs
)
# Chunking for batch processing
for batch in chunked(user_ids, 100):
process_batch(batch)
# [1,2,3,4,5] with size=2 → [1,2], [3,4], [5]
# Sliding window for moving averages
for window in windowed(prices, 7):
avg = sum(window) / len(window)
# [1,2,3,4,5] with size=3 → [1,2,3], [2,3,4], [3,4,5]
# Safe reduction
user = one(users) # Raises if != 1 item
first_or_none = first(results, default=None)
# Recursive data structure traversal
def visit(path, key, value):
if isinstance(value, str) and 'secret' in key.lower():
return '***REDACTED***'
return value
clean_data = remap(user_data, visit=visit)
# Exponential backoff with jitter
for wait_time in backoff(start=0.1, stop=60, count=5, jitter=True):
if try_operation():
break
time.sleep(wait_time)
When to use: Batch processing, sliding windows, recursive data transformation, retry logic.
3. tbutils - Enhanced Tracebacks [@context7]
Better exception handling and debugging:
from boltons.tbutils import TracebackInfo, ExceptionInfo, ParsedException
try:
risky_operation()
except Exception as e:
# Capture full traceback info
exc_info = ExceptionInfo.from_current()
# Access structured traceback data
tb_info = TracebackInfo.from_current()
for frame in tb_info.frames:
print(f"{frame.filename}:{frame.lineno} in {frame.func_name}")
# Format for logging
formatted = exc_info.get_formatted()
logger.error(formatted)
When to use: Enhanced error logging, debugging tools, error analysis.
4. fileutils - Safe File Operations [@context7]
Atomic writes and safe file handling:
from boltons.fileutils import atomic_save, mkdir_p, FilePerms
# Atomic file write (write-to-temp, rename)
with atomic_save('config.json') as f:
json.dump(config, f)
# File only replaced if write succeeds
# Create directory path (like mkdir -p)
mkdir_p('/path/to/nested/directory')
# Readable permission management
perms = FilePerms(0o755)
perms.apply('/path/to/script.sh')
When to use: Configuration files, data persistence, safe concurrent writes.
5. dictutils - Advanced Dictionaries [@context7]
Enhanced dictionary types:
from boltons.dictutils import OrderedMultiDict, OMD
# Preserve order + allow duplicate keys (like HTTP headers)
headers = OMD([
('Accept', 'application/json'),
('Accept', 'text/html'), # Multiple values for same key
('User-Agent', 'MyBot/1.0')
])
for accept in headers.getlist('Accept'):
print(accept) # application/json, text/html
When to use: HTTP headers, query parameters, configuration with duplicate keys.
6. strutils - String Utilities [@github/README.md]
Common string operations:
from boltons.strutils import (
slugify, # URL-safe slugs
bytes2human, # Human-readable byte sizes
find_hashtags, # Extract #hashtags
pluralize, # Smart pluralization
strip_ansi # Remove ANSI codes
)
slugify("Hello, World!") # "hello-world"
bytes2human(1234567) # "1.18 MB"
7. queueutils - Priority Queues [@context7]
Enhanced queue types:
from boltons.queueutils import HeapPriorityQueue, PriorityQueue
pq = HeapPriorityQueue()
pq.add("low priority", priority=3)
pq.add("high priority", priority=1)
item = pq.pop() # Returns "high priority"
Integration Patterns
Full Install
pip install boltons
Import Individual Modules
# Import only what you need
from boltons.cacheutils import LRU
from boltons.iterutils import chunked
from boltons.fileutils import atomic_save
Vendoring (Copy Into Project)
Since boltons has zero dependencies and each module is independent:
# Copy specific module
cp /path/to/site-packages/boltons/iterutils.py myproject/utils/
# Copy entire package
cp -r /path/to/site-packages/boltons myproject/vendor/
This is explicitly supported by the project design [@context7/architecture.rst].
Real-World Usage Examples [@github/search]
Example 1: Clastic Web Framework [@mahmoud/clastic]
# Enhanced traceback handling
from boltons.tbutils import ExceptionInfo, TracebackInfo
class ErrorMiddleware:
def handle_error(self, exc):
exc_info = ExceptionInfo.from_current()
return self.render_error_page(exc_info.get_formatted())
Example 2: Click-Extra CLI Framework [@kdeldycke/click-extra]
# Enhanced traceback formatting for CLI error messages
from boltons.tbutils import print_exception
try:
run_command()
except Exception:
print_exception() # Beautiful formatted traceback
Example 3: Reader Feed Library [@lemon24/reader]
# Type checking utilities
from boltons.typeutils import make_sentinel
NOT_SET = make_sentinel('NOT_SET') # Better than None for defaults
Example 4: Batch Processing Pattern
from boltons.iterutils import chunked
# Process database records in batches
for batch in chunked(fetch_all_records(), 1000):
bulk_insert(batch)
db.commit()
Example 5: API Rate Limiting
from boltons.iterutils import backoff
from boltons.cacheutils import LRU
# Exponential backoff for API retries
cache = LRU(max_size=1000)
def call_api_with_retry(endpoint):
for wait in backoff(start=0.1, stop=60, count=5):
try:
return requests.get(endpoint)
except requests.HTTPError as e:
if e.response.status_code == 429: # Rate limited
time.sleep(wait)
else:
raise
Python Version Compatibility
- Minimum: Python 3.7
- Maximum Tested: Python 3.13
- Also Tested: PyPy3
- 3.11-3.14 Status: Fully compatible (tested 3.11, 3.12, 3.13)
Per @github/README.md:
Boltons is tested against Python 3.7-3.13, as well as PyPy3.
When to Use Boltons
Use Boltons When
-
Need stdlib-style utilities with no dependencies
- Building libraries that avoid dependencies
- Corporate environments with strict dependency policies
- Want vendorable, copy-pasteable code
-
Iteration patterns beyond itertools
- Chunking/batching data
- Sliding windows
- Recursive data structure traversal
- Exponential backoff
-
Enhanced caching needs
- Size-limited LRU caches
- TTL expiration
- Custom eviction policies
- Better API than
functools.lru_cache
-
Atomic file operations
- Safe configuration file updates
- Preventing corrupted writes
- Concurrent file access
-
Advanced debugging
- Structured traceback information
- Custom error formatting
- Error analysis tools
-
OrderedMultiDict needs
- HTTP headers/query parameters
- Configuration with duplicate keys
- Preserving insertion order + duplicates
Use Standard Library When
- Basic iteration:
itertoolssuffices - Simple caching:
functools.lru_cacheis enough - Basic file ops:
pathlibandshutilwork fine - Standard dicts:
dictorcollections.OrderedDictmeets needs
Use more-itertools When
- Need even more specialized iteration utilities
- Already using
more-itertoolsin project - Want community recipes from itertools docs
Key Difference: Boltons is broader (files, caching, debugging) while more-itertools focuses purely on iteration.
Decision Matrix
| Scenario | Use Boltons | Use Stdlib | Use Alternative |
|---|---|---|---|
| LRU cache with size limits | ✅ cacheutils.LRU |
⚠️ lru_cache (no size control) |
cachetools (more features) |
| Chunked iteration | ✅ iterutils.chunked |
❌ Manual slicing | more-itertools.chunked |
| Atomic file writes | ✅ fileutils.atomic_save |
❌ Manual temp+rename | atomicwrites (archived) |
| Enhanced tracebacks | ✅ tbutils.TracebackInfo |
❌ traceback (basic) |
rich.traceback (prettier) |
| OrderedMultiDict | ✅ dictutils.OMD |
❌ Custom solution | werkzeug.datastructures |
| Exponential backoff | ✅ iterutils.backoff |
❌ Manual implementation | tenacity, backoff |
| URL parsing | ✅ urlutils.URL |
⚠️ urllib.parse (basic) |
yarl, furl |
| Zero dependencies | ✅ Pure Python | ✅ Built-in | ❌ Most alternatives |
When NOT to Use Boltons
-
Already using specialized libraries
- Have
cachetoolsfor advanced caching - Have
tenacityfor retry logic - Have
richfor pretty output
- Have
-
Need high-performance implementations
- Boltons prioritizes correctness over speed
- C-extension alternatives may be faster
-
Want cutting-edge features
- Boltons is conservative, stdlib-like
- Specialized libraries may innovate faster
-
Framework-specific needs
- Django/Flask have their own utils
- Web frameworks provide similar functionality
Maintenance and Stability
- Versioning: CalVer (YY.MINOR.MICRO) [@github/README.md]
- Latest: 25.0.0 (February 2025)
- Maintenance: Active, 71 open issues, 373 forks
- Author: Mahmoud Hashemi (@mahmoud)
- License: BSD (permissive)
Related Libraries
Complementary
- more-itertools: Extended iteration recipes
- toolz/cytoolz: Functional programming utilities
- attrs/dataclasses: Enhanced class definitions
Overlapping
- cachetools: More advanced caching (but has dependencies)
- atomicwrites: Atomic file writes (now archived)
- werkzeug: Web utilities including MultiDict
When to Combine
# Use both boltons and more-itertools
from boltons.iterutils import chunked # For chunking
from more_itertools import flatten # For flattening
from boltons.cacheutils import LRU # For caching
cache = LRU(max_size=1000)
@cached(cache=cache)
def process_data(records):
for batch in chunked(records, 100):
yield process_batch(batch)
Key Takeaways
- Zero Dependencies: Pure-Python, no external requirements
- Vendorable: Copy individual modules into your project
- Battle-Tested: 6,765+ stars, production-proven
- Stdlib Philosophy: Familiar API, conservative design
- Broad Coverage: Caching, iteration, files, debugging, data structures
- Production Ready: Python 3.7-3.13, PyPy3 support
Quick Start
# Install
pip install boltons
# Common patterns
from boltons.cacheutils import LRU
from boltons.iterutils import chunked, windowed, backoff
from boltons.fileutils import atomic_save
from boltons.tbutils import ExceptionInfo
# LRU cache
cache = LRU(max_size=256)
# Batch processing
for batch in chunked(items, 100):
process(batch)
# Atomic writes
with atomic_save('data.json') as f:
json.dump(data, f)
# Enhanced error handling
try:
risky()
except Exception:
exc_info = ExceptionInfo.from_current()
logger.error(exc_info.get_formatted())
References
- Repository: @github/mahmoud/boltons
- Documentation: @readthedocs
- PyPI: @pypi/boltons
- Context7: @context7/mahmoud/boltons
- Architecture: @readthedocs/architecture
Research completed: 2025-10-21 Sources: Context7, GitHub, PyPI, ReadTheDocs, Exa code search Trust Score: 9.8/10 (Context7)