Python Utils

Live
Library Developer Tools Utilities

A comprehensive utility library that eliminates boilerplate from everyday Python code.

The Problem

Every project has the same utilities scattered around: that function to slugify strings, the one that safely gets nested dict values, the email validator you copied from Stack Overflow. You end up with a utils.py file that grows organically and becomes unmaintainable.

This library is my answer: a well-organized, thoroughly tested collection of utilities I actually use, packaged for reuse.

Design Principles

Static Methods Only - No classes to instantiate. Just import and call:

from utils import String
String.slugify(text='Hello World!')  # 'hello-world'

Keyword-Only Arguments - Every method uses keyword-only args for clarity. No guessing what positional arguments mean:

# Clear what each argument does
Dict.deep_get(obj=config, path='database.host', default='localhost')

Zero Core Dependencies - The base utilities have no external dependencies. Optional features (HTTP, Pydantic integration) require their respective packages.

Utility Categories

String Utilities (22 methods)

  • Case Conversion: snake_case, camelCase, PascalCase, kebab-case
  • Slugify: URL-safe string conversion
  • Truncate: Smart truncation with ellipsis
  • Hashing: MD5, SHA256, SHA512
  • Extraction: Extract emails, URLs, numbers from text
  • Padding: Left, right, center padding

Integer Utilities (15 methods)

  • Properties: is_even, is_odd, is_prime, is_perfect_square
  • Math: factorial, GCD, LCM, power_of_two
  • Conversion: to_roman, from_roman, to_words
  • Formatting: bytes_to_human (1048576 -> '1 MB')

Dict Utilities (18 methods)

  • Deep Access: deep_get, deep_set with dot notation
  • Merge: deep_merge for nested dicts
  • Transform: map_keys, map_values, filter
  • Case Convert: snake_case_keys, camel_case_keys (recursive)
  • Flatten/Unflatten: Nested dict <-> flat with dot keys

Iterable Utilities (22 methods)

  • Chunking: Split lists into fixed-size batches
  • Grouping: Group by key function
  • Aggregation: sum_by, avg_by, count_by
  • Finding: first, last, find_all with predicates
  • Filtering: unique, compact (remove None/empty)

Datetime Utilities (28 methods)

  • Parsing: ISO, RFC3339, custom formats
  • Arithmetic: add_days, add_months, add_years
  • Boundaries: start_of_day, end_of_month, start_of_week
  • Human: 'human_time' for relative dates ('3 days ago')
  • Comparison: is_weekend, is_business_day, days_between

Validator Utilities (14 methods)

  • Format: is_email, is_url, is_uuid, is_phone
  • Financial: is_credit_card (Luhn algorithm)
  • Geographic: is_latitude, is_longitude, is_timezone
  • Content: is_empty, is_numeric, is_hex_color

Path Utilities (12 methods)

  • Read/Write: text, lines, JSON with encoding handling
  • Operations: copy, move, delete with safety checks
  • Info: size, modified_time, extension, exists

Logger Utilities

Structured JSON logging with context:

from utils import Logger
log = Logger.create(name='myapp')
with log.context(user_id='123', request_id='abc'):
    log.info(message='Processing request')
# Output: {"timestamp": "...", "level": "INFO",
#  "message": "Processing request", "user_id": "123"}

Testing

1,114 tests covering all utilities, edge cases, and error conditions. Tests run in under 5 seconds.

Installation

pip install bp-utils  # Core utilities
pip install bp-utils[http]  # Adds Session utilities
pip install bp-utils[pydantic]  # Adds Pydantic validators
pip install bp-utils[all]  # Everything

Tech Stack

Python 3.11+ Pydantic 2.0 requests pytest Hatchling

Send a Message