New or Improved Libraries

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:

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:

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:

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.

Summary

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.