Thursday, September 28, 2006

Python 2.5, Zope 3.3 releases

This month my favourite programming language and web framework relased newer
versions. I reported Python release here: Oh! many people dugg it.
But I was afraid of Zope 3 release there :) Though, I have written an article

Using Personal Wiki

Recently I realised that using a personal wiki for documentation is better than
a public one. (Why ?). Well, the main reason is that you get more freedom and
courage. I will be more cautious when adding content to public wiki.
And my favourite wiki is ZWiki.

Tuesday, September 19, 2006

A work in progress chapter from Zope Guide

This is a work in progress chapter from Zope Guide:
Please contribute! especially grammer mistakes :)

Unit testing


In this chapter, you will learn more about unit testing. Zope 3 use `doctest`
based unit testing heavily. Zope 3 preferes test driven development (TDD).

To explain the idea, consider a use case. A module is required with a function
which returns 'Good morning, name!'. The name will be given as argument.
Before writing the real code write the unit test for this. Infact you will be
writing the real code and it's test cases almost in parallel. So just create a
file named `` with just the function definition::

def goodmorning(name):
"This returns a good morning message"

See you are not yet wrote the logic yet. But this is necessary to run tests
successfully with failures!. Ok, now create a file named `example1.txt`
with test cases, use ReStructuredText format::

These are test for `example1` module.

First import the module::

>>> import example1

Now call the function `goodmorning` with out any argument::

>>> example1.goodmorning()
Traceback (most recent call last):
TypeError: goodmorning() takes exactly 1 argument (0 given)

Now call the function `goodmorning` with one argument::

>>> example1.goodmorning('Jack')
'Good morning, Jack!'

See the examples are written like executed from prompt. You can use your
python prompt and copy paste from there. Now create another file
`` with this content::

import unittest
import doctest

def test_suite():
return unittest.TestSuite((

if __name__ == '__main__':

This is just a boilerplate code for running the test. Now run the test using
`python2.4` command.
You will get output with following text::

File "example1.txt", line 16, in example1.txt
Failed example:
'Good morning, Jack!'
Got nothing

Now one test failed, so implement the function now::

def goodmorning(name):
"This returns a good morning message"
return "Good morning, %s!" % name

Now run the test again, it will run without failures.

Now start thinking about other functionalities required for the module. Before
start coding write about it in text file. Decide API, write test, write code,
than continue this cycle untill you finish your requirements.

Monday, September 18, 2006

Python talk on SFD (2)

Promode has written a good blog entry here: . As he said, my intention was to
motivate the engineering students to learn Python. I got around 90 minutes.
Promode's talk was also included lots of Python, because Python is used in
Phoenix ( It was a really interesting talk,
event hough electronics was not my area of interest, I really enjoyed it.

I welcome all those who attended this talk to Kerala Python User Group, more
details are here:

Hmm.. I mentioned Zope 3 too :)

Saturday, September 16, 2006

Python talk on SFD

Today I had one Talk on Python at Vidya academy:
I am writing this from their library. Now helping them to install Koha.
This is my first Python talk, will write details tomorrow.

Friday, September 15, 2006

Sqlite usage

This is a simple tutorial(?) to use sqlite. I used pysqlite 1.0.1 in Python 2.3,
which is the default in Debian 3.1 (Sarge).

The latest pysqlite version is 2.3.2, Visit this site for more info:

""" sqlite module usage with examples

First import sqlite::

>>> import sqlite

Create a temporary directory::

>>> import tempfile
>>> tempdir = tempfile.mkdtemp()

Now create a db file::

>>> import os
>>> dbfile = os.path.join(tempdir, 'testdb.sdb')

Now connect to db and create a cursor::

>>> conn = sqlite.connect(dbfile)
>>> cr = conn.cursor()

Create a table::

>>> cr.execute("CREATE TABLE first_table (a INT)")

Insert a value and get::

>>> cr.execute("INSERT INTO first_table (a) values (1)")
>>> cr.execute("SELECT a FROM first_table")
>>> cr.fetchall()

Test commit and rollback::

>>> conn.commit()
>>> cr.execute("DELETE FROM first_table")
>>> cr.execute("SELECT a FROM first_table")
>>> cr.fetchall()
>>> #now rollback and query again
>>> conn.rollback()
>>> cr.execute("SELECT a FROM first_table")
>>> cr.fetchall()

Insert more values as parameters::

>>> cr.execute("CREATE TABLE second_table (a INT, b VARCHAR(20))")
>>> cr.execute("INSERT INTO second_table (a, b) VALUES (%s, %s)",
... (1, 'hi1'))
>>> cr.execute("SELECT a, b FROM second_table")
>>> cr.fetchone()
(1, 'hi1')

Insert more values using executemany::

>>> cr.executemany("INSERT INTO second_table (a, b) VALUES (%s, %s)",
... [(2, 'hi2'), (3, 'hi3')])
>>> cr.execute("SELECT a, b FROM second_table")
>>> rst = cr.fetchall()
>>> (2, 'hi2') in rst
>>> (3, 'hi3') in rst

Hmm.. there is no rewind::

>>> cr.rewind
Traceback (most recent call last):
AttributeError: rewind

Views are supported::

>>> cr.execute("CREATE VIEW first_second_view \
AS SELECT a.a AS a1, b.a AS a2, b.b \
FROM first_table AS a LEFT JOIN second_table AS b \
ON a.a = b.a")
>>> cr.execute("SELECT a1, a2, b FROM first_second_view")
>>> cr.fetchone()
(1, 1, 'hi1')

Cleanup tempdir::

>>> import shutil
>>> shutil.rmtree(tempdir)


def _test():
import doctest
return doctest.testmod()

if __name__ == '__main__':

Monday, September 4, 2006


Yet another Onam [1] is reached here. Tomorrow is Thiruvonam.
Happy Onam to all my journal vistors!


Friday, September 1, 2006

A nice article by Joe Gregorio

I came across this article today:
"Python isn't just Java without the compile"