Chapter 29. Python Resources

Programming languages, like natural languages, grow tight-knit communities— speakers of the same language have a natural affinity for each other. Programming languages, being the result of individual choice rather than accident of birth, lead to stronger feelings of kinship than might seem reasonable for what some see as a purely technological topic. Open source languages, which are never chosen because of a marketing campaign, but instead after a process of deliberation and comparison, seem to elicit even more enthusiasm (some would even say fanaticism) from their users. This chapter is about the community that defines itself as being “The Python Community,” from the inner sanctum of people who dream about Python daily to the occasional Python user.

Because writing shareable Python code is so easy, much of this community shares their enthusiasm and their work, quite often in the form of yet more free software. The resulting snowball effect (or, to use a more trendy term, the “network effect”) makes writing even large programs a snap compared to many other language choices. This chapter will point out some of the most valuable third-party offerings, from small modules to interfaces to operating system libraries to module repositories.

Layers of Community

In this section, we will discuss the various layers of the community, from the core of very serious programmers who implement the official Python interpreter, out through the Python Software Foundation, special interest groups and user groups, and out to the broad spectrum of participants, which is known as “python-list.” You’ll probably find that you belong in one or more of these neighborhoods, and may want to visit some of the neighborhoods you don’t yet know. Regardless of where you choose to settle, welcome to our community!

The Core

Unlike many language communities, the Python world has a very clear center. This center has grown over the years, with Guido van Rossum as the permanent core, surrounded physically by a trusty cohort called “Pythonlabs,” and surrounded virtually by the “python-dev group.” Pythonlabs consists of a few key Python developers(Tim Peters, Barry Warsaw, Jeremy Hylton, and Fred Drake) who were recruited by Guido to work with him on Python and Python-related projects, first at BeOpen.com, and then at Zope Corporation. Along with Guido, they have generally been the ones making the most radical changes to the Python internals, although there have been some notable exceptions from other contributors.

Python is too big for so few people to manage and grow as fast as its users would like (especially as Python is only a part-time job, even for Guido). A supporting cast, generally referred to as the python-dev crowd after the mailing list that anchors the discussions, is available to help in design discussions, implementing, testing, and, most of all, arguing (all in good faith, though).

To most Python users, however, the work of Pythonlabs and Python-dev is gratefully acknowledged but somewhat mysterious. Many more people live in one of the outer layers of Pythondom, either virtual or physical (or, hopefully, both). We’ll get back to the technically “deep” layers later—it’s far more reasonable to learn about a community from the tourism bureau than from the city planning committee, however.

Local User Groups

While most Python-related communications occur on the Internet, it’s nice to ground the names with faces and accents, and to get a feel for the real-world personalities behind the online personas. There are two great ways to do that, each with their own benefits: user groups and conferences.

You may have a local Python or Zope user group in your neighborhood. There is a list at http://www.python.org/UserGroups.html, but check with your local computer society, Linux users group or other such organization for possibly more up-to-date information. If there isn’t one yet, chances are quite good that there is pent-up demand for it, and they’re easy to organize. It’s a great occasion to learn more about Python, find out who else is using Python locally (which can be helpful at resume-passing time), and meet people with whom you share at least one interest. Both of the authors visit local user groups on a regular basis.

Conferences and Workshops

Whether or not you partake in an occasional beer with local Pythoneers, we encourage you to go to Python conferences. There are several regular conferences, all worth attending.

The oldest conference, the International Python Conference (IPC), is now part of the O’Reilly Open Source Convention (OSCON) (see http://conferences.oreillynet.com for information). IPC has grown over the years from a very small informal gathering to a large, very well attended meeting where Python experts, enthusiasts and novices from around the world come to mingle. For many, it is the only occasion to talk to their Python acquaintances in real life, and it’s well worth it.

A new conference, PyCon, has recently been started. Meant to complement the track at OSCON, it is a low-cost, hacker’s conference, emphasizing technical discussions and foregoing the more commercial aspects of trade shows such as an exhibit hall and fancy lunches. When not writing this chapter, I (David) am helping to plan the second PyCon event, which promises to have very high-quality presentations.

Both of the two conferences mentioned are based in the U.S. An alternative for some is to head to Europe for some of the Python conferences there: EuroPython (http://www.europython.org), and the Python UK Conference (http://www.python-uk.org). More conferences may have sprung up to accommodate the crowds. See http://www.python.org/workshops/ for updates.

Where to Get Help

As easy as Python is to learn, no book or website can give all the answers. If you have a Python-related question, there are a few avenues for free and paid support. Information about all of the mailing lists mentioned below, including how to subscribe, is available at http://www.python.org/psa/MailingLists.html.

Python-help

Should you have a Python question that you can’t get an answer for through the usual means, you can send an email to . Your email will be sent to a set of volunteers around the world who will do their best to answer you. Be as detailed as possible in your questions, copy and paste your code and the errors you get into your email (as opposed to often confusing paraphrases), and be patient as you await an answer. You’ll find python-help can be quite a useful resource.

Python-tutor

While python-help provides one-on-one help, python-tutor is a mailing list for people who are looking for help in the early stages of learning about Python or even learning about programming by using Python. It’s a friendly list where no question is deemed too basic and still is fairly low-volume. See http://www.python.org/psa/MailingLists.html#tutor for details.

Python-list

Python-list is the commons of Python development. It’s available either as a mailing list or as the Usenet newsgroup comp.lang.python. Over the years it’s grown to be a very popular destination, with thousands of messages a month. Conversations tend to be about Python, although some interesting excursions through the oddest topics do occur. It’s a good place to hang out on the net with like minded folk. Python-list defines the public face of the Python community to a large extent.

Special Interest Groups

All of the resources mentioned thus far have been generalist resources. Two kinds of more focused discussion groups also need to be mentioned. The first and most essential one is the discussion around the implementation of the language itself, on a list called python-dev. Additionally, some specialized topics naturally lead to long-term discussions, which can get quite technical but still remain within a well-defined domain. Typical examples of this kind of special interest group are the discussions aimed at defining specialized libraries that should be added to Python, such as those for scientific processing, database interfaces, XML or Unicode support. People interested in working on such areas typically form a special interest group(s) mailing list to coordinate their activities, get access to an area of http://www.python.org to publicize their results, and, when they have reached internal agreement, lobby the greater Python community (and Guido in particular) for inclusion of a particular functionality (as the Numeric SIG did for some syntactic changes) or just to report an agreement (as the Database SIG did to announce the definition of the DB-API standard). These days, many satellite discussions occur in other mailing lists that aren’t “official” SIGs, although those tend to not feed back to the language core quite as much. Information about SIGs is always available at http://www.python.org/sigs.

python-dev

Apart from those few “physical” conversations that happen in the Reston, VA area (where Pythonlabs team members are located), daily discussion about the development of Python itself happens on a mailing list called python-dev. The number of people participating in this list is considerably larger (dozens of active contributors, probably thousands of regular readers), and provides a very good forum for people who already know most of what there is to know about Python today, and want to participate in shaping tomorrow’s Python. If you’re curious, you can subscribe to the mailing list (http://mail.python.org/mailman/listinfo/python-dev), read it through a mail/web interface (e.g., http://aspn.activestate.com/ASPN/Mail/Browse/Threaded/python-dev/) or through a mail/news interfaces (see e.g. http://www.gmane.org). However, most readers of this book will find the discussions on python-dev to be much too detailed and technical. To get a feel for what’s keeping the core Python developers busy while staying awake, you’re better off reading the edited summaries that get posted to python-dev and python-list on a periodic basis and that are archived at http://www.python.org/dev/summary/ (you can also ask to receive them by email). It’s important to keep in mind that Python-dev is a working group. If you are a Python novice, you’re welcome to read, but you probably shouldn’t post except in very rare circumstances. You should definitely not ask for help on python-dev—as we’ve seen, there are many other, more appropriate places to go for help. Similarly, requests for new syntax or new features are in large part inappropriate on python-dev.

News Sources

If you’re too busy to follow python-list on a regular basis, there are several options to make sure that you learn of all new developments in the Python world.

Major announcements typically get sent both to python-list and to python-announce, which is a moderated list. That’s the primary forum to announce new Python packages or modules, new conferences, and other major announcements of broad interest to the community.

Several people report on Python developments, such as Fredrik Lundh, a longstanding web log of Python news at http://www.pythonware.com/daily/.

A rotating volunteer also monitors python-list and summarizes it on a weekly basis. Sponsored by Doctor Dobb’s Journal, archives and information on how to subscribe are available at http://www.ddj.com/topics/pythonurl/.

The Process

The topic of how Python is developed is a fascinating one, as Python is one of the better run open source projects, but it is mostly off-topic for this book. If you’re interested in learning more (whether you think you can contribute to Python yourself or not), read some of the information at http://www.python.org/dev—you’ll find everything from descriptions of the Python developer culture to specific technical details on how to contribute.

As a user of Python, however, you can play a role in the (unlikely) event that you find a bug in Python. If you do, you should isolate the code that’s causing you headaches or not behaving according to specification to the bare minimum, and post it as a bug on Python’s bug tracker. As of this writing, Python is using the bug tracker run by Sourceforge, although there is talk to move to something else. The bug manager is located at http://sourceforge.net/bugs/?group_id=5470, and instructions on how to submit a bug report are at http://www.python.org/doc/current/ext/reporting-bugs.html.

Services and Products

There are hundreds of thousands or perhaps millions of people using Python, and thousands of companies relying on it. Several commercial vendors, both corporations and individual consultants provide support of various kinds to help people and companies work with Python, from training to development tools to on-call support. Not altogether surprisingly, the first author’s main job is to teach Python to individuals and companies worldwide and the second author’s company provides developer tools and enterprise-level support for Python. Many other vendors exist—consult your usual channels to find the provider most suited to your needs.

The Legal Framework: The Python Software Foundation

Python is, at its core, a programming language. As a technology, Python needs an owner—a person or entity who can define it to the world, protect it from attack, and nurture its growth. While Guido van Rossum is the recognized father of Python, Guido doesn’t want to be the sole person responsible for Python. The “what if Guido gets hit by a bus?” discussions have been dealt with by, over the years, defining a legal entity called the Python Software Foundation (PSF) to act as Python’s legal owner. Thus, all of the Python intellectual property is being assigned to the PSF. The PSF has received provisional non-profit status from the US IRS, thus making donations to the organization tax-deductible. The PSF is composed of individual members (invited by the existing membership because of their contribution to Python) and is funded by corporate sponsorships. Information about the PSF is available at http://www.python.org/psf/. Both of the authors are members of the PSF and the second author is a director of the PSF. Essentially, what this means is that the PSF is an organization we believe is important to the long-term health of Python.

Software

This book’s task was to present the Python language, including brief overviews of some of the most important modules and libraries that come with Python. There are countless other such supporting software packages available, most of them for free, on the Internet. In this section, we give you pointers as to what this landscape of software looks like, what maps are available to help you find what you’re looking for, and finally some notable software packages that can make choosing Python such a high-value choice.

One of Python’s weaknesses has been the lack of a single, authoritative repository for such third-party software. While there are volunteers working hard to solve that problem, the best we can do at time of writing is to point you to the several alternative methods that can be used to find out what’s available and where.

Search the Web

It used to be hard to find things on the Internet. Some of us remember days before the Web, when word of mouth and secret handshakes seemed to be required to find particular pieces of software. These days, search engines like Google do 95% of the hard work. Regardless of the topic, searches on Google are very likely to get you what you want.

Search the Mailing List Archives

Software that’s available on the Web has typically been announced in public, or at the very least discussed in public. You can search the various mailing lists mentioned above with specialized search engines, such as Google’s Groups interface (although that doesn’t cover all of the Python mailing lists, only those mirrored as newsgroups), http://python.org/search, or the mailing list archives at http://aspn.activestate.com/ASPN.

Look in the Vaults of Parnassus

“The Vaults of Parnassus” is a fairly old (in Internet years) and well-established directory of Python software. It uses a library-style directory of Python software and Python-related tools. The vaults are at: http://py.vaults.ca/. Note that the vaults archive only metadata finding something on the vaults is no guarantee that the pages it refers you to are still around, or that the information on the vaults are necessarily up to date.

Check the Python Package Index (PyPI)

A new project which, unlike some of its predecessors, seems likely to succeed is called PyPI (Python Package Index). Hosted at http://www.python.org/pypi, the current prototype lets people register their python package either manually, or, preferably, using the package description software distutils, which is part of the standard library. As of this writing there are only a few dozen packages listed, but by the time you read this the catalog is likely to be much larger.

Look in the Python Cookbook

The Python Cookbook is a joint project combining the efforts of ActiveState, O’Reilly and Associates, and the Python community. ActiveState hosts a web site (aspn.ActiveState.com/Python/Cookbook), which lets anyone post their favorite recipe of Python code, and solicit feedback from readers of the site. O’Reilly published a book from selected, edited, and expanded recipes called the Python Cookbook, coedited by Alex Martelli and David Ascher. The book contains hundreds of well-motivated recipes explained in detail, and has become a favorite even of long-time Python programmers. The online site contains hundreds more recipes, and is constantly being updated. Both are excellent resources for that smallest kind of software package, the snippets and idioms that define the fluent speaker of a language.

Popular Third-Party Software

In this section, we list some of the most popular third-party add-ons to Python. Some are small yet deeply useful modules, others are full fledged applications with massive internal complexity. Each is what we consider a good tool.

URLs change, so don’t be dissapointed if the URLs we mention are no longer valid by the time you type them in. Instead, go to Google and type python name of the package—you’re more than likely to find it.

Interfaces to Windows and the MacOS

While each operating system provides a wide variety of interfaces, Unix and related operating systems like Linux tend to provide that interface through command-line tools and special-purpose files, both of which tend to vary too much across versions to allow for useful programmatic interfaces. Windows and Macintosh use a more API-oriented approach, and as a result make the operating system more naturally accessible from a programming language like Python. There are Python interfaces to pretty much every corner of Windows and the Macintosh APIs.

Windows

Core Python comes with some interfaces to basic Windows interfaces like the os module for basic operating system functions and the _regedit low-level API to the Windows Registry. Serious Windows programming, however, requires access to many more Windows libraries. Most of these are exposed by the win32all package by Mark Hammond. win32all is available either as an add-on to the Python distribution from http://www.python.org, or bundled as part of ActivePython from ActiveState (http://www.ActiveState.com/Python). win32all also includes a Window-only IDE for Python, Pythonwin.

Not all Windows APIs are exposed by win32all. Should you wish to use one of these, you can use Thomas Heller’s ctypes module, which provides a foreign function interface from Python to dynamically loaded shared libraries. ctypes is described below.

Macintosh

The Macintosh port of Python, maintained by Jack Jansen, and available at http://www.cwi.nl/~jack/macpython.html, comes in two kinds as of this writing. There is a new version that runs from the Mac OS X command line, as well as a version which runs on Mac OS 9 or OS X, although there are plans to merge the two. The documentation for the Macintosh library, the Macintosh Library Modules, is part of the standard library reference. Recent versions of Mac OS X come with Python preinstalled.

Special-Purpose Libraries

There are some domains of computing that lend themselves to well-defined libraries. These typically involve specialized data types (e.g., arrays of numbers, dates) or special-purpose algorithms (e.g., three-dimensional graphics engines). In this section we survey some of the most popular such libraries.

Scientific computing libraries

Python has a significant following in the scientific and engineering fields. Its fairly mathematically reasonable syntax and ease of extensibility make it a good match for scientists who need to combine easy to use syntax with the ability to attach high-power computational engines.

The granddaddy of scientific libraries in Python is called Numeric Python, NumPy, or Numeric for short. Numeric consists of a set of Python and C extension modules that provide very powerful and high performance array operations. With Numeric, one can do “en masse” operations on very large multidimensional arrays of numbers much faster than one can do in pure Python. Numeric is the current standard solution for doing such operations in Python, although many are looking to its proposed replacement, numarray.

Here’s an example of typical NumPy code, numpytest.py, and one representation of the data in generates:

from Numeric import *              
coords = arange(-6, 6, .02)               # Create a range of coordinates.
xs = sin(coords)                          # Take the sine of all of the x's.
ys = cos(coords)*exp(-coords*coords/18.0) # Take a complex function of the y's.
zx = xs * ys[:,NewAxis]                   # Multiply the x row with the y column.

If you remember your math, you might figure out that xs is an array of the sines of the numbers between -6 and 6, and ys is an array of the cosines of those same numbers scaled by an exponential function centered at 0. zs is simply the outer product of those two arrays of numbers. If you’re curious as to what that might look like, you could convert the array zs into an image and obtain the image shown in Figure 29-1.

Graphical representation of the array zs in numpytest.py
Figure 29-1. Graphical representation of the array zs in numpytest.py

NumPy lets you manipulate very large arrays of numbers very efficiently. The preceding code runs orders of magnitude faster than comparable code using large lists of numbers and uses a fraction of the memory. Many Python users never have to deal with these kinds of issues, but many scientists and engineers require such capabilities daily.

Numarray (http://stsdas.stsci.edu/numarray/) is a reimplementation of the basic functionality of Numeric, while aiming to fix some of the problems with the original having to do with type conversions, extensibility, memory efficiency and other factors. It’s too early to tell, but our sources indicate that numarray is well positioned to take over Numeric’s market share in a nice, progressive, and orchestrated transition.

For those interested in doing more with numbers, the SciPy project is worth looking at. SciPy supplements the core array-crunching modules with a variety of high-level scientific and engineering modules, such as solvers, optimizers, statistical routines, and a very interesting inlining tool that compiles expressions using C++ in a semi-transparent way, called weave. SciPy also includes the most Pythonic scientific graphing software, called Chaco. Information about SciPy can be found at its web site, http://www.scipy.org.

Finally, there are other scientific libraries for Python not listed above—some for dealing with Fortran libraries, some for doing specialized processing in the fields of chemistry, genetics, etc. Use the resources mentioned to look for software in your particular field.

Relational database interfaces

Relational databases are a very common mode of storing and manipulating data, although as we’ve seen in this book, there are other, simpler ways of storing at least small amounts of data (such as using pickle, or the related marshal and shelve modules—see their documentation for details). All popular scripting languages have interfaces to common database engines, and Python is no exception. There is even a standard interface that allows Python programmers to talk to a variety of databases in a database-independent way, called the DB-API. Details on the DB-API, including references to current modules, are available at http://www.python.org/topics/database/. You will find that there are interfaces to Oracle, Postgres, MySQL, SAP DB, Informix, and many others.

There are other, less standard databases that are worth knowing about in case you don’t have a particular reason to use a SQL server. The two most commonly used ones are Gadfly and MetaKit. Gadfly (http://gadfly.sourceforge.net/gadfly.html), is a SQL server written entirely in Python (with some C extensions), which provides a very simple SQL solution for people who have relatively small databases (Gadfly stores its data in memory), and don’t require multiuser capabilities, but still want transactional support. MetaKit (http://www.equi4.com/metakit/python.html) is a C++ database engine with very good Python bindings. MetaKit has a more record-oriented view of the world, and is remarkably fast at a remarkable set of operations. A third and more recent entry half-way between Gadfly and full-fledged SQL databases is PySQLite, an interface to the popular embedded SQL engine SQLite.

A final player worth mentioning here is the set of tools from eGenix.com. These include mxODBC (an interface to the ODBC database layer), mxDateTime (a high-power date/time library), and other add-ons. These are all available at http://www.egenix.com).

Graphics libraries

There are several types of packages that can help produce graphical output of various kinds, from image processing tools to game frameworks to visualization toolkits.

Python Imaging Library (PIL)

The Python Imaging Library is an extensive framework written by Fredrik Lundh for creating, manipulating, converting, and saving bitmapped images in a variety of formats such as GIF, JPEG, and PNG. It has interfaces to Tk and Pythonwin, so that one can use either Tk widgets or Pythonwin code to display PIL-generated images. Alternatively, the images can be saved to disk in a variety of formats. The home for PIL is at http://www.pythonware.com.

PyGame and PyOpenGL

Two of the most popular selections for graphics work are PyGame for two-dimensional and gaming software, and PyOpenGL for 3-D graphics, especially in the area of scientific visualization. Pygame (http://pygame.org) is a set of Python modules written on top of the free, cross-platform SDL library. It has been used for a variety of game and nongame software, and provides excellent interfaces to the screen, the mouse, the keyboard and sound output. People have written some remarkable games with PyGame, as well as used it for nongaming applications that have heavy emphasis on game-like animations, controls etc. A simple example of PyGame programming is the following snippet, which plays the first five seconds of the first track of the first CD player it finds, and then ejects it.

import pygame
pygame.cdrom.init(  )
cd_object = pygame.cdrom.CD(pygame.cdrom.get_count(  )-1)
cd_object.init(  )
if cd_object.get_track_audio(i):
    audio_track_found = 1
    cd_object.play(i)
    pygame.time.delay(5000)
cd_object.eject(  )
cd_object.quit(  )

PyOpenGL is a fairly different tool—it is a wrapper around OpenGL, the standard for cross-platform high-performance 2-D and 3-D graphics. PyOpenGL, available from http://pyopengl.sourceforge.net, provides Python-level interfaces to a very large number of OpenGL APIs as well as related libraries such as GLU, GLUT, and more. PyOpenGL, especially when combined with Numeric Python arrays and used with a graphics card that provides hardware acceleration of OpenGL, can produce stunningly fast graphics. Anyone looking to do scientific or engineering visualization should explore this option.

Interfaces to GUI Toolkits

Working with a GUI toolkit is a requirement for many people who write end-user- software. The decision of which GUI toolkit to use is typically quite a complex one, involving such factors as portability, performance, look and feel, licensing and cost, threading models, etc. Suffice it to say that Python has interfaces to all of the popular GUI toolkits on the major platforms.

There are several popular cross-platform toolkits with Python interfaces:

  • Tk has an interface that ships with standard Python called Tkinter. While Tk’s widget set isn’t as rich as others, there are ways to extend it using megawidget tools such as Pmw (http://pmw.sf.net).

  • wxPython, the interface to the wxWindows GUI toolkit, has become very popular of late (see http://www.wxPython.org for details).

  • PyQt on top of Qt (see http://www.riverbankcomputing.co.uk/pyqt/ for details; note the somewhat restrictive licensing terms and purchasing options).

Another cross-platform solution is:

For the most native Windows look and feel, you can use either low-level Win32 calls through the win32ui and win32gui modules, or the higher-level Microsoft Foundation Classes (MFC). Both interfaces are available with win32all, either standalone or bundled with ActivePython.

Jython users can use the native Java toolkits (AWT and Swing).

Unix/Linux users can also use:

Several other toolkit interfaces are available, but have been omitted here because they are not as popular or battle-tested.

Interfaces to C/C++/Fortran

Many of the libraries mentioned are wrappers around third-party libraries written in a compiled language like C, C++ or Fortran. There are several options available to those wishing to expose such libraries to Python.

SWIG and f2py

The oldest, but still very useful package is SWIG (Simple Wrapper Interface Generator), available from http://www.swig.org. SWIG, unlike all of the other packages mentioned in this section, works with other languages in addition to Python. SWIG takes (a sometimes tweaked version of ) C or C++ header files and automatically produces the interfacing code that exposes the C/C++ APIs to Python. It is especially well-suited to large code bases with clean APIs, such as OpenGL, the Windows API, and other well-defined codebases. SWIG makes writing such interfaces quite simple even for nonexperts, and takes care of many of the tedious details such as reference counting, error checking and the like.

People interested in wrapping Fortran 77/90/95 should investigate f2py, available from http://cens.ioc.ee/projects/f2py2e/.

CXX and Boost::Python

Sometimes one wants to write extensions in C or C++. C extension writers tend to use the API that comes with Python (documented in Extending and Embedding the Python Interpreter, which is part of the standard documentation set, available at http://www.python.org/doc/current/ext/ext.html). C++ extension writers tend to use one of two popular tools, CXX and Boost::Python. CXX was designed to write extensions in C++ instead of C. Standard CXX code looks like a hybrid of C++ and Python—one manipulates Python-style data structures using C++ syntax. CXX takes care of reference counting and of mapping C++ exceptions to Python and vice-versa. Boost::Python is a more recent library, part of the well-respected Boost C++ library, and probably more actively maintained than CXX as of this writing. Boost::Python is a framework for interfacing Python and C++, designed to let you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools. Because it aims to wrap C++ interfaces unintrusively, you should not have to change the C++ code at all in order to wrap it. To compare the two, CXX is probably the right choice when trying to write an extension from scratch whose only purpose is to be a Python extension, while Boost::Python is more appropriate for wrapping C++ code and making a Python interface to that interface. Your mileage may vary, and you are encouraged to study both before embarking on a significant project.

Pyrex, Weave, and Psyco

SWIG and Boost::Python are designed mostly to wrap existing codes. CXX, while it can be used to interface to C++ libraries, is more of a straight port of the C extension mechanism to C++. Given that many C extensions are written strictly to optimize away some bottlenecks, an alternative approach is to use C or C++ to write special-purpose code strictly for performance reasons. Three other systems target this case: Pyrex, Weave and Psyco.

Pyrex is a language specially designed to write extensions for Python. Pyrex can be summarized as Python with C data types. It’s best illustrated with a simple example:

def primes(int kmax):
    cdef int n, k, i
    cdef int p[1000]
    result = [  ]
    if kmax > 1000:
        kmax = 1000
    k = 0
    n = 2
    while k < kmax:
        i = 0
        while i < k and n % p[i] <> 0:
            i = i + 1
        if i == k:
            p[k] = n
            k = k + 1
            result.append(n)
        n = n + 1
    return result

When Pyrex runs this code, it notices the cdef statements that tell it that n, k and i are C integers, and that p is an array of 1,000 C integers. The rest of the function is straight Python code, but thanks to those declarations, Pyrex can compile the code to C, making the key loop run much faster than if this same program had run in Python.

Weave takes a different approach to solve the same fundamental problem—how to insert C/C++ code inside of otherwise perfectly fine, logical Python code. Unlike Pyrex, Weave uses no new syntax, but instead requires you to insert C or C++ code in a Python string, and to call out to a specialized module to do compilation and loading behind the scenes. So the straightforward but slow binary search routine written in Python as:

def binary_search(seq, t):
        min = 0; max = len(seq) - 1
        while 1:
            if max < min:
                return -1
            m = (min  + max)  / 2
            if seq[m] < t: 
                min = m  + 1 
            elif seq[m] > t: 
                max = m  - 1 
            else:
                return m

can be written in Weave as:

def c_int_binary_search(seq,t):
    # Do a little type checking in Python.
    assert(type(t) == type(1))
    assert(type(seq) == type([  ]))
    
    # Now the C code
    code = """
           # Line 29 "binary_search.py"
           int val, m, min = 0;  
           int max = seq.length(  ) - 1;
           PyObject *return_val; 
           for(;;) {
               if (max < min) { 
                   return_val =  Py::new_reference_to(Py::Int(-1)); 
                   break;
               } 
               m =  (min + max) /2;
               val = py_to_int(PyList_GetItem(seq.ptr(  ),m),"val"); 
               if (val  < t) 
                   min = m  + 1;
               else if (val >  t)
                   max = m - 1;
               else {
                    return_val = Py::new_reference_to(Py::Int(m));
                    break;
                }
           }
           """
    return inline(code,['seq','t'])

Weave is part of SciPy, available from http://www.scipy.org/site_content/weave.

Psyco is the most ambitious of any of the projects mentioned here, and, as a result the least practically useful at this stage. Psyco is a specializing compiler for Python. The details are much beyond the level of this book, and Psyco isn’t of much use today, but it offers great promise for the future, including the possibility of making Python code run faster than C code, which is considered the benchmark for interpreted languages. The curious can read up on the current Psyco project at http://psyco.sourceforge.net/, as well as a related project, pypy, which aims to write a new implementation of Python in Python, and to build Psyco-style technology to compile it (see http://codespeak.net/pypy/).

ctypes

The latest entry in this set of tools is ctypes. ctypes uses a completely different approach to interfacing—unlike SWIG, Boost::Python or the standard C extension module system, ctypes is designed to call directly from Python into shared libraries. Using platform-specific tricks (ctypes currently runs only on Windows, Linux and OS/X, although more platforms may be added), ctypes lets you load arbitrary shared libraries and call arbitrary functions, as long as you know the signature of these functions:

>>> from ctypes import cdll, c_double
>>> printf = cdll.msvcrt.printf
>>> printf("An int %d, a double %f
", 1234, c_double(3.14))
An int 1234, a double 3.140000

There is a lot more to ctypes that makes this approach realistic for a wide variety of applications. See http://starship.python.net/crew/theller/ctypes.html for details.

Little Gems

In this section, we’ll list some modules that, while not necessarily deeply complex or major efforts, provide nice solutions to common problems.

The platform module (part of the standard library in Python 2.3 but available for earlier python version at http://www.egenix.com/files/python/platform.py) is a module that lets you find out many more subtle variations of what your current platform, compared to sys.platform. Instead of just saying win32, it lets you know that you’re running on e.g. "Windows-2000-5.0.2195-SP3“. Sometimes this sort of information is crucial, and it’s hard to identify without code like that in platform.py.

turtle.py, which is part of the standard distribution but unknown to many, provides a simple LOGO-like system for interactive drawing and beginning programmers. Built on Tkinter, it provides a nice first step for teaching programming.

Trent Mick’s go (at http://starship.python.net/crew/tmick/) is a simple command line tool that makes it trivial to build bookmarks of often-used directories. Instead of having to remember long path names, you can teach go which directories you use often and then use two-word commands to go there (go home, go python23, etc.).

Packaging Tools

Python developers who wish to share their Python code with other Python developers should learn how to use the distutils package, which lets people build distributions of their programs that are easy for other developers to install. distutils is documented as part of the standard Python library (http://www.python.org/doc/current/dist/dist.html).

Python developers who are building standalone applications for use by non-Python developers have a variety of approaches available to them. To make it easy for end users, it is generally a good idea to ship a single file that installs everything the users need—this may include all or a part of the Python distribution. While you can do it the hard way, by shipping the hundreds of files that make up this distribution, it is much easier to use a tool that packages the program and the part of the standard distribution that the program needs.

The Python distribution includes such a program, called freeze, which, while it should work, has far fewer features than the alternatives.

Installer

The most popular packaging tool is probably Gordon McMillan’s Installer tool. Installer inspects your source code to find out all of the modules that it imports, as well as the modules that they import and so on, until it has built a list of all of the files it thinks your program needs. Those files, including the shared libraries that contain the Python interpreter and extension modules you use, are then packaged in an executable, which you can ship to your users. When the executable is run, it unpacks its contents in a temporary directory and runs the code from there. Installer works on Windows and Linux.

py2exe

Windows users have an alternative to Installer that has a more standard approach—it leverages the existing distutils mechanisms for defining the contents of a package, extends it with a few special purpose options, but generally performs the same task as Installer. py2exe has specialized support for packaging Windows NT Services.

Web Application Frameworks

Anyone who has used the Internet recently knows that it’s very possible to develop full-featured applications that happen to use web browsers as their GUI. In the last few years, a plethora of web application development frameworks for Python have been developed. They span a huge gamut, from simple hacks to elaborate systems. Many allow embedding of Python code in HTML, others generate HTML from Python code. Many provide support for persistence, cookie management, URL rewriting, and more. A comparative review of all of these is far beyond the scope of this book. We’ll simply mention some of the more commonly used frameworks.

Zope

Zope is the grand-daddy of Python web application frameworks. While Zope is open source, it is very much the product of a company called Zope Corp., in collaboration with a vast user community. Zope is a very powerful tool for building content management systems, including such advanced features as replication, transactional support, sophisticated security models and workflow. Zope often stumps people who expect it to be a simple system. While efforts are under way to redesign part of Zope to make learning Zope easier, those who find Zope most useful are typically those with very large or complex websites to build. Information about the Zope software is available at http://www.zope.org, and information about Zope Corp. is at http://www.zope.com.

Twisted

If Zope is the web application server for sites with sophisticated workflow, Twisted is more of a swiss army knife for networked application development. Not strictly focused on the web and the HTTP/HTML standards that anchor the web, Twisted is a framework for building networked applications. Using Twisted, it is relatively easy to build high-performance clients and servers for any protocol, from instant messaging to IRC to HTTP to NNTP. The Twisted framework, like all frameworks, requires a certain learning curve. Those who do learn it do tend to be passionate about it, and it seems to perform admirably. Twisted’s home is http://www.twistedmatrix.com.

Quixote

Quixote is a dynamic web application framework built by Python programmers for Python programmers. Unlike many of the alternatives such as Zope, Quixote deliberately does not try to cater to web designers. To use Quixote means to program in Python, even for HTML generation. To those of us who are more comfortable with Python modules and classes than with HTTP redirects, however, that’s a great benefit. Quixote has wonderfully clear documentation (at least if you are comfortable with Python) at http://www.mems-exchange.org/software/quixote/.

Webware, Spyce, and More

The three systems, Zope, Twisted, and Quixote, while probably among the most popular, do not cover the extent of Python/web frameworks. If embarking on a new project, you owe it to yourself to compare the various frameworks and figure out which one is best for you. Programming on the Web is a complex task with lots of variables—it’s very possible that in your particular case, you’d want to use something more like a PHP-style system such as Spyce (http://spyce.sourceforge.net), or more of a component-oriented system like WebWare for Python (http://webware.sourceforge.net/).

Tools for Python Developers

Given the large number of Python developers and the large number of Python programs that need editing and debugging, there is a wide variety of editing and development tools available for Python programmers. They range from customization files for free general-purpose editors like Emacs and Vim to specialized integrated development environments. They can be free, such as Idle (which comes with Python) and Pythonwin (part of win32all and ActivePython), or commercial products like Archaeopteryx’s Wing IDE and ActiveState’s Komodo and Visual Python .NET. Be sure to research what’s available at the time you need it, as this is an area where new tools and new revisions of existing tools show up fairly often.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.240.222