boltons

repository·master·Indexed 27 days ago

https://github.com/mahmoud/boltons

A collection of over 230 pure-Python utilities designed to augment the Python standard library. It provides optimized tools for file handling (atomic_save), data structures (OrderedMultiDict, FrozenDict, OneToOne, ManyToMany), iteration (chunked, windowed, remap), caching (LRU, LRI, @cached), and debugging (wrap_trace, pdb_on_signal).

Tokens
24.7K
Snippets
35
Records
229
Agent score
92%

What's inside boltons

  1. Overview of Boltons utilities

    master

    Boltons provides over 230 BSD-licensed, pure-Python utilities designed to fill gaps in the Python standard library. Key features include:

    • Atomic file saving: via boltons.fileutils.atomic_save.
    • OrderedMultiDict: a highly-optimized dictionary in boltons.dictutils.
    • PriorityQueue: two types of priority queues in boltons.queueutils.
    • Iteration utilities: chunked and windowed iteration in boltons.iterutils.
    • Recursive data structure manipulation: iteration and merging via boltons.iterutils.remap.
    • Exponential backoff: including jitter via boltons.iterutils.backoff.
    • TracebackInfo: a type for representing stack traces in boltons.tbutils.
  2. Use boltons.iterutils for enhanced iteration

    master
    The boltons.iterutils module provides improvements and extensions to the standard itertools library. It includes generators and list-producing functions for common iteration patterns not found in the standard library, such as chunking, windowing, and unique element extraction.
  3. Use boltons.formatutils for advanced string formatting

    master
    The boltons.formatutils module provides a toolbox of utilities that extend and enhance the standard Python str.format() capabilities. It is designed to provide more powerful and flexible string interpolation and formatting tools.
  4. Use boltons.namedutils for lightweight containers

    master
    The boltons.namedutils module provides lightweight container classes that allow accessing attributes by name, similar to a namedtuple but with more flexibility or different use cases. These containers are designed to be lightweight alternatives to full classes when you only need to group data with named accessors.
  5. Integrate boltons into your project

    master

    Boltons is designed for easy inclusion in any project because it has zero external dependencies. You can integrate it using one of two methods depending on your needs:

    1. Full Package Integration: Copy the entire boltons package into your project directory.
    2. Cherry-picking: Since individual modules are designed to be self-contained, you can copy only the specific utils.py file or module required by your project.
  6. Integrate Boltons modules via vendorization

    master
    Because Boltons is pure-Python and has no dependencies, and because each module is independent, you can vendorize individual modules by copying them directly into your project instead of installing the entire package.
  7. Download and process large files (e.g., ZIPs) using SpooledBytesIO

    master

    When downloading files via requests, use SpooledBytesIO to buffer the content. This allows you to keep small files in memory while automatically offloading large files to disk, which is particularly useful for processing ZIP files that require a compatible file-like object.

    from zipfile import ZipFile
    import requests
    from boltons import ioutils
    
    # Using a context manager with stream=True ensures the connection is closed.
    with requests.get("http://127.0.0.1/test_file.zip", stream=True) as r:
        if r.status_code == 200:
            with ioutils.SpooledBytesIO() as flo:
                for chunk in r.iter_content(chunk_size=64000):
                    flo.write(chunk)
    
                flo.seek(0)
    
                zip_doc = ZipFile(flo)
    
                # Print all the files in the zip
                print(zip_doc.namelist())
  8. Use BufferedSocket for socket wrapping

    master
    The boltons.socketutils.BufferedSocket class provides a wrapper around standard Python sockets to provide buffered operations. It includes specific exceptions that derive from socket.error to provide clearer error messages and better code clarity during socket operations.