Python DevCenter
oreilly.comSafari Books Online.Conferences.


What's New in Python 2.5
Pages: 1, 2, 3, 4

New or Improved Libraries

The core modules have seen many changes. The full details are available in New, Improved, and Removed Modules. Here are some highlights.

Calling Arbitrary Shared Libraries and DLLs

Python 2.5 now has a nice package called ctypes. (This package has been around for awhile, but now has a new version and is included as part of Python.) ctypes lets you load external shared libraries and then call functions that are inside the libraries. Here's an example on Windows where I'm calling a function inside the system library USER32:

>>> import ctypes
>>> libc = ctypes.CDLL('c:/windows/system32/user32.dll')
>>> print libc.GetDesktopWindow()

Although I've refrained from personal opinion throughout this article, I have to say, this module is a godsend for those of us who have written code that calls into third-party libraries.

Working with XML through ElementTree

In the old days, parsing XML files was a nightmare. Generating XML files was a bit easier, although you had to do a lot of the work yourself. As the use of XML has grown and evolved, people have found better programming paradigms for managing XML (without XML itself changing). The ElementTree module greatly simplifies XML processing:

>>> from xml.etree import ElementTree as ET
>>> tree = ET.parse('test.xml')
>>> r = tree.getroot()
>>> r
<Element doc at aeeb70>
>>> def trav(node, indent=0):
...     for c in node.getchildren():
...         print ' '*indent, c.tag, ':', c.text
...         trav(c, indent+1)
>>> trav(r)
header1 : abc
text1 : This is some
    b : text
text1 : This is some more text

The classes in ElementTree let you read in your XML and traverse the XML hierarchy. This sample code I wrote uses recursions to traverse down through the children. For much of my own work with XML, I find that parsing the XML hierarchically like this rather than linearly is much more useful.

SQLite Support for Simple SQL Data Storage

The sqlite3 module is an interface to the SQLite library. This library provides SQL access to a single file for data storage, and I find it to be extremely useful for storing small amounts of local data without the need for large external database systems. Here's a sample:

import sqlite3
conn = sqlite3.connect('data.dat')
c = conn.cursor()
   create table members(
       id int,
       name varchar,
       login timestamp
   insert into members
   values (1,'jeff','2006-10-01')
   insert into members
   values (2,'angie','2006-10-02')
   insert into members
   values (3,'dylan','2006-10-03')

res = c.execute('select * from members')
for row in res:
   print row

This code connects to a database (which is simply a file, data.dat), then creates a new table in the database using standard SQL CREATE TABLE syntax, then inserts data, again using standard SQL syntax. Then the code finally selects data from the table, again with SQL, and iterates through the selected data.

This module is especially useful for data that isn't big enough to justify a large database system, but is relational in nature and requires more than simple data types.


Python 2.5 includes many welcome changes. For me personally, I will get great use out of the new ElementTree classes for XML processing and the SQLite classes for storing data.

The changes to the language itself, particularly generators and contexts, will help me write more robust code, taking away the emphasis on how to do something, and focusing more on what to do. For my own work, I know that moving the emphasis in that direction always helps reduce bugs.

As you explore the changes to version 2.5, remember that many of the changes are precursors of what will come when the big Python 3.0 eventually appears. If you start adopting the changes now, you'll be better prepared for Python 3.0, requiring less porting and changing of code. I encourage you to explore these changes and use them, and, most importantly, have fun!

Jeff Cogswell lives near Cincinnati and has been working as a software engineer for over 15 years.

Return to the Python DevCenter.

Sponsored by: