Skip to content

pyutilkit: python's missing batteries

build lint tests license codecov readthedocs pypi downloads build automation: yam Lint: ruff

Python has long maintained the philosophy of "batteries included", providing users with a rich standard library that avoids the need for third-party tools for most tasks. Some packages are so common they have achieved similar status to the standard library. Yet, certain utilities are reimplemented across countless projects. This lightweight library, with minimal dependencies, aims to eliminate that repetition.

Quick Start

Install pyutilkit:

pip install pyutilkit

Or with uv (recommended for speed):

uv pip install pyutilkit

Basic usage example:

from pyutilkit.date_utils import now
from pyutilkit.timing import Stopwatch
from pyutilkit.term import SGRString, SGRCodes

# Get current time in any timezone
from zoneinfo import ZoneInfo
tokyo_time = now(ZoneInfo("Asia/Tokyo"))
print(f"Current time in Tokyo: {tokyo_time}")

# Measure execution time
stopwatch = Stopwatch()
with stopwatch:
    # Your code here
    result = sum(range(1000000))
print(f"Computation took: {stopwatch.elapsed}")

# Colorful terminal output
success = SGRString("โœ“ Task completed", params=[SGRCodes.GREEN, SGRCodes.BOLD])
success.print()

Key Features

  • ๐Ÿ• Timezone Utilities: Robust datetime handling with cross-platform timezone support, ISO parsing (including Zulu timezone), and seamless timezone conversion
  • โฑ๏ธ High-Precision Timing: Nanosecond-precision timing with human-readable formatting, lap tracking, and stopwatch functionality
  • ๐ŸŽจ Terminal Formatting: ANSI color codes with smart TTY detection, automatic style stripping for piped output, and convenient header formatting
  • ๐Ÿ›ก๏ธ Error Handling: Elegant exception handling decorator that logs errors and returns defaults instead of raising exceptions
  • ๐Ÿ“ File Utilities: Efficient SHA-256 file hashing with buffered reading for large files
  • ๐Ÿš€ Subprocess Enhancement: Run shell commands with real-time output streaming, automatic timing, and structured results
  • ๐Ÿ”ง Design Patterns: Thread-safe Singleton metaclass implementation
  • โœจ Zero Dependencies: Pure Python using only standard library (except optional tzdata on Windows)
  • ๐Ÿงช Fully Tested: 100% test coverage with comprehensive type annotations

Documentation

Requirements

  • Python 3.10 or higher
  • No external dependencies (tzdata is automatically installed on Windows)