Posts tagged Python

Percent in python’s string formatting

In python, we don’t need to escape the char ‘%’ in a string, except when we want to format the specific string. Examples:

>>> 'Here is a percent: %'
'Here is a percent: %'

>>> 'Here is two percent %%'
'Here is two percent %%'

>>> 'Here is a percent %% %s' % 'in formatted string'
'Here is a percent % in formatted string'

>>> 'We have to double the percent % %s!' % 'in formatted string'
Traceback (most recent call last):
  File "", line 1, in 
TypeError: not all arguments converted during string formatting

>>> 'If we do not specify a format, we must not double the percent: %(foo)s %(bar)s!'
'If we do not specify a format, we must not double the percent: %(foo)s %(bar)s!'

>>> 'If we want to format specifically, we must double the percent: %(foo)s %%(bar)s!' % {'foo': 'test'}
'If we want to format specifically, we must double the percent: test %(bar)s!'

>>> 'And of course this will crash: %(foo)s %(bar)s!' % {'foo': 'test'}
Traceback (most recent call last):
  File "", line 1, in 
KeyError: 'bar'

OpenOffice with buildouts

Easy install the pyuno library with buildout

This recipe was started by Infrae, I have added the pyuno egg creation within the recipe. It is so nice to be able to share code so easily !

Download OpenOffice in a buildout might sound odd but

  • You got used (and I think you are right) to isolate your python environment for each projects (e.g using buildout & virtualenv)
  • You want to control OpenOffice components with your favourite python using the official OpenOffice library: pyuno
  • If you already installed OpenOffice, you might not see how to link your favourite python with the pyuno library provided by the OpenOffice package of your favourite distribution

The python uno library (aka pyuno) is only delivered with OpenOffice. The library provides access to all OpenOffice UNO api which means:

  • read and write doc, odt, rdf, xls and the most common file extension known in the different well known office suites
  • generate nice pdf
  • no xml parsing/transforms when you want to read odt & co
  • fill in templates with content (e.g. pod)
  • drawing shapes
  • … (you might want to have a look at the OO developper guide for more informations even if there is lot’s of java examples )

The only major disadvantage is that it requires to connect to an openoffice process. This can be a problem if your sysadmin don’t want to install a real X server in a production environment but this disadvantage is quickly fixed if you install and use Xvfb

This recipe can

  • download OpenOffice from any url (default to OpenOffice 2.3) and extract it in your buildout
  • replace the default python interpreter delivered with OO with the one you are using in your buildout
  • create an egg with pyuno and link it inside your buildout

More info about installation and usage of this recipe on pypi

Linux is only supported by now (and I don’t want to be sorry for not having a mac), if you want to fix this the code is here ;)

Zope 3 dependencies in Zope 2 buildout

Describe a buildout recipe to avoid fetching zope 3 libraries when installing eggs with zope 3 depedencies in a zope 2 buildout

I got tired removing zope.interface, zope.component, zope.deferredimport, zope.event, … of my eggs folder inside my buildout when installing package such as z3c.sqlalchemy in my Zope2 / Plone buildouts …

After the discussion on Zope3-users list and thanks to Jim lights, I wrote a really simple recipe which just create egg links to zope libraries in your develop-eggs so that setuptools can see that the dependencies are
already satisfied.

So if you list your zope library :

$ ls yourbuildout/parts/yourzope2/lib/python/zope

app               configuration    documenttemplate  exceptions  i18n   pagetemplate  schema    structuredtext  testbrowser  viewlet
cachedescriptors  contentprovider  dottedname        formlib     i18nmessageid  interface    proxy         security  tal             testing
component         deprecation      event             hookable    index          modulealias  publisher     server    tales           thread

You will get

$ ls yourbuildout/develop-eggs      zope.dottedname.egg-info     zope.interface.egg-info     zope.structuredtext.egg-info            zope.event.egg-info          zope.modulealias.egg-info   zope.tal.egg-info         zope.cachedescriptors.egg-info  zope.exceptions.egg-info     zope.pagetemplate.egg-info  zope.tales.egg-info          zope.component.egg-info         zope.formlib.egg-info        zope.proxy.egg-info         zope.testbrowser.egg-info     zope.configuration.egg-info     zope.hookable.egg-info       zope.publisher.egg-info     zope.testing.egg-info  zope.contentprovider.egg-info   zope.i18n.egg-info           zope.schema.egg-info        zope.thread.egg-info     zope.deprecation.egg-info       zope.i18nmessageid.egg-info      zope.viewlet.egg-info        zope.documenttemplate.egg-info  zope.index.egg-info          zope.server.egg-info

Where each of these file will be seen for setuptools as an egg:

$ cat yourbuildout/develop-eggs/

Metadata-Version: 1.0
Version: 0.0

Sure this might look like an ugly hook but I can’t wait for zope 2 eggification …

Buildout and Virtualenv

How and why use buildout with virtualenv

Buildout is this wonderful tool which helps you to automate setup and configuration of your applications.

Virtualenv is a tool which will help you to isolate your python environment

A few days ago I got stuck during a long time because I didn’t see that one library I installed in my global site-packages of my favourite python 2.4 (on my ubuntu: /usr/lib/python2.4/site-packages/) was a lower version of a library I was using in my buildout. Package was sqlalchemy 0.4 in my global sites-package and my buildout based application was using sqlalchemy 0.3.8 … Here is a simple solution to avoid this kind of things.

Idea is to start buildout with a python free of any external library. Virtualenv is the answer.

Install Virtualenv


$ easy_install virtualenv

you will have then a file that you can run: /usr/bin/virtualenv

Let’s say you have a buildout configuration go into it:

$ cd myApp.buildout
$ ls -l

Now you just have to create the python environment without any access to the global site-packages with virtualenv:

$ virtualenv --no-site-packages .

You will have then your new python in the bin folder :

$ ls -l bin

You can now run the buildout configuration with your new python:

$ ./bin/python2.4
$ ./bin/buildout

This will create you a nice and totally isolated environment …

Writing nicer Python code

Starting from the basic facts:

  • you can’t always code in pair
  • you don’t want to compile & tests to check that is fine in the last few line you have just written
  • you want to write nice python code
  • you want to improve the way you write python

It exists a few very interesting softwares that can help you.

Here is a list of the one I am using really often:

PyFlakes –

PyLint –

These two are nice also but not as good as the previous one:

PyMetrics –

PyChecker –

These tool are directly linked to my favourite editor VI:

command Pyflakes :call Pyflakes()
function! Pyflakes()
    let tmpfile = tempname()
    execute "w" tmpfile
    execute "set makeprg=(pyflakes\\ " . tmpfile . "\\\\\\|sed\\ s@" . tmpfile ."@%@)"
command Pylint :call Pylint()
function! Pylint()
    setlocal makeprg=(echo\ '[%]';\ pylint\ %)
    setlocal efm=%+P[%f],%t:\ %#%l:%m
    silent make

And better each time you save your python file in vim , I check for wrong imports with Pyflakes with:

autocmd BufWrite *.{py} :call Pyflakes()

Watch my new commit…

Get notification as soon as somebody commit is really important! A quick note to set it up with emails…

We will make this in 4 steps.

Step 1: Installing pysvn

Check that you don’t have it yet:

$ python2.4
Python 2.4.3 (#2, Oct  6 2006, 07:52:30)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import svn

If you get something else (e.g ImportError: No module named svn) you will need to do this step:

You will need the python library to access subversion: pysvn. Note that header of the libsvn are required [should come with your subversion install]!
Go to and fetch last tarball (MacOSX version also available). Note that
pysvn is noted there as “Extension”. I fetched last stable version:

$ tar xvzf pysvn-1.5.0.tar.gz
$ cd pysvn-1.5.0/Source
$ python2.4 configure
$ make
$ mkdir /usr/lib/python2.4/site-packages/pysvn
$ cp pysvn/ /usr/lib/python2.4/site-packages/pysvn
$ cp pysvn/ /usr/lib/python2.4/site-packages/pysvn

Now try again and you should get something like:

$ python2.4
Python 2.4.3 (#2, Oct  6 2006, 07:52:30)
[GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import svn

Step 2: Installing SVNMailer

Go to and fetch last tarball (stable actual one is:

Fallow these steps:

$ tar xvzf svnmailer-1.0.8.tar.gz

$ cd svnmailer-1.0.8
$ /usr/bin/python2.4 install

You have now a brand new svnmailer installed. Check it with:

$ ls -l /usr/bin/svnmailer
-rwxr-xr-x 1 root root 2192 2006-10-24 17:00 /usr/bin/svn-mailer

Step 3: Configure your repository to use svnmailer

We created a svn repository:

$ svnadmin create /var/svn/repos1

So we have a repository on our local filesystem in /var/svn/repos1

Go into the hooks directory:

$ cd /var/svn/repos1/hooks

Add/edit post-commit file.
And add the fallowing lines:

/usr/bin/svn-mailer commit "$REPOS" "$REV" /etc/svn-mailer.conf

Change execute permission on post-commit

chmod 755 post-commit

It’s time to configure SVNMailer

Step 4: Configure SVNMailer

Edit/Add the file /etc/svn-mailer.conf
And add the fallowing lines [you might edit few things...]. Imagine that I have mymodule in my repository (so that i can do svn co file:///var/svn/repos1/mymodule)

# see for details.

# the diff command to be used ... just copy it...
diff = /usr/bin/diff -u -L %(label_from)s -L %(label_to)s %(from)s %(to)s
# the sendmail location
mail_command = /usr/sbin/sendmail

# see for details
# this part of the config apply only for commits under mymodule
for_paths = mymodule/.*

# the subject of the email
commit_subject_prefix = [MYMODULE]

# From address in the mail
from_addr =

# To address ...
to_addr =

# see for details
# this part of the config apply for all the other module

# Default From address template
from_addr = %(author)s@localhost.localdomain

# the subject of the email
commit_subject_prefix = [SVN]
to_addr =
generate_diffs = add copy modify
suppress_deletes = yes

Here it is… Try to commit and you should get email…

If you want to traceback error, go to edit /var/svn/repos1/hooks/post-commit and for example log to a file by changing the line

/usr/bin/svn-mailer commit "$REPOS" "$REV" /etc/svn-mailer.conf


/usr/bin/svn-mailer commit "$REPOS" "$REV" /etc/svn-mailer.conf 2> 1> /tmp/svnmailer.log

Hope this helps …