Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

A collection of useful snippets

Project Description
# python-util

[![Version](https://img.shields.io/pypi/v/python-util.svg?style=flat-square)](https://pypi.python.org/pypi/python-util)
[![Downloads](https://img.shields.io/pypi/dm/python-util.svg?style=flat-square)](https://pypi.python.org/pypi/python-util)
[![Build Status](https://img.shields.io/circleci/project/TheKevJames/python-util.svg?style=flat-square)](https://circleci.com/gh/TheKevJames/python-util)
[![Coverage Status](https://img.shields.io/coveralls/TheKevJames/python-util/master.svg?style=flat-square)](https://coveralls.io/github/TheKevJames/python-util?branch=master)
[![Code Quality](https://img.shields.io/codacy/cbdae523e03c45a2b469201f20bfa566.svg?style=flat-square)](https://www.codacy.com/app/KevinJames/python-util)
[![Requirements](https://img.shields.io/requires/github/TheKevJames/python-util.svg?style=flat-square)](https://requires.io/github/TheKevJames/python-util/requirements)

This module defines a set of useful python snippets.

## BiDict

```python
from python_util import Bidict

x = Bidict()
x['apples'] = 'oranges'
print(x['oranges'])
# ['apples']

x['peanuts'] = 'oranges'
print(x['oranges'])
# ['apples', 'peanuts']

print(x['apples'])
# 'oranges'
```

## ColumnPrinter

```python
from python_util import ColumnPrinter

printer = ColumnPrinter()
printer.append(('x', 'y'))
printer.append((3, 1000000000000000))
printer.append((700000, 2))
printer.output()
# x y
# 3 1000000000000000
# 700000 2
```

You can set the amount of seperation between columns with
```python
ColumnPrinter(sep=2)
```

To give each row a prefix, call
```python
ColumnPrinter(prefix='-> ')
```

You can also enable incremental mode to print each line as it is received. This
will dynamically resize columns as it receives new data, making columns expand
over time. Though it will not output a perfectly-formatted table, it can be
useful in long-running cases. Simply call
```python
ColumnPrinter(incremental=True)
```

## Decorators

```python
from python_util import deprecated

@deprecated()
def old_function():
print(42)

old_function()
# demo.py:2: DeprecationWarning: Call to deprecated function old_function.
# 42


from python_util import memoized

@memoized
def do_computation(input):
print('Long calculation here!')
return 'aardvark'

print(do_computation(3))
# Long calculation here!
# 'aardvark'
print(do_computation(3))
# 'aardvark'
print(do_computation(4))
# Long calculation here!
# 'aardvark'


from python_util import synchronized
from threading import Lock, Thread


resource = []
resource_lock = Lock()

@synchronized(resource_lock)
def add(item):
resource.append(item)

@synchronized(resource_lock)
def get():
return resource.pop()

for i in range(10):
Thread(target=add, args=(i, )).start()
if i % 2:
Thread(target=get).start()
# No two or more functions are called at the same time, no guarantee or order


from python_util import timeout

@timeout(3)
def long_function():
import time
time.sleep(10)


long_function()
# after 3 seconds:
# raises python_util.TimeoutError: Function call timed out
```

You can also pass an additional message to `@deprecated` to include it in the
warning string, ie. with
```python
@deprecated('Please use "new_function()"')
def old_function():
pass
```


## Docopt (wrapper)

```python
"""usage:
demo.py [--x=X]
"""

from python_util import docopt

args = docopt(__doc__,
argv=['--x', '3']
validations={'--x': lambda x: int(x) > 0},
transforms={'--x': int})
# args == {'--x': 3}

args = docopt(__doc__,
argv=['--x', '-3']
validations={'--x': lambda x: int(x) > 0},
transforms={'--x': int})
# raises python_util.ValidationException: Could not validate parameter --x. Error was: invalid literal for int() with base 10: 'aardvark'


args = docopt(__doc__,
argv=['--x', 'aardvark']
validations={'--x': lambda x: int(x) > 0},
transforms={'--x': int})
# raises python_util.ValidationException: Could not validate parameter --x. Error was: did not obey requirements
```

## Picklers

```python
from python_util import DictPickler

x = DictPickler('data.dump')
for i in range(100000000000):
x[i] = i % 7
print(x[9])
# 2

x = DictPickler('data.dump')
print(x[9])
# 2


from python_util import ObjectPickler

class Test:
def __init__(self, name):
self.name = name

def print(self):
print(self.name)

x = ObjectPickler('objdata.dump')

x.store = Test('aardvark')
x.store.print()
# 'aardvark'

x = ObjectPickler('objdata.dump')
x.store.print()
# 'aardvark'
```

## Timer

```python
from python_util import Timer

with Timer(message='Processing') as t:
import time
time.sleep(4)
# Processing... done in 4.002s
```
Release History

Release History

This version
History Node

2.0.0

History Node

1.2.1

History Node

1.2.0

History Node

1.1.3

History Node

1.1.2

History Node

1.1.1

History Node

1.1.0

History Node

1.0.1

History Node

1.0.0

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
python_util-2.0.0-py2.7.egg (16.2 kB) Copy SHA256 Checksum SHA256 2.7 Egg May 12, 2016
python_util-2.0.0-py2.py3-none-any.whl (8.0 kB) Copy SHA256 Checksum SHA256 3.4 Wheel May 12, 2016
python_util-2.0.0-py3.4.egg (17.1 kB) Copy SHA256 Checksum SHA256 3.4 Egg May 12, 2016
python-util-2.0.0.tar.gz (6.8 kB) Copy SHA256 Checksum SHA256 Source May 12, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting