libb-util Documentation
A comprehensive collection of utility functions and classes designed to enhance productivity and simplify common programming tasks in Python.
Note
All utilities should be imported from the top-level libb package:
from libb import Setting, compose, attrdict, timeout
Function Reference
Core Utilities
Configuration - Settings and environment management
Dict where |
|
Abstract base class for loading options from config.py. |
|
Wrapper that builds dataclass options from config file. |
|
Configure environment settings at runtime. |
|
Patch a library's config module directly in sys.modules. |
|
Context manager to safely modify a setting with unlock/lock protection. |
|
Get temporary directory setting from environment or system default. |
|
Get vendor directory setting from environment or system default. |
|
Get output directory setting from environment or system default. |
|
Get local data directory setting using platform-appropriate location. |
Classes - Class manipulation and decorators
Create property getters/setters for private attributes. |
|
Include dictionary items as class attributes during class declaration. |
|
Decorator that enforces singleton pattern on a class. |
|
Decorator that caches function results based on arguments. |
|
Decorator that creates computed properties at the class level. |
|
Delegate attribute access to another object. |
|
Decorator that makes a property lazy-evaluated. |
|
Decorator combining @property and @staticmethod with caching. |
|
Decorator allowing a method to work as both static and instance method. |
|
Mapping class that references values weakly. |
|
Class factory that resolves metaclass conflicts automatically. |
|
Dynamically extend an instance's class hierarchy at runtime. |
|
Find the ultimate non-object base class in an inheritance hierarchy. |
|
Decorator that catches and reports exceptions without re-raising. |
|
Metaclass that wraps all methods with exception catching. |
Functions - Function composition and decorators
Check if a function is an instance method. |
|
Find decorators applied to functions in a target module/class. |
|
Return a function folding over a list of functions. |
|
Decorator that takes a list of decorators to be composed. |
|
Decorator to copy the docstring of another function. |
|
Find the calling function in many common cases. |
|
Decorator to repeat a function multiple times. |
|
Decorator to log function execution time. |
|
Decorator to suppress warnings during function execution. |
|
Multimethod that supports args (no kwargs by design). |
|
Decorator for type-based method dispatch (multiple dispatch). |
Iterators - Iterator utilities and sequence operations
Break iterable into lists of length n: |
|
Break iterable into lists of approximately length n. |
|
Recursively flatten nested lists/tuples into a single list. |
|
Remove falsy values from an iterable (including None and 0). |
|
Group elements from iterable into fixed-length groups of length n. |
|
Create a dictionary from iterable using a key function. |
|
Create an iterator that cycles infinitely through items. |
|
Check if object is a collection (iterable and not a string). |
|
Check if object is iterable (excluding strings). |
|
Check if object is a sequence (excluding strings). |
|
Returns a 2-tuple of iterables derived from the input iterable. |
|
Peel iterator one by one, yield item, aliasor item, item |
|
Visit input iterables in a cycle until each is exhausted. |
|
Peel iterator one by one, yield alias if tuple, else item" |
|
Remove duplicate elements while preserving order. |
|
Yield unique elements, preserving order. |
|
Compare two lists and check if elements in ref appear in same order in comp. |
|
Return first non-None value. |
|
Safe sequence indexing with default value |
|
Back-fill a sorted array with the latest value. |
|
Back-fill a sorted iterdict with the latest values. |
|
Given two lists of dicts ('iterdicts'), sorted on some attribute, build a single list with dicts, with keys within a given tolerance anything that cannot be aligned is DROPPED |
Text - Text processing and encoding
Generate a random alphanumeric string. |
|
Use ftfy magic to fix text encoding issues. |
|
Convert underscore_delimited_text to camelCase. |
|
Convert camelCase to snake_case. |
|
Remove non-ASCII characters from a string. |
|
Replace vulgar fractions with decimal equivalents. |
|
Round a numeric string to specified decimal places. |
|
Extract number from string. |
|
Truncate a string to max width characters. |
|
Apply rot13-like translation to string. |
|
Decode base64 encoded words with intelligent charset handling. |
|
Convert a string representation of truth to boolean. |
|
Search for term in a list of items using fuzzy matching. |
|
Check if value can be converted to a float. |
Formatting - String and number formatting
Float subclass that marks values for percentage formatting in display tables. |
|
Capitalize with special handling for known abbreviations. |
|
Capitalize words in a string, accommodating acronyms. |
|
Add commas to a numeric value. |
|
Alias for |
|
Format a numeric value with various formatting options. |
|
Reformat phone numbers for display. |
|
Format seconds as human-readable time delta. |
|
Format a timedelta as human-readable string. |
|
Format a time interval as human-readable string. |
|
Split and capitalize string by delimiter (or camelcase). |
|
Convert string to title case using python-titlecase library. |
Path - Path and module utilities
Add a path to the Python system search path. |
|
Context manager to safely change working directory. |
|
Get the directory containing a module. |
|
Return name of script being run, without file extension. |
Dictionaries - Dictionary manipulation
Check if something is a mapping (dict-like). |
|
Invert a dictionary, swapping keys and values. |
|
Apply a function to all keys in a dictionary. |
|
Apply a function to all values in a dictionary. |
|
Flatten a dictionary, recursively flattening nested dicts. |
|
Recursively convert dict into list of tuples. |
|
Context manager for temporarily patching a dictionary value. |
|
Context manager for temporarily monkey patching an object attribute. |
|
Python 2 style cmp function with null value handling. |
|
Sort list of dictionaries by list of keys. |
|
Simulate a Python 2-like map with longest iterable behavior. |
|
Get class attributes (excluding methods and dunders). |
|
Trace dictionary key in nested dictionary. |
|
Get values at all locations of a key in nested dictionary. |
|
Insert a value into a dict at the path specified by vector. |
|
Recursively merge two dictionaries, including nested dictionaries and iterables. |
Module - Module loading and manipulation
Wrapper to override __getattr__ of a Python module. |
|
Import a dotted module name and return the innermost module. |
|
Get a class by its fully qualified name. |
|
Get all classes in a module that are subclasses of parentcls. |
|
Get a function by name from a module. |
|
Load a module from a file path. |
|
Patch and load a module with regex substitutions. |
|
Replace a source module with a target module in sys.modules. |
|
Create an instance of a class by its fully qualified name. |
|
Create a mock module with specified attributes. |
|
Virtual module with submodules sourced from other modules. |
|
Create a virtual module with submodules from other modules. |
|
Get package info for modules, useful for pytest conftest loading. |
|
Get package paths within modules, useful for pytest conftest loading. |
|
Import a module using a custom name to avoid local name conflicts. |
Type Definitions - Type aliases
alias of |
|
alias of |
|
Built-in immutable sequence. |
Collections
Attribute Dictionaries - Dict subclasses with attribute access
A dictionary subclass that allows attribute-style access. |
|
A dictionary where function values are lazily evaluated. |
|
A dictionary that returns None for non-existing keys without raising exceptions. |
|
Bidirectional dictionary that allows multiple keys with the same value. |
|
An ordered dictionary with additional insertion methods. |
|
A case-insensitive dictionary-like object. |
Ordered Set - Set with insertion order
A set that maintains insertion order using a doubly linked list. |
Heap - Priority queue with custom comparator
Heap with custom key comparator. |
Input/Output
CSV/JSON - Data serialization
Render rows as CSV string. |
|
Zipped CSV file that handles file permissions correctly on DOS. |
|
Convert an iterable that yields bytestrings to a read-only input stream. |
|
Parse ASCII encoded JSON from file handle. |
|
Parse ASCII encoded JSON from text string. |
|
Context manager to suppress stdout (print statements). |
|
Decorator version of suppress_print context manager. |
Stream - TTY and stream utilities
Check if running in a terminal. |
|
Check if a stream is running in a terminal. |
Process - Process management
Find processes by name that are listening on a port. |
|
Find a process by name listening on a specific port. |
|
Kill processes matching name and/or version. |
Signals - Signal handling
Map of signal numbers to signal names. |
|
Context manager that suppresses SIGINT & SIGTERM during a block. |
MIME - MIME type utilities
Guess mimetype from a URL or filename. |
|
Guess file extension for a mimetype. |
|
Detect mimetype using the |
Directory - File system operations
Create directory and any missing parent directories. |
|
Context manager to wrap a temporary directory with auto-cleanup. |
|
Expand path to absolute path with environment variables and user expansion. |
|
Create a nested dictionary representing the folder structure. |
|
Search for files by name, extension, or both in directory. |
|
Move a file to a new location, optionally deleting anything in the way. |
|
Save a document to the specified temp directory, optionally with date. |
|
Get paths of existing files matching each glob pattern. |
|
Load file contents from directory matching pattern. |
|
Load files from temp directory matching patterns. |
|
Convert directory structure to a dictionary. |
|
Download file from URL with progress bar and retry logic. |
|
Split path into all its components. |
|
Split path by multiple separators. |
Specialized
Statistics - Math and statistics
Decorator to convert args to numpy format and results back to Python. |
|
Compute average of array, ignoring None/NaN values. |
|
Compute percent change between consecutive elements. |
|
Compute one-period difference between consecutive elements. |
|
Round to nearest integer if within threshold distance. |
|
Check if value is a numeric type. |
|
Count number of integer digits in a number. |
|
Convert value to numeric type, handling common formatting. |
|
Extract number from string. |
|
Round number to the nearest tick value. |
|
Compute population covariance between x and y. |
|
Compute sample covariance between x and y. |
|
Compute population variance of x. |
|
Compute sample variance of x. |
|
Compute population standard deviation. |
|
Compute sample standard deviation. |
|
Compute beta of x with respect to index (typically over returns). |
|
Compute correlation between x and y. |
|
Compute R-squared (coefficient of determination) between x and y. |
|
Compute simple returns between consecutive values. |
|
Compute log returns between consecutive values. |
|
Compute a weighted average of field in a DataSet using weight_field as the weight. |
|
Compute the least-squares linear regression line for the set of points. |
|
Compute the distance from each point to the line defined by m and b. |
|
Linearly interpolate y between y0 and y1 based on x's position. |
|
Safely divide numpy arrays, returning 0 where divisor is 0. |
|
Safely add numbers, returning None if any argument is None. |
|
Safely subtract numbers, returning None if any argument is None. |
|
Safely divide numbers, returning None if any arg is None. |
|
For big lists of stuff to multiply, when some things may be None |
|
Safely round a number, returning None if argument is None. |
|
Compare two values using a comparison operator. |
|
Min returns None if it is in the list - this one returns the min value |
|
Max returns None if it is in the list - this one returns the max value |
|
Convert mixed numeral string to decimal fraction. |
|
Convert decimal or fraction to mixed numeral string. |
|
Round value to nearest multiple of base. |
|
Smooth the data using a window with requested size. |
|
Compute binomial coefficient (n choose k). |
Threading - Concurrency utilities
Decorator to run synchronous function asynchronously. |
|
Call function and set result on future. |
|
Thread pool executor with rate limiting and Request/Response API. |
|
Request to process an item with optional ID for tracking. |
|
Response from processing a request. |
|
Decorator to run function in a separate thread. |
Synchronization - Timing and synchronization
Decorator to synchronize functions with a shared lock. |
|
Non-blocking delay for checking time elapsed. |
|
Delay non-blocking for N seconds (busy-wait). |
|
Decorator to debounce function calls. |
|
Calculate time to wait until specified hour/minute/second. |
|
Context manager for timing out potentially hanging code. |
Cryptography - Encoding utilities
Encode file contents as base64. |
|
Converts a string to an integer by concatenating hex values of characters. |
Geographic - Coordinate transformations
Project longitude into mercator / radians from major axis. |
|
Project latitude into mercator / radians from major/minor axes. |
Random - OS-seeded random functions
Random choice from a list, seeded with OS entropy. |
|
Random integer between a and b inclusive, seeded with OS entropy. |
|
Random sample of N elements from numpy array. |
|
Random float in [0, 1), seeded with OS entropy. |
Exceptions - Error handling
Print exception traceback with optional verbosity. |
|
Wrap function to return default value if it fails. |
Charts - Visualization
Create a matplotlib timeseries plot with automatic subplot layout. |
Pandas - DataFrame utilities
Check if value is null/None (pandas required). |
|
Download timezone data for pyarrow date wrangling. |
|
Downcast DataFrame to minimum viable type for each column. |
|
Merge two DataFrames using fuzzy matching on specified columns. |
Web - Web application utilities
Get existing model instance or create new one (Django-style). |
|
Decorator to pass in default order/page/per page for pagination. |
|
Sleep for a random amount of time. |
|
Decorator that retries function with random delays. |
|
Wrap a web.py controller with CORS headers. |
|
Wrap a Flask controller with CORS headers. |
|
Decorator that checks if user meets an auth criterion. |
|
Generate cross-site request forgery protection token. |
|
Decorator protecting PUT/POST requests from session riding. |
|
Check if key has valid format. |
|
Decorator requiring valid API key for controller access. |
|
Generate URL with query parameters. |
|
Add prefixes to web.py URL mappings. |
|
Normalize URL parts and join them with a slash. |
|
Return first non-empty element from each sequence. |
|
Safely join untrusted path components to a base directory. |
|
Find template in working directory or static folder. |
|
Read file contents for injection into HTML email templates. |
|
Generate base64 data URI for image embedding in HTML. |
|
Build breadcrumb HTML from web.py app_stack. |
|
Patch URL mapping into web.py subapps for breadcrumbs. |
|
Build HTML menu from web.py URL mapping. |
|
Scale a hex color by a percentage. |
|
Render form field with error styling. |
|
Decorator protecting routes by session authentication. |
|
Decorator limiting access to own user ID unless admin. |
|
Decorator limiting access to managed resources unless admin. |
|
Wrap internalerror function to log tracebacks. |
|
Check if address is a valid IPv6 address. |
|
Check if address is a valid IPv4 address. |
|
Check if port is a valid port number. |
|
Parse IP address and port from string. |
|
Parse address as IP:port tuple or Unix socket path. |
|
Quote string for safe use in a URL. |
|
Format datetime object for HTTP headers. |
|
Parse HTTP date string into datetime object. |
|
Encode text for safe use in HTML. |
|
Decode HTML-encoded text. |
|
Convert value to safe Unicode HTML string. |
|
JSON encoder that serializes dates in ISO format. |
|
JSON decoder that parses date strings into datetime objects. |
|
WSGI middleware for profiling requests. |
|
Default kwargs for cookielib.Cookie constructor. |
Windows - Windows-specific utilities
Execute a shell command and return output. |
|
Context manager for running psexec commands. |
|
Context manager for temporarily mounting a file share. |
|
Mount or unmount the admin$ share required for psexec commands. |
|
Mount or unmount a Windows file share. |
|
Parse output from WMIC query into list of dicts. |
|
Kill all running cmd.exe processes via WMI. |
Design Philosophy
Transparent API: Always import from top-level
libb, never from submodulesGraceful Dependencies: Optional dependencies wrapped for clean imports
Short, Singular Names: Modules use concise names (e.g.,
funcnotfuncutils)Comprehensive Exports: All modules explicitly define their public API via
__all__
Getting Started