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:

For the Python 3+ release (i.e. v3.0.0 or higher) there are no requirements. For the Python 2 compatible version (v2.x.x) the six package is needed.

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 easily retry a block of code with a configurable timeout, you can use the time.timeout_generator:

>>> for i in time.timeout_generator(10):
...     try:
...         # Run your code here
...     except Exception as e:
...         # Handle the exception

Easy formatting of timestamps and calculating the time since:

>>> time.format_time('1')
'0:00:01'
>>> time.format_time(1.234)
'0:00:01'
>>> time.format_time(1)
'0:00:01'
>>> time.format_time(datetime.datetime(2000, 1, 2, 3, 4, 5, 6))
'2000-01-02 03:04:05'
>>> time.format_time(datetime.date(2000, 1, 2))
'2000-01-02'
>>> time.format_time(datetime.timedelta(seconds=3661))
'1:01:01'
>>> time.format_time(None)
'--:--:--'

>>> formatters.timesince(now)
'just now'
>>> formatters.timesince(now - datetime.timedelta(seconds=1))
'1 second ago'
>>> formatters.timesince(now - datetime.timedelta(seconds=2))
'2 seconds ago'
>>> formatters.timesince(now - datetime.timedelta(seconds=60))
'1 minute ago'

Converting your test from camel-case to underscores:

>>> camel_to_underscore('SpamEggsAndBacon')
'spam_eggs_and_bacon'

A convenient decorator to set function attributes using a decorator:

You can use:
>>> @decorators.set_attributes(short_description='Name')
... def upper_case_name(self, obj):
...     return ("%s %s" % (obj.first_name, obj.last_name)).upper()

Instead of:
>>> def upper_case_name(obj):
...     return ("%s %s" % (obj.first_name, obj.last_name)).upper()

>>> upper_case_name.short_description = 'Name'

Or to scale numbers:

>>> converters.remap(500, old_min=0, old_max=1000, new_min=0, new_max=100)
50

# Or with decimals:
>>> remap(decimal.Decimal('250.0'), 0.0, 1000.0, 0.0, 100.0)
Decimal('25.0')

To get the screen/window/terminal size in characters:

>>> terminal.get_terminal_size()
(80, 24)

That method supports IPython and Jupyter as well as regular shells, using blessings and other modules depending on what is available.

To extract a number from nearly every string:

>>> converters.to_int('spam15eggs')
15
>>> converters.to_int('spam')
0
>>> number = converters.to_int('spam', default=1)
1

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.7.1.tar.gz (29.0 kB view details)

Uploaded Source

Built Distribution

python_utils-2.7.1-py2.py3-none-any.whl (14.4 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: python-utils-2.7.1.tar.gz
  • Upload date:
  • Size: 29.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.9

File hashes

Hashes for python-utils-2.7.1.tar.gz
Algorithm Hash digest
SHA256 88595bfa054975534a2f813b6c8deb96b44f3b6fad00a927fd062fe65550fadf
MD5 b9278d8063e17f59507b77e03f7d05b9
BLAKE2b-256 1f4f7d2862c43ed7d1695ea19919a66978b2fd7d2aa10796c88ae1e7e4eede4a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: python_utils-2.7.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 14.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.9

File hashes

Hashes for python_utils-2.7.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9d535eda3fd4c0cd51f459bb9cfddd983a50f5adfacb0995504d12bf0c2981cb
MD5 52ba7fc673cec1b8c1bd2f9acad346de
BLAKE2b-256 0ac2962e0ec22173309d8914733dc78cf8a689fe2ae612938ec64c26d0f29ac6

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