Python Utils

3.9.1 · active · verified Sun Mar 29

Python Utils is a module with convenient utilities not included with the standard Python install. It offers a collection of functions and classes for common patterns, including decorators, converters, formatters, and data structures like `UniqueList` and `SlicableDeque`. The library is actively maintained with frequent releases, currently at version 3.9.1.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `to_int` converter to safely extract integers from strings and the `listify` decorator to convert generator output into a list.

from python_utils.converters import to_int
from python_utils.decorators import listify

# Convert a string to an integer, with a default if conversion fails
num = to_int('spam15eggs')
print(f"Converted 'spam15eggs' to {num}")

default_num = to_int('spam', default=1)
print(f"Converted 'spam' with default to {default_num}")

# Use a decorator to automatically convert a generator to a list
@listify()
def generate_numbers():
    yield 1
    yield 2
    yield 3

my_list = generate_numbers()
print(f"Generator output as a list: {my_list}")

view raw JSON →