home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam    

Preface "And Now for Something Completely Different . . . Again" Signs of the Python Times Why This Edition? Major Changes in This Edition Using the Examples and Demos Conventions Used in This Book Where to Look for Updates Contacting O'Reilly Acknowledgments

TOC  Chapter 1

Preface

"And Now for Something Completely Different . . . Again"

The first edition of this book was one of the first to present the Python language. This second edition is an almost completely new advanced Python topics book, designed to be a follow-up to the core language material in Learning Python and supplemented by the reference material in Python Pocket Reference.

That is, this edition is focused on ways to use Python, rather than on the language itself. Python development concepts are explored along the way -- in fact, they really become meaningful only in the context of larger examples like those in this edition. But in general, this text now assumes that you already have at least a passing acquaintance with Python language fundamentals, and moves on to present the rest of the Python story.

In this preface, I'll explain some of the rationales for this major rewrite, describe the structure of this edition in more detail, and give a brief overview of how to use the Python programs shipped on the enclosed CD-ROM (view CD-ROM content online at http://examples.oreilly.com/python2). First of all, though, a history lesson is in order.

Signs of the Python Times

It's been an exciting five years in the Python world. Since I wrote the first edition of this book between 1995 and 1996, Python has grown from a new kid on the scripting languages block to an established and widely used tool in companies around the world. Although measuring the popularity of an open source (http://opensource.org) and freely distributed tool such as Python is not always easy, most statistics available reveal exponential growth in Python's popularity over the last five years. Among the most recent signs of Python's explosive growth:

Books

As I write this in 2001, there are now over a dozen Python books on the market, with almost that many more on the way (in 1995 there were none). Some of these books are focused on a particular domain (e.g., Windows), and some are available in German, French, and Japanese language editions.

Users

In 1999, one leading industry observer suggested that there were as many as 300,000 Python users worldwide, based on various statistics. Other estimates are more optimistic still. In early 2000, for instance, the Python web site was already on track to service 500,000 new Python interpreter downloads by year end (in addition to other Python distribution mediums); this figure is likely closer to the true user-base size as I write this book.

Press

Python is now regularly featured in industry publications. In fact, since 1995, Python creator Guido van Rossum has appeared on the cover of prominent tech magazines such as Linux Journal and Dr. Dobb's Journal; the latter publication gave him a programming excellence award for Python.[1]

Applications

Real companies have adopted Python for real products. It has shown up animating the latest Star Wars movie (Industrial Light & Magic), serving up maps and directories on the Internet (Yahoo), guiding users through Linux operating system installation (Red Hat), testing chips and boards (Intel), managing Internet discussion forums (Egroups), scripting online games (Origin), talking to CORBA frameworks (TCSI), implementing web site tools (Digital Creations' Zope), scripting wireless products (Agilent), and much more.[2]

Newsgroup

User traffic on the main Python Internet newsgroup, comp.lang.python, has risen dramatically too. For instance, according to eGroups (see http://www.egroups.com/group/python-list), there were 76 articles posted on that list in January 1994, and 2678 in January 2000 -- a 35-fold increase. Recent months have been busier still (e.g., 4226 articles during June, 2000 alone -- roughly 140 per day), and growth has been constant since the list's inception. This, and all other user-base figures cited in this preface, are likely to have increased by the time you read this text. But even at current traffic rates, Python forums are easily busy enough to consume the full-time attention of anyone with full-time attention to burn.

Conferences

There are now two annual Python conferences, one of which is hosted by O'Reilly & Associates. Attendance at Python conferences has roughly doubled in size every year. An annual Python Day is now also held in Europe.

Group therapy

Regional Python user groups have begun springing up in numerous sites in the U.S. and abroad, including Oregon, San Francisco, Washington D.C., Colorado, Italy, Korea, and England. Such groups work on Python-related enhancements, organize Python events, and more.

Domains

Python has grown to embrace both Microsoft Windows developers, with new support for COM and Active Scripting, as well as Java developers, with the new JPython (renamed "Jython") Java-based implementation of the language. As we'll see in this edition, the new COM support allows Python scripts to be both component server and client; Active Scripting allows Python code to be embedded in HTML web page code and run on either client or server; and JPython compiles Python scripts to Java Virtual Machine code so that they can be run in Java-aware systems and can seamlessly integrate Java class libraries for use by Python code. As an open source tool for simplifying web site construction, the Python-based Zope web application framework discussed in this edition has also begun capturing the attention of webmasters and CGI coders.

Services

On the pragmatics front, commercial support, consulting, prepackaged distributions, and professional training for Python are now readily available from a variety of sources. For instance, the Python interpreter can be obtained on CDs and packages sold by various companies (including Walnut Creek, Dr. Dobb's Journal, and ActiveState), and Python usually comes prebuilt and free with most Linux operating system distributions.

Jobs

It's now possible to make money as a Python programmer (without having to resort to writing large, seminal books). As I write this book, the Python job board at http://www.python.org/Jobs.html lists some 60 companies seeking Python programmers in the U.S. and abroad. Searches for Python at popular employment sites yield even more hits -- for instance, 285 Python-related jobs on Monster.com, and 369 on dice.com. Not that anyone should switch jobs, of course, but it's nice to know that you can now make a living by applying a language that also happens to be a pleasure to use.

Tools

Python has also played host to numerous tools development efforts. Among the most prominent as I write these words: the Software Carpentry project, which is developing new core software tools in Python; ActiveState, which ison the verge of releasing a set of Windows and Linux-focused Python development products; and PythonWare, which is about to release an integrated Python development environment and GUI builder.

Compilers

As I write this preface, ActiveState has also announced a new Python compiler for the Microsoft .NET framework and C# language environment -- a true Python compiler and independent implementation of the Python language that generates DLL and EXE files, allows Python code to be developed under Visual Studio, and provides seamless .NET integration for Python scripts. It promises to be a third implementation of Python, along with the standard C-based Python, and the JPython Java-based system.

Education

Python has also begun attracting the attention of educators, many of whom see Python as a "Pascal of the 2000s" -- an ideal language for teaching programming, due to its simplicity and structure. Part of this appeal was spawned by Guido van Rossum's proposed Computer Programming for Everybody (CP4E) project, aimed at making Python the language of choice for first-time programmers worldwide. At this writing the future of CP4E itself is uncertain, but a Python special interest group (SIG) has been formed to address education-related topics. Regardless of any particular initiative's outcome, Python promises to make programming more accessible to the masses of people who will surely soon grow tired of clicking preprogrammed links, as they evolve from computer users to computer scripters.

In other words, it's not 1995 anymore. Much of the preceding list was unimaginable when the first edition of this book was conceived. Naturally, this list is doomed to be out of date even before this book hits the shelves, but it is nonetheless representative of the sorts of milestones that have occurred over the last five years, and will continue to occur for years to come. As a language optimized to address the productivity demands of today's software world, Python's best is undoubtedly yet to come.

So What's Python?

If you are looking for a concise definition of this book's topic, try this:

Python is a general-purpose open source computer programming language, optimized for quality, productivity, portability, and integration. It is used by hundreds of thousands of developers around the world, in areas such as Internet scripting, systems programming, user interfaces, product customization, and more.

Among other things, Python sports object-oriented programming (OOP); a remarkably simple, readable, and maintainable syntax; integration with C components; and a vast collection of precoded interfaces and utilities. Although general-purpose, Python is often called a scripting language because it makes it easy to utilize and direct other software components. Perhaps Python's best asset is simply that it makes software development more rapid and enjoyable. To truly understand how, read on.

Why This Edition?

One consequence of the growing popularity of Python has been an influx of new users, programming styles, and applications, all of which have conspired to make parts of the first edition of this book prime for updates. Python itself has changed in smaller ways, but important extensions have simplified various aspects of Python development and merit new coverage.

Perhaps most relevant for this edition is that the Python "audience" has changed. Over the last five years, Python has transitioned from an emerging language of interest primarily to pioneers to a widely accepted tool used by programmers for day-to-day development tasks. This edition has been refocused for this new Python audience. You will find that it is now more of a nuts-and-bolts text, geared less toward introducing and popularizing the language than to showing how to apply it for realistically scaled programming tasks.

Because of the breadth of change, this edition is something of an entirely new book. To readers who enjoyed the first edition, I would like to express my gratitude, and I hope you will find the same spirit in this second edition. Although this is a major rewrite, I have tried to retain as much of the original book's material and flavor as possible (especially the jokes :-).

Since writing the first edition five years ago, I have also had the opportunity to teach Python classes in the U.S. and abroad, and some of the new examples reflect feedback garnered from these training sessions. The new application domain examples reflect common interests and queries of both myself and my students. Teaching Python to workers in the trenches, many of whom are now compelled to use Python on the job, also inspired a new level of practicality that you will notice in this edition's examples and topics.

Other new examples are simply the result of my having fun programming Python. Yes, fun; I firmly believe that one of Python's greatest intangible assets is its ability both to kindle the excitement of programming among newcomers, and to rekindle the excitement among those who have toiled for years with more demanding tools. As we will see in this edition, Python makes it incredibly easy to play with advanced but practical tools such as threads, sockets, GUIs, web sites, and OOP -- areas that can be both tedious and daunting in traditional compiled languages like C and C++.

Frankly, even after eight years as a bona fide Pythonista, I still find programming most enjoyable when it is done in Python. Python is a wildly productive language, and witnessing its application first-hand is an aesthetic delight. I hope this edition, as much as the first, will demonstrate how to reap Python's productivity benefits and communicate some of the satisfaction and excitement found in a rapid-development tool such as Python.

Major Changes in This Edition

The best way to get a feel for any book is to read it, of course. But especially for people who are familiar with the first edition, the next few sections go into more detail about what is new in this edition.

It's Been Updated for Python 2.0

This edition has been updated for Python 2.0, and the Graphical User Interface (GUI) material has been updated for Tk versions 8.0 or later. Technically, this update was begun under Python 1.5.2, but all examples were revisited for 2.0 before publication.

For the trivia buffs among you: release 2.0 was the first Python release following Guido's move to BeOpen, while 1.6 was the last release from Guido's prior employer, CNRI. Just before I finished this book's final draft and after the 2.0 release, Guido and the core Python development team moved from BeOpen to Digital Creations, home of the Zope web application construction kit, but this move is independent of Python releases (see Chapter 1, for more details).

Release 2.0 introduces a few language extensions, but 2.0 and 1.6 are similar in content, and the updates just add a handful of features. The examples in this book should generally work with later Python releases. Remarkably, almost all examples in the first edition still work five years later, with the latest Python releases; those that didn't work required only small fixes (e.g., GUI call formats and C API interfaces).

On the other hand, although the core language hasn't changed much since the first edition, a number of new constructs have been added, and we'll apply them all here. Among these new Python features: module packages, class exceptions, pseudo-private class attributes, unicode strings, the new regular expression module, new Tkinter features such as the grid manager, standard dialogs, and top-level menus, and so on. A new appendix summarizes all of the major changes in Python between the first and second editions of this book.

In addition to the language changes, this book presents new Python tools and applications that have emerged in recent years. Among them: the IDLE programming interface, the JPython (a.k.a. "Jython") compiler, Active Scripting and COM extensions, the Zope web framework, Python Server Pages (PSP), restricted execution mode, the HTMLgen and SWIG code generators, thread support, CGI and Internet protocol modules, and more (it's been a busy five years). Such applications are the heart and soul of this second edition.

It's Been Refocused for a More Advanced Audience

This edition presents Python programming by advanced examples. Becoming proficient in Python involves two distinct tasks: learning the core language itself, and then learning how to apply it in applications. This edition addresses the latter (and larger) of these tasks by presenting Python libraries, tools, and programming techniques. Since this is a very different focus, I should say a few words about its rationale here.

Because there were no other Python books on the horizon at the time, the first edition was written to appeal to many audiences at once -- beginners and gurus alike. Since then, another O'Reilly book, Learning Python, has been developed to address the needs of beginners, and Python Pocket Reference was published for readers seeking a short Python reference. As a result, the core language introductory-level material and the original reference appendixes have been removed from this book.

Learning Python introduces the core language -- its syntax, datatypes, and so on -- using intentionally simplistic examples. Many have found it to be ideal for learning the language itself, but Python can become even more interesting once you master the basic syntax and can write simple examples at the interactive prompt. Very soon after you've learned how to slice a list, you find yourself wanting to do real things, like writing scripts to compare file directories, responding to user requests on the Internet, displaying images in a window, reading email, and so on. Most of the day-to-day action is in applying the language, not the language itself.

Programming Python focuses on the "everything else" of Python development. It covers libraries and tools beyond the core language, which become paramount when you begin writing real applications. It also addresses larger software design issues such as reusability and OOP, which can only be illustrated in the context of realistically scaled programs. In other words, Programming Python, especially in this new edition, is designed to pick up where Learning Python leaves off.

Therefore, if you find this book too advanced, I encourage you to read Learning Python as a prelude to this text, and return here for the rest of the story once you've mastered the basics. Unless you already have substantial programming experience, this edition might serve you best as a second Python text.

It Covers New Topics

Most of the changes in this edition were made to accommodate new topics. There are new chapters and sections on Internet scripting, CGI scripts, operating system interfaces, the SWIG integration code generator, advanced Tkinter topics, the HTMLgen web page generator, JPython, threads, restricted execution mode, and more. You should consult the Table of Contents for the full scoop, but here are some of the new topics and structural changes you'll find in this edition:

Topics

The Internet, systems programming, Tkinter GUIs, and C integration domains get much more attention, and are arguably now the main focus of this text. For instance, you'll find six new chapters on Internet scripting, covering client-side tools, server-side scripts and web sites, and advanced Internet topics and systems. Four new chapters address systems topics: threads, directory processing, program launching, and so on. And the GUI material has also grown from one chapter to a much more complete four-chapter presentation, and now covers all widgets (including text and canvas), as well as new grid, menu, and dialog support.

C integration

The C extending and embedding chapters have been expanded to cover new topics such as SWIG (the way to mix Python with C/C++ libraries today) and present new mixed-mode examples such as callback dispatch (extending plus embedding). C integration is at the heart of many Python systems, but the examples in this domain are inevitably complex, and involve large C programs that are only useful to C users. In deference to readers who don't need to code C integrations, this material is now isolated at the end of the text. Some of the C code listings are gone as well -- to reduce page count, I have opted instead to point readers to C source files on the enclosed CD-ROM where possible (see http://examples.oreilly.com/python2).

Although later chapters build upon material in earlier chapters, topics in this edition are covered fairly independently, and are associated by book parts. Because of that, it's not too much of a stretch to consider this edition to be akin to four or five books in one. Its top-level structure underscores its application topics focus:

Two notes here: First of all, don't let these titles fool you -- although most have to do with application topics, Python language features and general design concepts are still explored along the way, in the context of real-world goals. Second, readers who use Python as a standalone tool can safely skip the integration chapters, though I still recommend a quick glance. C programming isn't nearly as fun or easy as Python programming. Yet because integration is central to Python's role as a scripting tool, a cursory understanding can be useful, regardless of whether you do integrating, scripting, or both.

First edition readers will notice that most of this material is new, and even chapters with old titles contain largely new material. Noticeably absent in this edition are the original Sneak Preview, Mini Reference, Tutorial Appendix, and all of the old Part II -- a reflection of the new focus and intended readership.

It's More Example-Oriented

This book is largely about its examples. In this edition, old examples have been extended to become more realistic (e.g., PyForm and PyCalc), and new examples have been added throughout. Among the major examples, you'll find:

PyEdit

A Python/Tk text file editor object and program

PyView

A photo image and note-file slideshow

PyDraw

A paint program for drawing and moving image objects

PyTree

A tree data structure drawing program

PyClock

A Python/Tk analog and digital clock widget

PyToe

An AI-powered graphical tic-tac-toe program

PyForm

A persistent object table browser

PyCalc

A calculator widget in Python/Tk

PyMail

A Python/Tk POP and SMTP email client

PyFtp

A simple Python/Tk file-transfer GUI

PyErrata

A web-based error report system

PyMailCgi

A web-based email interface

There are also new mixed-mode C integration examples (e.g., callback registration and class object processing), SWIG examples (with and without "shadow" classes for C++), more Internet examples (FTP upload and download scripts, NNTP and HTTP examples, email tools, and new socket and select module examples), many new examples of Python threads, and new coverage of JPython, HTMLgen, Zope, Active Scripting, COM, and Python database interfaces. Many of the new examples are somewhat advanced, but of course this is now a somewhat advanced text.

In addition, the old Python/C embedding API (now called ppembed ) has been extended to support precompiling strings to bytecode, and the original calculator example (now called PyCalc) has been beefed up to support keyboard entry, history lists, colors, and more.

In fact, the new book examples tree distributed on this edition's CD-ROM (see http://examples.oreilly.com/python2) is itself a fairly sophisticated Python software system, and the examples within it have been upgraded structurally in a number of important ways:

Examples tree

The entire examples distribution has been organized as one big Python module package to facilitate cross-directory imports and avoid name-clashes with other Python code installed on your computer. Using directory paths in import statements (instead of a complex PYTHONPATH) also tends to make it easier to tell where modules come from. Moreover, you now need to add only one directory to your PYTHONPATH search-path setting for the entire book examples tree: the directory containing the PP2E examples root directory. To reuse code in this book within your own applications, simply import through the PP2E package root (e.g., from PP2E.Launcher import which).

Example filenames

Module names are generally much less cryptic now. I punted on 8.3 DOS compatibility long ago, and use more descriptive filenames. I've also fixed some old all-uppercase filenames, which were a last vestige of MS-DOS.

Example titles

Labels of example listings now give the full directory pathname of the example's source file to help you locate it in the examples distribution. For instance, an example source-code file whose name is given as Example N-M: PP2E\Internet\Ftp\sousa.py refers to the file sousa.py in the PP2E\Internet\Ftp subdirectory of the examples distribution directory.[4]

Example command lines

Similarly, when a command line is shown typed after a prompt such as C:\...\PP2E\System\Streams>, for example, it is really to be typed in the PP2E\System\Streams subdirectory in your examples tree. Unix and Linux users: please think / when you see \ in filename paths (my official excuse for which is outlined in the next section).

Example launchers

Because it's just plain fun to click on things right away, there are new self-configuring demo launcher programs (described later in this preface in Section P.5.1), to give you a quick look at Python scripts in action with minimal configuration requirements. You can generally run them straight off the book's CD without setting any shell variables first.

It's More Platform-Neutral

Except for some C integration examples, the majority of the programs in this edition were developed on my Windows 98 laptop, with an eye toward portability to Linux and other platforms. In fact, some of the examples were born of my desire to provide portable Python equivalents of tools missing on Windows (e.g., file splitters). When programs are shown in action, it's usually on Windows; they are demonstrated on the Red Hat Linux 6.x platform only if they exercise Unix-specific interfaces.

This is not a political statement at all -- I like Linux too. It's mostly a function of the fact that I wrote this book with MS Word; when time is tight, it's more convenient to run scripts on the same platform as your publishing tools than to frequently reboot into Linux. Luckily, because Python has now become so portable to both Windows and Linux, the underlying operating system is less of a concern to Python developers than it once was. Python, its libraries, and its Tkinter GUI framework all work extremely well on both platforms today.

Because I'm not a politician, though, I've tried to make the examples as platform-neutral as possible, and point out platform-specific issues along the way. Generally speaking, most of the scripts should work on common Python platforms unchanged. For instance, all the GUI examples were tested on both Windows (98, 95) and Linux (KDE, Gnome), and most of the command-line and thread examples were developed on Windows but work on Linux too. Because Python's system interfaces are generally built to be portable, this is easier than it may sound.

On the other hand, this book does delve into platform-specific topics where appropriate. There is new coverage of many Windows-specific topics -- Active Scripting, COM, program launch options, and so on. Linux and Unix readers will also find material geared towards their platforms -- forks, pipes, and the like. There is also new discussion of ways to edit and run Python programs on most major platforms.

The one place where readers may still catch a glimpse of platform biases is in the Python/C integration examples. For simplicity, the C compilation details covered in this text are still somewhat Unix/Linux-biased. One can at least make a reasonable case for such a focus -- not only does Linux come with C compilers for free, but its development environment grew up around that language. On Windows, the C extension code shown in this book will work, but you may need to use different build procedures that vary per Windows compiler. O'Reilly has published an outstanding text, Python Programming on Win32, that covers Windows-specific Python topics like this, and should help address some of the disparity here. If you do Windows-specific programming, please see that book for all Windows details skipped here.

But It's Still Not a Reference Manual

Please note that this edition, like the first, is still more of a tutorial than a reference manual (despite sharing a title pattern with a popular Perl reference text). This book aims to teach, not document. You can use its table of contents and index to track down specifics, and the new structure helps make this easy to do. But this edition is still designed to be used in conjunction with, rather than to replace, Python reference manuals. Because Python's manuals are free, well-written, available online, and change frequently, it would be folly to devote space to parroting their contents. For an exhaustive list of all tools available in the Python system, consult other books (e.g., O'Reilly's Python Pocket Reference) or the standard manuals at Python's web site and on this book's CD-ROM (see http://examples.oreilly.com/python2).

Using the Examples and Demos

I want to briefly describe how to use the book's examples here. In general, though, please see the following text files in the examples distribution directory for more details:

·         README-root.txt: package structure notes

·         PP2E\README-PP2E.txt: general usage notes

·         PP2E\Config\setup-pp.bat: Windows configuration

·         PP2E\Config\setup-pp.csh: Unix and Linux configuration

Of these, the README-PP2E.txt file is the most informative, and the PP2E\Config directory contains all configuration file examples. I give an overview here, but the files listed give a complete description.

The Short Story

If you want to see some Python examples right away, do this:

1.      Install Python from the book's CD-ROM (see http://examples.oreilly.com/python2), unless it is already installed on your computer. On Windows, click on the name of the self-installer program on the CD and do a default install (say "yes" or "next" to every prompt). On other systems, see the README file (the gzipped source distribution on the CD can be used to build Python locally).

2.      Start one of the following self-configuring scripts located in the top-level Examples\PP2E directory on the CD (see http://examples.oreilly.com/python2). Either click on their icons in your file explorer, or run them from your system prompt (e.g., DOS console box, Linux Xterm) using command lines of the form python script-name (you may need to use the full path to python if it's not on your system):

o        Launch_PyDemos.pyw: the main Python/Tk demo launcher toolbar

o        Launch_PyGadgets_bar.pyw: a Python/Tk utilities launcher bar

o        Launch_PyGadgets.py: starts standard Python/Tk utilities

o        LaunchBrowser.py: opens web examples index in web browser

The Launch_* scripts start Python programs portably[5] and require only that Python be installed -- you don't need to set environment variables first or tweak the included PP2E\Config setup files to run them. LaunchBrowser will work if it can find a web browser on your machine, even if you don't have an Internet link (though some Internet examples won't work completely without a live link).

If installing Python isn't an option, you can still run a few Python web demos by visiting http://starship.python.net/~lutz/PyInternetDemos.html with your browser. Because these examples execute scripts on a server, they tend to work best when run live from this site, rather than from the book's CD.

The Details

To help organize the new examples, I've provided a demo launcher program, PyDemos.pyw, in the top-level PP2E directory of the examples distribution. Figure P-1 shows PyDemos in action on Windows after pressing a few buttons. The launcher bar appears on the left of the screen; with it, you can run most of the major graphical examples in the book with a mouse click. The demo launcher bar can also be used to start major Internet book examples if a browser can be located on your machine (see the following launcher description).

Besides launching demos, the PyDemos source code provides pointers to major examples in the distribution; see its source code for details. You'll also find Linux automated build scripts for the Python/C integration examples in the top-level examples directory, which serve as indexes to major C examples.

Figure P-1. The PyDemos launcher with pop-ups and demos (Guido's photo reprinted with permission from Dr. Dobb's Journal)

figs/ppy2_ap01.gif

I've also included a top-level program called PyGadgets.py, and its relative PyGadgets_bar.pyw, to launch some of the more useful GUI book examples for real use instead of demonstration (mostly, the programs I use often; configure as desired). Figure P-2 shows what PyGadgets_bar looks like on Windows, along with a few of the utilities that its buttons can launch. All of the programs are presented in this book and included in the examples distribution. Most gadgets require a Python with Tkinter support, but that is the default configuration for the standard Windows port on the book's CD (see http://examples.oreilly.com/python2).

Figure P-2. The PyGadgets utilities launcher bar, with gadgets

figs/ppy2_ap02.gif

To run the files listed in the prior paragraph directly, you'll need to set up your Python module search path (see the top-level PP2E/Config/setup* files for hints). But if you want to run a collection of Python demos from the book and don't want to bother with setting up your environment first, simply run the self-launching utility scripts in the PP2E directory instead: Launch_PyDemos.pyw, Launch_PyGadgets.py, and Launch_PyGadgets_bar.pyw.

These Python-coded launcher scripts assume Python has already been installed, but will automatically find your Python executable and the book examples distribution, and set up your Python module and system search paths as needed to run the demos. You can probably run these launch scripts by simply clicking on their names in a file explorer, and you should also be able to run them directly from the book's CD-ROM (see http://examples.oreilly.com/python2). See the comments at the top of Launcher.py for more details (or read about these scripts in Chapter 4 ).

Many of the browser-based Internet examples from the book can also be found online at http://starship.python.net/~lutz/PyInternetDemos.html, where you can test-drive a few Python Internet scripting examples. Because these examples run in your web browser, they can be tested even if you haven't installed Python (or Python's Tk support) on your machine.

The PyDemos program also attempts to launch a web browser on the major example web pages by starting the LaunchBrowser.py script in the examples root directory. That script tries to find a usable browser on your machine, with generally good results; see the script for more details if it fails. Provided LaunchBrowser can find a browser on your machine, some demo buttons will pop up web pages automatically, whether you have a live Internet connection or not (if not, you'll see local files in your browser). Figure P-3 shows what the PyInternetDemos page looks like under Internet Explorer on Windows.

Figure P-3. The PyInternetDemos web page

figs/ppy2_ap03.gif

Of special interest, the getfile.html link on this page allows you to view the source code of any other file on the book's site -- HTML code, Python CGI scripts, and so on; see Chapter 12 for details. To summarize, here is what you'll find in the top-level PP2E directory of the book's examples distribution:

PyDemos.pyw

Button bar for starting major GUI and Internet examples

PyGadgets.py

Starts programs in non-demo mode for regular use

PyGadgets_bar.pyw

Button bar for starting PyGadgets on demand

Launch_*.py*

Starts PyDemos and PyGadgets programs using Launcher.py to autoconfigure search paths (run these for a quick look)

Launcher.py

Used to start programs without environment settings -- finds Python, sets PYTHONPATH, spawns Python programs

LaunchBrowser.py

Opens example web pages with an automatically located web browser, either live off the Net or by opening local web page files; if started directly, opens the PyInternetDemos index page

There are also subdirectories for examples from each major topic area of the book.

In addition, the top-level PP2E\PyTools directory contains Python-coded command-line utilities for converting line-feeds in all example text files to DOS or Unix format (useful if they look odd in your text editor), making all example files writable (useful if you drag-and-drop off the book's CD (see http://examples.oreilly.com/python2), deleting old .pyc bytecode files in the tree, and more. Again, see the distribution's README-PP2E.txt file for more details on all example issues.

Where It's At

The book examples distribution can be found on the CD-ROM that accompanies this book. See the CD's top-level README file for usage details, or browse the CD's example root directory in your favorite file explorer for a quick tour.

In addition to the book examples, the CD also contains various Python-related packages, including a full Windows self-installer program with Python and Tk support (double-click and say "yes" at all prompts to install), the full Python source code distribution (unpack and compile on your machine), and Python's standard documentation set in HTML form (click to view in your web browser).

Extra open source packages such as the latest releases (at the time of publication) of the SWIG code generator and JPython are also included, but you can always find up-to-date releases of Python and other packages at Python's web site, http://www.python.org.

Conventions Used in This Book

The following font conventions are used in this book:

Italic

Used for file and directory names, commands, to emphasize new terms when first introduced, and for some comments within code sections

Constant width

Used for code listings and to designate modules, methods, options, classes, functions, statements, programs, objects, and HTML tags

Constant width bold

Used in code sections to show user input

Constant width italic

Used to mark replaceables

This icon designates a note related to the nearby text.

This icon designates a warning related to the nearby text.

Where to Look for Updates

As before, updates, corrections, and supplements for this book will be maintained at the author's web site, http://www.rmi.net/~lutz. Look for the second edition's link on that page for all supplemental information related to this version of the book. As for the first edition, I will also be maintaining a log on this web site of Python changes over time, which you should consider a supplement to this text.

Beginning with this edition, I am making available a user-driven book errata reporting system on the World Wide Web, at this site:

http://starship.python.net/~lutz/PyErrata/pyerrata.html

There, you'll find forms for submitting book problem reports and comments, as well as viewing the report database by various sort keys. Reports are stored in a publicly browsable database by default, but an option lets you email them privately instead. The PyErrata system also happens to be written in Python, and is an example presented and shipped with this book; see Chapter 14. Figure P-4 shows what the root page of PyErrata looks like.

Figure P-4. The PyErrata book updates site

figs/ppy2_ap04.gif

If any of these addresses stop working over time, these pages might also be accessible from O'Reilly's web site, http://www.oreilly.com.[6] I'm still happy to receive direct emails from readers, of course, but the PyErrata site will hopefully streamline the report-posting process.

Contacting O'Reilly

You can also address comments and questions about this book to the publisher:

O'Reilly & Associates, Inc.

101 Morris Street

Sebastopol, CA 95472

(800) 998-9938 (in the United States or Canada)

(707) 829-0515 (international/local)

(707) 829-0104 (fax)

O'Reilly has a web page for this book, which lists errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/python2/

To comment or ask technical questions about this book, send email to:

bookquestions@oreilly.com

For more information about books, conferences, software, Resource Centers, and the O'Reilly Network, see the O'Reilly web site at:

http://www.oreilly.com

Acknowledgments

In addition to the people I mentioned in the first edition's preface, I'd like to extend appreciation to a few of the people who helped in some way during this second edition project:

·         To this book's first editor, Frank Willison, for overseeing this update, as well as championing the Python cause at O'Reilly and beyond. To this book's later editor, Laura Lewin, for picking up the ball and lighting necessary fires.

·         To Python creator Guido van Rossum for making this stuff fun again.

·         To the people who took part in a review of an early draft of this edition: Eric Raymond, Mark Hammond, David Ascher, Tim Peters, and Dave Beazley.

·         To Tim O'Reilly and the staff of O'Reilly & Associates, both for producing this book, and supporting open source software in general.

·         To the Python community at large, for diligence, hard work, and humor -- both in the early years and today. We've come far, but to steal a line from the 1970s: You aint' seen nothin' yet.

·         And to the students of the many Python classes I have taught, as well as the scores of readers who took the time to send me comments about the first edition; your opinions helped shape this update.

Finally, a few personal notes of thanks. To my children, Michael, Samantha, and Roxanne, for purpose. If they are at all representative of their generation, the future of our species seems in very good hands. You'll have to pardon me if that sounds proud; with kids like mine, it's impossible to feel otherwise.

And most of all to Lisa, the mother of those amazing kids. I owe her my largest debt of gratitude, for everything from enduring my flights from reality while writing books like this, to keeping me out of jail in our youth. No matter what the future may hold, I'll always be glad that something threw us together two decades ago.

Mark Lutz

November 2000

Somewhere in Colorado

"When Billy Goes Down, He's Going Down Fast"

The last five years have also been host to the rise of the open source movement. Open source refers to software that is distributed free of charge with full source code, and is usually the product of many developers working in a loosely knit collaborative fashion. Python, the Linux operating system, and many other tools such as Perl and the Apache web server fall into this category. Partly because of its challenge to the dominance of mega-companies, the open source movement has quickly spread through society in profound ways.

Let me tell you about an event that recently underscored the scope of this movement's impact on me. To understand this story, you first need to know that as I was writing this book, I lived in a small town in Colorado not generally known for being on the cutting edge of technological innovation. To put that more colorfully, it's the sort of place that is sometimes called a "cowboy town."

I was at a small local bookstore hunting for the latest Linux Journal. After browsing for a while, I found a copy and walked it to the checkout. Behind the counter were two clerks who looked as if they might be more at home at a rodeo than behind the counter of this establishment. The older of the two sported gray hair, a moustache, and the well-worn skin of a person accustomed to life on a ranch. Both wore obligatory baseball caps. Cowboys, to be sure.

As I put the magazine down, the elder clerk looked up for a moment, and said, in classic cowboy drawl, "Linux, huh? I tell you what, when Billy goes down, he's goin' down fast!" Of course, this was in reference to the widely publicized competition between Linux and Bill Gates' Microsoft Windows, spurred by the open source movement.

Now, in another time and place, these two might have instead been discussing livestock and firearms over strong cups of coffee. Yet somehow, somewhere, they had become passionate advocates of the Linux open source operating system. After collecting my chin from the floor, we wound up having a lively discussion about Linux, Microsoft, Python, and all things open. You might even say we had a good-old time.

I'm not trying to express a preference for one operating system over another here; both have merits, and Python runs equally well on either platform (indeed, this book's examples were developed on both systems). But I am amazed that an idea that software developers often take for granted has had such a deep, mainstream impact. That seems a very hopeful thing to me; if technology is to truly improve the quality of life in the next millennium, we need all the cowboys we can get.

[1] As I was writing this book, Linux Journal also published a special Python supplement with their May 2000 edition -- the cover of which, of course, featured a naked man seated outdoors in front of a computer desk instead of a piano. If you don't know why that is funny, you need to watch a few reruns from Python's namesake, the Monty Python television series (consider it a first suggested exercise). I'll say more about the implications of Python's name in the first chapter. [back]

[2] See http://www.python.org for more details. Some companies don't disclose their Python use for competitive reasons, though many eventually become known when one of their web pages crashes and displays a Python error message in a browser. Hewlett Packard is generally counted among companies thus "outed." [back]

[4] The "examples distribution directory" is the directory containing the top-level PP2E directory of the book examples tree. On the CD (see http://examples.oreilly.com/python2), it's the topmost Examples directory; if you've copied the examples to your machine, it's wherever you copied (or unpacked) the PP2E root directory. You can run most of the examples from the CD directly, but you'll want to copy them to your hard drive to make changes, and to allow Python to save .pyc compiled bytecode files for quicker startups. [back]

[5] All the demo and launcher scripts are written portably but are known to work only on Windows 95/98 and Linux at the time of this writing; they may require minor changes on other platforms. Apologies if you're using a platform that I could not test: Tk runs on Windows, X11, and Macs; Python itself runs on everything from handheld PDAs to mainframes; and my advance for writing this book wasn't as big as you may think. [back]

[6] O'Reilly has an errata report system at their site too, and you should consider the union of these two lists to be the official word on book bugs and updates. [back]

TOC  Chapter 1







??????????????@Mail.ru