A Python utility belt containing simple tools, a stdlib like feel, and extra batteries
npx @tessl/cli install tessl/pypi-ubelt@1.4.0A Python utility belt containing simple tools, a stdlib-like feel, and extra batteries. UBelt provides a comprehensive collection of 150+ focused utilities that make everyday Python tasks shorter, cleaner, and more consistent across different platforms.
pip install ubeltimport ubelt as ubAll utilities are available at the top level:
import ubelt as ub
# Use any function directly
ub.cmd('ls')
ub.Timer()
ub.download('https://example.com/file.zip')import ubelt as ub
# Command execution with enhanced features
result = ub.cmd('ls -la', verbose=2)
print(result['out'])
# Progress iteration with timing and ETA
items = range(1000)
for item in ub.ProgIter(items, desc='Processing'):
# Do work
pass
# Dictionary operations and grouping
items = ['apple', 'banana', 'cherry', 'apricot']
grouped = ub.group_items(items, key=lambda x: x[0])
print(grouped) # {'a': ['apple', 'apricot'], 'b': ['banana'], 'c': ['cherry']}
# Caching computations
cache = ub.Cacher('my_cache', depends=['input_data'])
result = cache.tryload()
if result is None:
result = expensive_computation()
cache.save(result)
# Path operations
with ub.TempDir() as tmp:
fpath = tmp / 'example.txt'
ub.touch(fpath)
print(ub.augpath(fpath, suffix='_backup'))
# Timing operations
with ub.Timer('my_operation'):
time.sleep(1) # Replace with actual workUBelt is organized into 27 focused submodules, each containing related functionality:
All utilities follow consistent design patterns with optional parameters, sensible defaults, and cross-platform compatibility.
Comprehensive dictionary utilities including enhanced dict classes with set operations, grouping functions, and data structure manipulation tools.
class UDict(dict): ...
class AutoDict(dict): ...
def group_items(items, key): ...
def dict_hist(items, weights=None): ...
def dict_union(*args, **kwargs): ...
def find_duplicates(items, k=2): ...Execute shell commands with enhanced features, platform detection, and system integration utilities.
def cmd(command, shell=False, detach=False, verbose=0, **kwargs): ...
WIN32: bool
LINUX: bool
DARWIN: bool
def find_exe(name, **kwargs): ...Cross-platform path utilities, file operations, symbolic links, and temporary directory management.
class Path(pathlib.Path): ...
class TempDir: ...
def ensuredir(dpath, **kwargs): ...
def symlink(real_path, link_path, **kwargs): ...
def touch(fpath, **kwargs): ...
def delete(fpath, **kwargs): ...Download files with progress tracking, verification, and comprehensive caching systems for computations and data.
def download(url, fpath=None, **kwargs): ...
def grabdata(url, fpath=None, **kwargs): ...
class Cacher: ...
class CacheStamp: ...Progress iteration with ETA, timing utilities, and performance measurement tools.
class ProgIter: ...
class Timer: ...
def timestamp(datetime=None, **kwargs): ...
def timeparse(stamp): ...Comprehensive sequence manipulation including chunking, filtering, sorting, and uniqueness operations.
def chunks(sequence, chunksize): ...
def group_items(items, key): ...
def argmax(sequence, key=None): ...
def argmin(sequence, key=None): ...
def argsort(sequence, key=None, reverse=False): ...
def unique(items, key=None): ...
def flatten(nested_list): ...String manipulation utilities including indentation, formatting, and text processing functions.
def indent(text, prefix=' '): ...
def codeblock(text): ...
def paragraph(text): ...
def hzcat(args, **kwargs): ...Hash arbitrary data and files, plus dynamic module importing and path resolution utilities.
def hash_data(data, hasher='sha512', base='hex', **kwargs): ...
def hash_file(fpath, hasher='sha512', base='hex', **kwargs): ...
def import_module_from_name(name, **kwargs): ...
def import_module_from_path(fpath, **kwargs): ...Function manipulation, memoization, and enhanced concurrency tools.
def memoize(func=None, **kwargs): ...
def identity(arg): ...
class Executor: ...
class JobPool: ...# Sentinel value for unspecified parameters
NoParam: object # Special sentinel used as default for hasher, base, and other parameters
# Enhanced dictionary types
class UDict(dict):
"""Dictionary with set operations and convenience methods"""
class AutoDict(dict):
"""Auto-vivifying dictionary"""
class SetDict(dict):
"""Dictionary with key-wise set operations"""
# Path utilities
class Path(pathlib.Path):
"""Enhanced pathlib.Path with additional methods"""
class TempDir:
"""Context manager for temporary directories"""
class ChDir:
"""Context manager for changing directories"""
# Progress and timing
class ProgIter:
"""Progress iterator with timing and ETA"""
class Timer:
"""Context manager and decorator for timing code"""
# Caching
class Cacher:
"""On-disk caching with dependency tracking"""
class CacheStamp:
"""Lightweight cache stamping for file-producing computations"""
# Command execution
class CmdOutput(dict):
"""Container for command output"""
# Concurrency
class Executor:
"""Enhanced executor interface"""
class JobPool:
"""Job pool for managing concurrent tasks"""
# Data structures
class OrderedSet:
"""Set that preserves insertion order"""
# Stream handling
class TeeStringIO:
"""StringIO that can write to multiple streams"""
class CaptureStdout:
"""Context manager to capture stdout"""
class CaptureStream:
"""Context manager to capture arbitrary stream"""
# Mixins
class NiceRepr:
"""Mixin for nice string representations"""
# Indexable utilities
class IndexableWalker:
"""Walk through indexable nested data structures"""