Technical

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 – http://www.divmod.org/projects/pyflakes

PyLint – http://www.logilab.org/view?rql=Any%20X%20WHERE%20X%20eid%20857

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

PyMetrics – http://sourceforge.net/projects/pymetrics

PyChecker – http://pychecker.sourceforge.net/

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 ."@%@)"
    make
    cw
endfunction
command Pylint :call Pylint()
function! Pylint()
    setlocal makeprg=(echo\ '[%]';\ pylint\ %)
    setlocal efm=%+P[%f],%t:\ %#%l:%m
    silent make
    cwindow
    endfunction

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 http://pysvn.tigris.org/project_downloads.html and fetch last tarball (MacOSX version also available). Note that
pysvn is noted there as “Extension”. I fetched last stable version: http://pysvn.tigris.org/files/documents/1233/34994/pysvn-1.5.0.tar.gz

$ tar xvzf pysvn-1.5.0.tar.gz
 ...
$ cd pysvn-1.5.0/Source
$ python2.4 setup.py configure
...
$ make
...[pray]...
$ mkdir /usr/lib/python2.4/site-packages/pysvn
$ cp pysvn/__init__.py /usr/lib/python2.4/site-packages/pysvn
$ cp pysvn/_pysvn.so /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 http://storage.perlig.de/svnmailer/ and fetch last tarball (stable actual one is: http://storage.perlig.de/svnmailer/svnmailer-1.0.8.tar.gz)

Fallow these steps:

$ tar xvzf svnmailer-1.0.8.tar.gz
   ...

$ cd svnmailer-1.0.8
$ /usr/bin/python2.4 setup.py 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:

#!/bin/sh
REPOS="$1"
REV="$2"
/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)

[general]
# see http://opensource.perlig.de/svnmailer/doc-1.0/#general 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

[mymodule]
# see http://opensource.perlig.de/svnmailer/doc-1.0/#groups 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 = jfroche@jfroche.be

# To address ...
to_addr = peopleinterestinginmymodule@foo.bar

[defaults]
# see http://opensource.perlig.de/svnmailer/doc-1.0/#groups 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 = jeff@jfroche.be foo@skynet.be bar@gmail.com
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

In

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

Hope this helps …