Python Utils

raw JSON →
3.9.1 verified Tue May 12 auth: no python install: verified

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.

pip install python-utils
error ModuleNotFoundError: No module named 'utils'
cause The `python-utils` library is installed via `pip install python-utils`, but the user is attempting to import it using the incorrect top-level module name `utils` instead of `python_utils`.
fix
import python_utils
error ImportError: cannot import name 'UniqueList' from 'python_utils'
cause The `UniqueList` class is part of the `python_utils.lists` submodule, not directly available under the main `python_utils` package.
fix
from python_utils.lists import UniqueList
error ImportError: cannot import name 'memoize' from 'python_utils'
cause The `memoize` decorator is located within the `python_utils.decorators` submodule, not directly in the top-level `python_utils` package.
fix
from python_utils.decorators import memoize
error TypeError: 'collections.deque' object does not support item assignment
cause While `SlicableDeque` (which inherits from `collections.deque`) allows retrieving elements using slice notation, it does not implement slice assignment (e.g., `my_deque[1:3] = [new_items]`) because this functionality is not supported by its base class `collections.deque`.
fix
To modify elements in a range, use SlicableDeque's del_slice() method to remove the old elements, and then use insert() or extend() to add new ones at the desired position.
gotcha When migrating from `python-utils` v2.x.x (Python 2 compatible) to v3.x.x (Python 3+), note that v3.x.x explicitly requires Python 3.9 or higher and has no other external dependencies. Compatibility with Python 2 requires the `six` package and older library versions.
fix Ensure your project is running on Python 3.9+ for `python-utils` v3.x.x. If using older Python 3 versions, you might need older `python-utils` releases and potentially address `typing-extensions` compatibility if encountering type-hinting issues.
gotcha Avoid incorrect top-level imports like `import utils`. The correct package name is `python_utils`, and you should import specific classes, functions, or submodules from `python_utils` (e.g., `from python_utils import UniqueList` or `from python_utils.converters import to_int`).
fix Always use `import python_utils` or `from python_utils import <symbol>` / `from python_utils.<submodule> import <symbol>`.
gotcha In `v3.8.2`, a fix was introduced for `fromtimestamp` overflow issues on Windows, where it previously raised a `ValueError` but can now also raise an `OSError`. This might affect code handling timestamp conversions.
fix Upgrade to `v3.8.2` or higher and ensure your code handles both `ValueError` and `OSError` when working with `datetime.fromtimestamp` and potentially large or invalid timestamp values, especially on Windows.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.11s 18.3M
3.10 alpine (musl) - - 0.12s 18.3M
3.10 slim (glibc) wheel 1.6s 0.08s 19M
3.10 slim (glibc) - - 0.08s 19M
3.11 alpine (musl) wheel - 0.20s 20.3M
3.11 alpine (musl) - - 0.22s 20.3M
3.11 slim (glibc) wheel 1.7s 0.18s 21M
3.11 slim (glibc) - - 0.16s 21M
3.12 alpine (musl) wheel - 0.40s 12.1M
3.12 alpine (musl) - - 0.42s 12.1M
3.12 slim (glibc) wheel 1.6s 0.40s 13M
3.12 slim (glibc) - - 0.36s 13M
3.13 alpine (musl) wheel - 0.40s 11.8M
3.13 alpine (musl) - - 0.42s 11.7M
3.13 slim (glibc) wheel 1.6s 0.37s 12M
3.13 slim (glibc) - - 0.37s 12M
3.9 alpine (musl) wheel - 0.11s 17.8M
3.9 alpine (musl) - - 0.11s 17.8M
3.9 slim (glibc) wheel 1.9s 0.09s 18M
3.9 slim (glibc) - - 0.09s 18M

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}")