Skip to main content

Python Utils is a module with some convenient utilities not included with the standard Python install

Project description

https://travis-ci.org/WoLpH/python-utils.svg?branch=master https://coveralls.io/repos/WoLpH/python-utils/badge.svg?branch=master

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: https://python-utils.readthedocs.org/en/latest/

Requirements for installing:

  • six any recent version

Installation:

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 (https://pypi-hypernode.com/pypi/python-utils) or Github.

Note that the releases on Pypi are signed with my GPG key (https://pgp.mit.edu/pks/lookup?op=vindex&search=0xE81444E9CE1F695D) and can be checked using GPG:

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

Quickstart

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.

Examples

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 *`
import_global('some_module')

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

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

my_class = MyClass()

# Accessing the logging method:
my_class.error('error')

# 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')

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python-utils-2.5.6.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

python_utils-2.5.6-py2.py3-none-any.whl (12.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file python-utils-2.5.6.tar.gz.

File metadata

  • Download URL: python-utils-2.5.6.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.8

File hashes

Hashes for python-utils-2.5.6.tar.gz
Algorithm Hash digest
SHA256 352d5b1febeebf9b3cdb9f3c87a3b26ef22d3c9e274a8ec1e7048ecd2fac4349
MD5 c89cf540de787b45ce33b8ee10b32a79
BLAKE2b-256 03d83ad4dd157e5a36184df0280857bcaea60b13cb6e945e27ac3d8589f8c2a2

See more details on using hashes here.

File details

Details for the file python_utils-2.5.6-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for python_utils-2.5.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 18fbc1a1df9a9061e3059a48ebe5c8a66b654d688b0e3ecca8b339a7f168f208
MD5 4c377713478d827e2262dcbe273f6a4d
BLAKE2b-256 6247974eb168d5012bc8841d4b5be10e345f3e706e25fa1661b68f193e16ca67

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page