Useful Python Utils

Python Utils is a collection of small Python functions and classes which make common patterns shorter and easier. It is by no means a complete collection but it has served me quite a bit in the past and I will keep extending it.

One of the libraries using Python Utils is Django Utils.

Documentation is available at:

Requirements for installing:

  • six any recent version


The package can be installed through pip (this is the recommended method):

pip install python-utils

Or if pip is not available, easy_install should work as well:

easy_install python-utils

Or download the latest release from Pypi ( or Github.

Note that the releases on Pypi are signed with my GPG key ( and can be checked using GPG:

gpg –verify python-utils-<version>.tar.gz.asc python-utils-<version>.tar.gz


This module makes it easy to execute common tasks in Python scripts such as converting text to numbers and making sure a string is in unicode or bytes format.


To extract a number from nearly every string:

from python_utils import converters

number = converters.to_int('spam15eggs')
assert number == 15

number = converters.to_int('spam')
assert number == 0

number = converters.to_int('spam', default=1)
assert number == 1

number = converters.to_float('spam1.234')

To do a global import programmatically you can use the import_global function. This effectively emulates a from … import *

from python_utils.import_ import import_global

# The following is  the equivalent of `from some_module import *`

Or add a correclty named logger to your classes which can be easily accessed:

class MyClass(Logged):
    def __init__(self):

my_class = MyClass()

# Accessing the logging method:

# With formatting:
my_class.error('The logger supports %(formatting)s',
               formatting='named parameters')

# Or to access the actual log function (overwriting the log formatting can
# be done n the log method)
import logging
my_class.log(logging.ERROR, 'log')