Adam Johnson

Home | Blog | Training | Projects | Colophon | Contact

Five Neat Little Python Libraries


Here are some great little Python libraries that have made my life (well, at least the coding part) a little bit nicer and easier. They mostly add neat syntax and a few things that you always wanted to do, but never knew.

1. Unipath

EDIT: I missed out on pathlib, which joined the standard library in Python 3.4, and provides the same object-oriented niceness for paths as Unipath. You should use that - there’s a backport for Python 2.x as well. Unipath is just a has-been contender now I guess.


It’s an “object-oriented alternative to os/os.path/shutil”.


From the README:

>>> p = Path("/usr/lib/python2.5/")
>>> p.ext

Basically, if you’re fiddling around a lot with os.path and other file-manipulation functions, you’re missing out on this much nicer, arguably more pythonic, way of doing things.


2. sh


Similar to Unipath, but for replacing subprocess. Call any program as if it were a function, and get return values in a sane manner.


Using a bit of python magic, the module lets you import any program as if it were a function:

>>> from sh import ls
>>> ls("-l")
total 16437
drwxrwxr-x+ 53 root  admin     1802  5 Jul 15:54 Applications
drwxr-xr-x+ 65 root  wheel     2210 25 Apr 09:24 Library
drwxr-xr-x@  2 root  wheel       68 25 Aug  2013 Network

Argument passing can also be done with keyword-args:

>>> from sh import curl
>>> curl("", silent=True).split()[:2]
[u'<!DOCTYPE', u'html>']

From that you might surmise that the return of any shell command is a unicode object - but it’s not! It’s a sh.RunningCommand that wraps around the result to expose the stdout by default, but lets you get at other attributes easily too…

>>> from sh import curl
>>> result = curl("", silent=True)
>>> type(result)
>>> result.stderr.splitlines()[-5:]
['< Location:',
 '< Expires: Wed, 16 Jul 2014 21:24:58 GMT',
 '< Cache-Control: max-age=1',
 '< ',
 '* Connection #0 to host left intact']

I’m sure you can see how much easier this makes subprocess interaction. Maybe you’ll find yourself forgoing bash for Python soon :)


3. schedule


A mini-library with easy syntax for scheduling recurring jobs and running a job loop that executes them. For when cron isn’t enough (environment issues, logging, etc.), but anything much bigger is overkill.


From the README:

import schedule
import time

def job():
    print("I'm working...")


while True:

It’s inspired by a Ruby module, as you might be able to tell from the DSL syntax (don’t let anyone ever tell you again that Python can’t do DSLs!). If you do use it to replace cron, use something like supervisord to keep your schedule program running in case of failure.


4. Delorean


“Time Travel Made Easy.” Simplifies a lot of datetime interactions that become painful when you’re doing a lot of them.


>>> from delorean import Delorean
>>> Delorean()  # defaults to now, in UTC
Delorean(datetime=2014-07-16 22:04:51.206981+00:00, timezone=UTC)
>>> Delorean().date, 7, 16)
>>> Delorean().next_tuesday().date, 7, 22)
>>> import delorean
>>> from delorean import stops
>>> for stop in stops(freq=delorean.HOURLY, count=3):
...    print stop.datetime
2014-07-16 22:10:00+00:00
2014-07-16 23:10:00+00:00
2014-07-17 00:10:00+00:00

Delorean wraps a couple of useful date and time libraries - pytz and dateutil - to provide this functionality. And it has an awesome name.


5. schema


“Schema validation just got Pythonic.”


>>> from schema import Schema, And, Use, Optional
>>> person_schema = Schema({
...     'name': str,
...     'age':  And(Use(int),
...                 lambda n: 18 <= n <= 99),
...     Optional('sex'): And(str,
...                          Use(str.lower),
...                          lambda s: s in ('male', 'female'))
... })
>>> sue = {'name': 'Sue', 'age': '28', 'sex': 'FEMALE'}
>>> sue_validated = person_schema.validate(sue)
>>> sue_validated['age']
>>> jim = {'name': 'jim', 'age': 22, 'sex': 'yes please'}
>>> person_schema.validate(jim)
Traceback (most recent call last):
 yada yada yada
SchemaError: <lambda>('yes please') should evaluate to True

Brilliant for validating API responses, user input, JSON stuffed in a database, or whatever. Also great for unittests, where validating all the values output by the code might be impossible, but you can at least check for their type and existence.


6. Bonus!! django-unchained


We’ve all heard of django for web app development; now there’s the awesome Django Unchained library to take it to the next level. You’ll have to pip install django-unchained to find out what it’s all about.

…and that’s the end of the presentation for today. Thanks for reading!

Are your Django project's tests slow? Read Speed Up Your Django Tests now!

Subscribe via RSS, Twitter, or email:

One summary email a week, no spam, I pinky promise.

Tags: python