python

matplotlib and multiple y-axis scales

This week I had to create a plot using two different scales in the same graph to show the evolution of two related, but not directly comparable, variables. This operation is described in this FAQ on the matplot lib website.

Parse French dates on a en_US machine

Immagine you work in France, but you are really fond of your good old en_US locales. I'm sure one day you would invariably face the task to use python to play with some french text. I just find out that this can't be easier. You just need to set create and set the correct locales for your python script and voila' !

In this case I need to parse a french date to build an ical file. First, if you haven't already done it for other reasons, you should rebuild your locales and select a freench encoding, for example fr_FR.UTF-8.

easy cudf parsing in python

With the forth run of Misc live, you might wonder how to you can quickly write a parser for a cudf document.

connect django and rfoo

This evening I spent 30 minutes to try out rconsole in the package rfoo . It's a simple environment to inspect and modify the namespace of a running script.

If you are on debian, you need to install two packages :

sudo aptitude install cython python-dev

Then download the source code. If you want to try it out without installing you have to compile it with the --inplace option :

python setup.py build_ext --inplace

Now you're ready to go.

python itertools and groupby

who said that ignorance is a bliss didn't try python :) This is the assignment : you have a list of dictionaries with a field date and you want to group all these dictionaries in a map date -> list of dictionaries with this date.

The first solution that came to my mind was something ugly like :

def group_by_date(qs):
    by_date = {}
    for r in qs :
        l = by_date.get(r['date'],[])
        l.append(r)
        by_date[r['date']] = l
    return by_date

for example :

In [36]: group_by_date([{'date' : 1},{'date' : 2}])
Out[36]: {1: [{'date': 1}], 2:

command line parsing in python

if a good API is meant to let you write code more concisely, I would say the argparse achieves this goal perfectly.

expose you command line application on the web with python and fcgi

So one day you're too lazy to write a fcgi library for your favorite language but you want nonetheless expose an application on the web... Then use python ! There are quite a few frameworks to run fcgi with python, but if you want something easy, I think that flup is for you.

The code below takes care of few aspects for you. First flup span a server talking at port 5555 on localhost. You can configure it to be multi thread is you want to. Then using the cgi module we make sure that the input is clean and ready to use. Finally we run your fantastic application as DOSOMETHING.

upload a file using httplib

I want to share a small snippet of code to upload a file to a remote server as a "multipart/form-data" . The function below gets two arguments. The server url ( ex: http://server.org/upload ) and a filename. First the filename encoded as a "form-data", then we use httplib to POST it to the server. Since httplib wants the host + path in separate stages, we have to parse the url using urlparse.

The receiving server must accept the data and return the location of the newly created resource. There are many snippet on the web, but I felt they were all incomplete or too messy.

add sqlite3 collation with python 2.5 and django

a while ago I wrote about enabling the sqlite3 extension with storm . This is how you do it with the Django ORM. The collation is the same and all details are in the old post. The only tricky part is to establish the connection with cursor = connection.cursor() before calling the function to enable the extension.

dynamic forms with django

Today I started learning how to write web forms in django. My quest was to write a simple search form where I could specify multiple criteria . Django is a very nice and flexible framework written in python and it is also reasonably well documented.

I don't feel writing much today. this is the code :

The tricky part was to understand how to re-display the view and to add a new field. This is easily accomplished in django using the formset class that allows to put display together more then one form. In this case the logic is simple.

Syndicate content