Saturday, January 12, 2013

Python libraries I want to use (AKA: my Python Wishlist)

Are you one of the guys that sometimes open the Python Package Index homepage simply because like to see what's new?
I am.

Often I see some new Python packages release about a library I never heard before and I say "Cool! I will use this library for sure one of those days". When this happen you add this library to your delicious bookmark... and you probably forget about it.

Unluckily my every-day-job is 95% focused on Plone where is not so common to introduce new Python packages, but recently I needed to develop something not Plone-related for a... let say "personal need", and I was able to use three of those libraries in the same project!

Let review them now.

requests

The requests library is not new or unknown, and a lot of other Python libraries use it. Why? Because it's simply amazing.

The first time I tried to connect to remote services using Python for something that isn't a simply operation like read from a remote URL, I found that making a POST request is not so simple (...curl is not simple at all).
Then I found the requests library and managing HTTP requests become very easy... and pythonic!
You need to...
  • perform a form POST request?
  • send a file?
  • perform a request with authentication?
  • need to send also cookies?
  • ...
 This is not a problem!

pyquery

If you ever used the popular JavaScript jQuery library to manipulate the DOM and after that you needed to check an HTML source with other programming languages, you missed all of the jQuery selector syntax.

pyquery is nothing more that this: you will be able to use a jQuery-like object in your Python environment, and use a lot of the CSS 3 selectors, or other pseudo selector you loved.

This is so quick and simple to use that I completely changed the way I perform Zope functional testing in Plone. Ok, is not so common to use doctests nowadays because the are often complex to be maintained, but when it happen and you need to check something in your generated HTML, using pyquery is a lot simpler.

HTTPretty

Tests in Python are easy and wheel documented, however is not so simple to create tests when you need to test a feature thats talk with remote services using the standard Python libraries for open remote URLs.
An example: can be easy to write tests when you are using the requests library introduced above?

The Python httpretty library solve exactly this need; you can read this definition in the project's home page: "HTTPretty is a HTTP client mock library for Python".
It mock standards HTTP Python call, giving you a way to simulate what the call must return you back and perform testing totally online.

Another good news? Documentation explicitly use requests library for giving examples!

Other libraries (not yet tested)

As say above: I was able to use all those three libraries together in a single project (Allanon), but my delicious list contains other packages that I've not explored yet:
  • say: a "super-simple templated printing and formatting" library.
  • premailer: "Turns CSS blocks into style attributes".
    This is something that Plone Newsletters products must evaluate!
  • pyzmail: "pyzmail is a high level mail library for Python".
  • nikola: "a Static Site and Blog Generator".
    Sometimes I think about drop keul.it site and redo it (and this blog) with this tool.
  • ...