Boltons

Like builtins, but boltons. Constructs/recipes/snippets that would be handy in the standard library. Nothing like Michael Bolton.

https://boltons.readthedocs.org

Boltons is a set of pure-Python utilities in the same spirit as — and yet conspicuously missing from — the standard library, including:

  • Atomic file saving, bolted on with fileutils
  • A highly-optimized OrderedMultiDict, in dictutils
  • Two types of PriorityQueue, in queueutils
  • Chunked and windowed iteration, in iterutils
  • A full-featured TracebackInfo type, for representing stack traces, in tbutils
  • A lightweight UTC timezone available in timeutils.
  • Recursive mapping for nested data transforms, with remap

Installing

pip install boltons

Boltons is tested against Python 2.6, 2.7, 3.4, 3.5, and PyPy.

Philosophy: Good Enough

  • Cover basic use
  • Docs recommend 3rd-party libraries

Architecture

  • Pure Python
  • No dependencies
  • Self-contained
  • Many utils.py can be copied
  • Themed-packages

In [ ]:
# Some imports used in examples
import time
from datetime import datetime
from pprint import pprint

Caching - cacheutils

  • LRI least-recently inserted
  • LRU least-recently used
  • cached and cachedmethod decorators

In [ ]:
from boltons.cacheutils import cached, LRU
my_cache = LRU(max_size=10)

@cached(my_cache)
def expensive(n=1):
    print(datetime.now())
    time.sleep(n)
    return n

In [ ]:
print(expensive(2))

In [ ]:
print(expensive(1))

In [ ]:
print(expensive(2))

Mappings - dictutils

  • OrderedMultiDict - ordered dict + methods
    • Counter functionality
    • .inverted()
    • Support for reversed()

In [ ]:
from boltons.dictutils import OrderedMultiDict

omd = OrderedMultiDict()
omd['a'] = 1
omd['b'] = 2
print(omd)

In [ ]:
omd.add('a', 3)
print(omd)

In [ ]:
print(omd.get('a'))
print(omd.getlist('a'))

In [ ]:
print(list(omd))
print(list(reversed(omd)))

Runtime Ecosystem - ecoutils

  • Executable runtime
  • Language version
  • Host OS
  • OS Features
  • Built-in libraries (OpenSSL, SQLite, etc.)
  • Machine info

In [ ]:
from boltons.ecoutils import get_profile
print(get_profile().keys())

In [ ]:
pprint(get_profile()['python']['features'])

Filesystem Helpers - fileutils

  • Atomic file saving
  • File permissions

functools improvements - funcutils

  • wraps modeled after functools.wraps
    • Retains original signature
  • Improved partial
  • Metaprogramming

itertools imporovements - iterutils

  • .split() - like str.split() for iterables
  • chunked() - get chunks of iterables
  • bucketize() - categorize iterable values
  • More!

Math helpers - mathutils

  • clamp
  • ceil
  • floor

Queues - queueutils

  • Priority queues
  • Variations (on sorting)

In [ ]:
from boltons.queueutils import PriorityQueue
pq = PriorityQueue()

In [ ]:
pq.add('low priority', 0)
pq.add('hi priority', 2)
pq.add('medium priority a', 1)
pq.add('medium priority b', 1)
print(len(pq))

In [ ]:
while pq:
    print(pq.pop())

Statistics basics - statsutils

  • mean()
  • median()
  • variance()
  • Other basic helpers

Text manipulation - strutils

  • Camel-casing
  • slugify()
  • ordinalize()
  • cardinalize()
  • pluralize()
  • ASCII, HTML, ANSI, etc.

Others

  • debugutils - Debugging help
  • formatutils - String formatting
  • gcutils - Garbage collection
  • ioutils - Spooled IO
  • jsonutils - JSON helpers
  • listutils - Special list types
  • namedutils - Simple containers
  • ...

Others (contd.)

  • ...
  • setutils - Set ordered list-set hybrid
  • socketutils - socket wrappers
  • tableutils - 2D data structures
  • tbutils - Tracebacks and call stacks
  • timeutils - datetime additions
  • typeutils - Type handling