skip to navigation
skip to content

python-util 2.0.0

A collection of useful snippets

# 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
```
 
File Type Py Version Uploaded on Size
python-util-2.0.0.tar.gz (md5) Source 2016-05-12 6KB
python_util-2.0.0-py2.7.egg (md5) Python Egg 2.7 2016-05-12 15KB
python_util-2.0.0-py2.py3-none-any.whl (md5) Python Wheel 3.4 2016-05-12 7KB
python_util-2.0.0-py3.4.egg (md5) Python Egg 3.4 2016-05-12 16KB