Better errors for local name oddities

When a local variable is known to the compiler but undefined when used, a new exception UnboundLocalError is raised. This is a class derived from NameError, so code that catches NameError should still work. The purpose is to provide better diagnostics in the following example This used to raise a confusing NameError on the print statement. A.2.2.7 Membership operator overloading You can now override the in operator by defining a _contains_ _ method. Note that it has its arguments backward x in...

A brief introduction to COM

COM is a Microsoft technology for language-neutral component integration. It is sometimes marketed as ActiveX, partially derived from a system called OLE, and is the technological heart of the Active Scripting system we met earlier.121 COM also sports a distributed extension known as dCom that allows communicating objects to be run on remote machines. Implementing DCOM often simply involves running through Windows registry configuration steps to associate servers with machines on which they...

Class exceptions

Exceptions may now take the fo rm of class (and class instance) objects. The intent is to support exception categories. Because an except clause will now match a raised exception if it names the raised class or any of its superclasses, specifying superclasses allows try statements to catch broad categories without listing all members explicitly (e.g., catching a numeric-error superclass exception will also catch specific kinds of numeric errors). Python's standard built-in exceptions are now...

Core Language Changes

The following sections describe changes made to the Python language itself. A.1.1.1 Augmented assignment After nearly a decade of complaints from C programmers, Guido broke down and added 11 new C-like assignment operators to the language + - * ** < < > > & A The statement a + b is similar to a a + b except that a is evaluated only once (useful if it is a complex expression). If a is a mutable object, it may be modified in place for instance, if it is a list, a + b has the same...

Extended print statement

The print statement now has an option that makes the output go to a different file than the default sys.stdout. For instance, to write an error message to sys.stderr, you can now write print > > sys.stderr, spam As a special case, if the expression used to indicate the file evaluates to None, the current value of sys.stdout is used (like not using > > at all). Note that you can always write to file objects such as sys.stderr by calling their write method this optional extension simply...

File Pattern Search Utility

The next script searches for patterns in a set of files, much like the grep command-line program. We wrote file and directory searchers earlier, in Chapter 5. Here, the file searches look for patterns instead of simple strings (see Example 18-8). The patterns are typed interactively separated by a space, and the files to be searched are specified by an input pattern for Python's glob.glob filename expansion tool we studied earlier, too. usr local bin python import sys, re, glob from string...

House upon the Sand

As you read the next three chapters of this book, please keep in mind that they are intended only as an introduction to server-side scripting with Python. The webmaster domain is large and complex, changes continuously, and often prescribes many ways to accomplish a given goal -- some of which can vary from browser to browser and server to server. For instance, the password encryption scheme of the next chapter may be unnecessary under certain scenarios, and special HTML tags may sometimes...

A IDLE an integrated development environment GUI

Python now ships with a point-and-click development interface named IDLE. Written in Python using the Tkinter GUI library, IDLE either comes in the source library's Tools directory or is automatically installed with Python itself (on Windows, see IDLE's entry in the Python menu within your Start button menus). IDLE offers a syntax-coloring text editor, a graphical debugger, an object browser, and more. If you have Python with Tk support enabled and are accustomed to more advanced development...

Little C Class But Not Too Much

To see how this all works, we need a C++ class. To illustrate, let's code a simple one to be used in Python scripts.171 The following C++ files define a Number class with three methods (add, sub, display), a data member (data), and a constructor and destructor. Example 19-19 shows the header file. 7 For a more direct comparison, you could translate the stack type in Example 19-15 to a C++ class too, but that yields much more C++ code than I care to show in this Python book. Moreover, such a...

Morality Tale of Perl Versus Python

(The following was posted recently to the rec.humor.funny Usenet newsgroup by Larry Hastings, and is reprinted here with the original author's permission. I don't necessarily condone language wars okay ) This has been percolating in the back of my mind for a while. It's a scene from The Empire Strikes Back, reinterpreted to serve a valuable moral lesson for aspiring programmers. With Yoda strapped to his back, Luke climbs up one of the many thick vines that grow in the swamp until he reaches...

New internal regular expression engine

The new regular expression engine, SRE, is fully backward-compatible with the old engine, and is invoked using the same interface (the re module). That is, the re module's interface remains the way to write matches, and is unchanged it is simply implemented to use SRE. You can explicitly invoke the old engine by importing pre, or the SRE engine by importing sre. SRE is faster than pre, and supports Unicode (which was the main reason to develop yet another underlying regular expression engine).

New list methods

List objects have a new method, pop, to fetch and delete the last item of the list is the same as the two statements x s -1 del s -1 and extend, to concatenate a list of items on the end, in place The pop method can also be passed an index to delete (it defaults to -1). Unlike append, extend is passed an entire list and adds each of its items at the end.

Portable Program Launch Framework

With all these different ways to start programs on different platforms, it can be difficult to remember what tools to use in a given situation. Moreover, some of these tools are called in ways that are complicated enough to easily forget (for me, at least). I write scripts that need to launch Python programs often enough that I eventually wrote a module to try and hide most of the underlying details. While I was at it, I made this module smart enough to automatically pick a launch scheme based...

Python multimedia extravaganza

I mentioned earlier that browsers are a cheap way to present multimedia. Alas, this sort of thing is best viewed live, so the best I can do is show startup commands here. The next command line and function call, for example, display two GIF images in Internet Explorer on my machine (be sure to use full local pathnames). The result of the first of these is captured in Figure 4-4. C PP2E> python LaunchBrowser.py > > > from LaunchBrowser import launchBrowser umberjack.gif') Starting

Python Tree Copy Script

My CD writer sometimes does weird things. In fact, copies of files with odd names can be totally botched on the CD, even though other files show up in one piece. That's not necessarily a show-stopper -- if just a few files are trashed in a big CD backup copy, I can always copy the offending files to floppies one at a time. Unfortunately, Windows drag-and-drop copies don't play nicely with such a CD the copy operation stops and exits the moment the first bad file is encountered. You only get as...

Python Tree Searcher

To help ease the task of performing global searches on all platforms I might ever use, I coded a Python script to do most of the work for me. Example 5-10 employs standard Python tools we met in the preceding chapters os.path.walk to visit files in a directory sting.find to search for a string in a text read from a file os.path.splitext to skip over files with binary-type extensions os.path.join to portably combine a directory path and filename os.path.isdir to skip paths that refer to...

PythonC Integration API Changes

Portability was ensured to 64-bit platforms under both Linux and Win64, especially for the new Intel Itanium processor. Large file support was also added for Linux64 and Win64. The garbage collection changes resulted in the creation of two new slots on an object, tp_traverse and tp_ciear. The augmented assignment changes result in the creation of a new slot for each in-place operator. The GC API creates new requirements for container types implemented in C extension modules. See Include...

Quick Geometry Lesson

Before I show you PyClock, though, a little background and a confession. Quick -how do you plot points on a circle This, along with time formats and events, turns out to be a core concept in clock widget programs. To draw an analog clock face on a canvas widget, you essentially need to be able to sketch a circle -- the clock face itself is composed of points on a circle, and the second, minute, and hour hands of the clock are really just lines from a circle's center out to a point on the...

Quick Introduction to JPython

Functionally speaking, JPython is a collection of Java classes that run Python code. It consists of a Python compiler, written in Java, that translates Python scripts to Java bytecodes so they can be executed by a Java virtual machine -- the runtime component that executes Java programs and is used by major web browsers. Moreover, JPython automatically exposes all Java class libraries for use in Python scripts. In a nutshell, here's what comes with the JPython system JPython always compiles...

A raise forms for class exceptions

Because exceptions can now either be string objects or classes and class instances, you can use any of the following raise statement forms raise string matches except with same string object raise string, data same, with optional data raise class, instance matches except with class or its superclass raise instance same as raise instance.__class__ , instance You can also use the following three forms, which are for backwards-compatibility with earlier releases where all built-in exceptions were...

A rand module replaced by random module

The rand module is now deprecated use random instead. A.3.2.9 Assorted Tkinter changes Tkinter became portable to and sprouted native look-and-feel for all major platforms (Windows, X, Macs). There has been a variety of changes in the Tkinter GUI interface The_call__method for StringVar class objects was dropped in Python 1.4 that means you need to explicitly call their get ( ) set( ) methods, instead of calling them with or without arguments. The ScrolledText widget went through a minor...

Regression Test Script

As we've seen, Python provides interfaces to a variety of system services, along with tools for adding others. Example 4-5 shows some commonly used services in action. It implements a simple regression-test system, by running a command-line program with a set of given input files and comparing the output of each run to the prior run's results. This script was adapted from an automated testing system I wrote to catch errors introduced by changes in program source files in a big system, you might...

Selected Library Module Changes

This section lists some of the changes made to the Python standard library. distutils New tools for distributing Python modules. zipfile New read and write zip archives (module gzip does gzip files). unicodedata New access to the Unicode 3.0 database. _winreg New Windows registry access (one without the _ is in progress). socket , httplib , urllib Expanded to include optional OpenSSL secure socket support (on Unix only). _tkinter Support for Tk versions 8.0 through 8.3. string This module no...

A short ASP example

We can't discuss ASP in any real detail here, but here's an example of what an ASP file looks like when Python code is embedded < SCRIPT RunAt Server Language Python> code here is run at the server < SCRIPT> < BODY> < HTML> As before, code may be embedded inside script tag pairs. This time, we tell ASP to run the code at the server with the RunAt option if omitted, the code and its tags are passed through to the client and run by IE (if configured properly). ASP also recognizes...

Simple C Extension Module

At least that's the short story we need to turn to some code to make this more concrete. C types generally export a C module with a constructor function. Because of that (and because they are simpler), let's start off by studying the basics of C module coding with a quick example. When you add new or existing C components to Python, you need to code an interface (or glue) logic layer in C that handles cross-language dispatching and data translation. The C source file in Example 19-1 shows how...

Simple Grail Applet Example

Writing Grail applets is remarkably straightforward. In fact, applets are really just Python Tkinter programs with a few exceptions, they don't need to know about Grail at all. Let's look at a short example the code in Example 15-6 simply adds a button to the browser, which changes its appearance each time it's pressed (its bitmap is reconfigured in the button callback handler). There are two components to this page definition an HTML file and the Python applet code it references. As usual, the...

Simple JPython Example

Once a Python program is compiled with JPython, it is all Java the program is translated to Java bytecodes, it uses Java classes to do its work, and there is no Python left except for the original source code. Because the compiler tool itself is also written in Java, JPython is sometimes called 100 pure Java. That label may be more profound to marketeers than programmers, though, because JPython scripts are still written using standard Python syntax. For instance, Example 15-4 is a legal...

Simple Python File Server

Let's conclude this chapter by putting some of these socket ideas to work in something a bit more useful than echoing text back and forth. Example 10-10 implements both the server-side and client-side logic needed to ship a requested file from server to client machines over a raw socket. In effect, this script implements a simple file download system. One instance of the script is run on the machine where downloadable files live (the server), and another on...

Simple Python File Server Tune in Log on and Drop out

In the last few years, the Internet has virtually exploded onto the mainstream stage. It has rapidly grown from a simple communication device used primarily by academics and researchers into a medium that is now nearly as pervasive as the television and telephone. Social observers have likened the Internet's cultural impact to that of the printing press, and technical observers have suggested that all new software development of interest occurs only on the Internet. Naturally, time will be the...

Stack Class

Perhaps the biggest drawback of the module-based stack is that it supports only a single stack object. All clients of the stack module effectively share the same stack. Sometimes we want this feature a stack can serve as a shared-memory object for multiple modules. But to implement a true stack datatype, we need to use classes. To illustrate, let's define a full-featured stack class. The stack class shown in Example 17-2 defines a new datatype, with a variety of behavior. Like the module, the...

The Great Grand Renaming is complete

All exposed Python symbols now start with a Py prefix. A.3.4.4 Threading support, multiple interpreters A handful of new API tools provide better support for threads when embedding Python. For instance, there are tools for finalizing Python (Py_Finalize) and for creating multiple interpreters (Py_NewInterpreter). Note that spawning Python language threads may be a viable alternative to C-level threads, and multiple namespaces are often sufficient to isolate names used in independent system...

Third CGI script

Each row in the table displays a row.column pair, as generated by the executing Python script. If you're curious how the generated HTML looks, select your browser's View Source option after you've accessed this page. It's a single HTML page composed of the HTML generated by the first print in the script, then the for loops, and finally the last print. In other words, the concatenation of this script's output is an HTML document with headers.

Unicode strings

Python now supports Unicode (i.e., 16-bit wide character) strings. Release 1.6 added a new fundamental datatype (the Unicode string), a new built-in function unicode, and numerous C APIs to deal with Unicode and encodings. Unicode string constants are prefixed with the letter u, much like raw strings (e.g., u ). See the file Misc unicode.txt in your Python distribution for details, or visit web site

Windows Changes

New popen2, popen3, and popen4 calls were added in the os module. The os.popen call is now much more usable on Windows 95 and 98. To fix this call for Windows 9x, Python internally uses the w9xpopen.exe program in the root of your Python installation (it is not a standalone program). See Microsoft Knowledge Base article Q150956 for more details. Administrator privileges are no longer required to install Python on Windows NT or Windows 2000. The Windows installer also now installs by default in...

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...

Active Scripting basics

Unfortunately, embedding Python in client-side HTML works only on machines where Python is installed and Internet Explorer is configured to know about the Python language (by installing the win32all extension package discussed in a moment). Because of that, this technology doesn't apply to most of the browsers in cyberspace today. On the other hand, if you can configure the machines on which a system is to be delivered, this is a nonissue. Before we get into a Python example, let's look at the...

Active Scripting Client Side Embedding

Active Scripting is a technology that allows scripting languages to communicate with hosting applications. The hosting application provides an application-specific object model API, which exposes objects and functions for use in the scripting language programs. In one if its more common roles, Active Scripting provides support that allows scripting language code embedded in HTML pages to communicate with the local web browser through an automatically exposed object model API. Internet Explorer,...

Active Server Pages Server Side Embedding

Active Server Pages (ASPs) use a similar model Python code is embedded in the HTML that defines a web page. But ASP is a server-side technology embedded Python code runs on the server machine and uses an object-based API to dynamically generate portions of the HTML that is ultimately sent back to the clientside browser. As we saw in the last three chapters, Python server-side CGI scripts embed and generate HTML, and deal with raw inputs and output streams. By contrast, server-side ASP scripts...

Add Python Mix Well Repeat

In the last chapter, we explored half of the Python C integration picture -- calling C services from Python. This mode lets programmers speed up operations by moving them to C, and utilize external libraries by wrapping them in C extension modules and types. But the inverse can be just as useful -- calling Python from C. By delegating selected components of an application to embedded Python code, we can open them up to onsite changes without having to ship a system's code. This chapter tells...

Adding a User Interface Frontend

You might have noticed that we have been living in the realm of the command line for all of this chapter -- our socket clients and servers have been started from simple DOS or Linux shells. There is nothing stopping us from adding a nice point-and-click user interface to some of these scripts, though GUI and network scripting are not mutually exclusive techniques. In fact, they can be arguably sexy when used together well. For instance, it would be easy to implement a simple Tkinter GUI...

Adding Comparison Functions

Since functions can be passed in like any other object, we can easily allow for an optional comparison function. In the next version (Example 17-25), the second argument takes a function that should return true if its first argument should be placed before its second. A lambda is used to provide an ascending-order test by default. This sorter also returns a new sequence that is the same type as the sequence passed in, by applying the slicing techniques used in earlier sections if you sort a...

Adding GUI frontends to command lines

The callback actions named within the prior module's classes, though, should normally do something GUI-oriented. Because the original file packing and unpacking scripts live in the world of text-based streams, we need to code wrappers around them that accept input parameters from more GUI-minded users. The module in Example 9-7 uses the custom modal dialog techniques we studied in Chapter 7, to pop up an input display to collect pack script parameters. Its runPackDialog function is the actual...

Adding Multiple Widgets

It's time to start building user interfaces with more than one widget. Example 6-17 makes the window shown in Figure 6-14. Label(win, text 'Hello container world').pack(side TOP) Button(win, text 'Hello', command greeting).pack(side LEFT) Button(win, text 'Quit', command win.quit).pack(side RIGHT) This example makes a Frame widget (another Tkinter class), and attaches three other widget objects to it, a Label and two Buttons, by passing the Frame as their first argument. In Tkinter terms, we...

Adding new buttons in new components

One obvious way to reuse the calculator is to add additional expression feature buttons -- square roots, inverses, cubes, and the like. You can type such operations in the command-line pop-ups, but buttons are a bit more convenient. Such features could also be added to the main calculator implementation itself but since the set of features that will be useful may vary per user and application, a better approach may be to add them in separate extensions. For instance, the class in Example 18-18...

Adding Pictures and Generating Tables

Now let's get back to writing server-side code. As anyone who's ever surfed the Web knows, web pages usually consist of more than simple text. Example 12-5 is a Python CGI script that prints an < img> HTML tag in its output to produce a graphic image in the client browser. There's not much Python-specific about this example, but note that just as for simple HTML files, the image file (ppsmall.gif) lives on and is downloaded from the server machine when the browser interprets the output of...

Adding Relational Algebra to Sets CD

If you are interested in studying additional set-like operations coded in Python, see the following files on this book's CD (see PP2E Dstruct Basic rset.py RSet implementation PP2E Dstruct Basic reltest.py Test script for RSet The RSet subclass defined in rset.py adds basic relational algebra operations for sets of dictionaries. It assumes the items in sets are mappings (rows), with one entry per column (field). RSet inherits all the original Set operations (iteration, intersection, union, &...

Adding Text Editing Operations

Example 8-11 puts some of these concepts to work. It adds support for four common text-editing operations -- file save, text cut and paste, and string find searching -- by subclassing scolledText to provide additional buttons and methods. The Text widget comes with a set of default keyboard bindings that perform some common editing operations too, but they roughly mimic the Unix Emacs editor, and are somewhat obscure it's more common and user-friendly to provide GUI interfaces to editing...

Administrative Tools

Just in case those massive listings aren't quite enough to go on, let's experiment with some of the simpler os tools interactively. Like sys, the os module comes with a collection of informational and administrative tools > > > os.chdir(r'c temp') > > > os.getcwd( ) As shown here, the os.getpid function gives the calling process's process ID (a unique system-defined identifier for a running program), and os.getcwd returns the current working directory. The current working directory...

Advanced Text and Tag Operations

Besides position specifiers, text tags can also be used to apply formatting and behavior to both all characters in a substring, and all substrings added to a tag. In fact, this is where much of the power of the text widget lies Tags have formatting attributes for setting color, font, tabs, and line spacing and justification to apply these to many parts of the text at once, associate them with a tag and apply formatting to the tag with the tag_config method, much like the general widget config...

Alternative encryption schemes

To show how to write alternative encryptors and encoders, secret also includes a digits-string encoder and a character-code shuffling encryptor both are enabled with global flag variables at the top of the module If set to true, the encrypted password is encoded into a string of ASCII code digits separated by dashes. Defaults to false to fall back on URL and HTML escape encoding. If set to false and the encryptor is used on an even-numbered day of the week, the simple character-code encryptor...

Anatomy of a C Extension Module

Though simple, the hello.c example illustrates the structure common to all C modules. This structure can vary somewhat, but this file consists of fairly typical boilerplate code The C file first includes the standard Python.h header file (from the installed Python Include directory). This file defines almost every name exported by the Python API to C, and serves as a starting point for exploring the API itself. The file then defines a function to be called from the Python interpreter in...

Anatomy of a C Extension Type

Although most of file stacktyp.c is missing, there is enough here to illustrate the global structure common to C type implementations The file starts off by defining a C struct called stackobject that will be used to hold per-instance state information -- each generated instance object gets a newly malloc'd copy of the struct. It serves the same function as class instance attribute dictionaries, and contains data that was saved in global variables by the C stack module. As in the module, a set...

And Now for Something Completely Different

This book is about using Python, a very high-level, object-oriented, open source111 programming language, designed to optimize development speed. Although it is completely general-purpose, Python is often called an object-oriented scripting language, partly because of its sheer ease of use, and partly because it is commonly used to orchestrate or glue other software components in an application. 11 Open source systems are sometimes called freeware, in that their source code is freely...

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...

Anonymous Pipe Basics

Pipes come in two flavors -- anonymous and named. Named pipes (sometimes called fifos) are represented by a file on your computer. Anonymous pipes only exist within processes, though, and are typically used in conjunction with process forks as a way to link parent and spawned child processes within an application -- parent and child converse over shared pipe file descriptors. Because named pipes are really external files, the communicating processes need not be related at all (in fact, they can...

App The root class

The top of the hierarchy knows what it means to be a shell application, but not how to accomplish a particular utility task (those parts are filled in by subclasses). App, listed in Example 4-11, exports commonly used tools in a standard and simplified interface, and a customizable start run stop method protocol that abstracts script execution. It also turns application objects into file-like objects when an application reads itself, for instance, it really reads whatever source its standard...

Appendix A Recent Python Changes

This appendix summarizes prominent changes introduced in Python releases since the first edition of this book. It is divided into three sections, mostly because the sections on 1.6 and 2.0 changes were adapted from release note documents Changes introduced in Python 2.0 (and 2.1) Changes between the first edition and Python 1.5.2 Python 1.3 was the most recent release when the first edition was published (October 1996), and Python 1.6 and 2.0 were released just before this second edition was...

Appendix B Pragmatics

This appendix is a very brief introduction to some install-level details of Python use, and contains a list of Python Internet resources. More information on topics not covered fully here can be found at other resources For additional install details, consult the various README text files in the examples distribution on this book's CD (view CD-ROM content online at http examples.oreilly.com python2), as well as the README files and other documentation that accompany the Python distributions and...

Appendix C Python Versus C

This appendix briefly summarizes some of the differences between Python and C++ classes. Python's class system can be thought of as a subset of C++'s. Although the comparison to Modula 3 may be closer, C++ is the dominant OOP language today. But in Python, things are intentionally simpler -- classes are simply objects with attached attributes that may have links to other class objects. They support generation of multiple instances, customization by attribute inheritance, and operator...

Application Hierarchy Superclasses

This section lists the source code of StreamApp and App -- the classes that do all this extra work on behalf of PackApp and UnpackApp. We don't have space to go through all this code in detail, so be sure to study these listings on your own for more information. It's all straight Python code. I should also point out that the classes listed in this section are just the ones used by the object-oriented mutations of the textpack and textunpack scripts. They represent just one branch of an overall...

Artificial Complexities

With many traditional programming tools, you can easily lose the forest for the trees the act of programming becomes so complex that the real-world goal of the program is obscured. Traditional languages divert valuable attention to syntactic issues and development of bookkeeping code. Obviously, complexity isn't an end in itself it must be clearly warranted. Yet some of our current tools are so complex that the language itself makes the task harder and lengthens the development process.

Attaching widgets to frames

In larger terms, the critical innovation in this example is its use of frames Frame widgets are just containers for other widgets, and so give rise to the notion of GUIs as widget hierarchies, or trees. Here, win serves as an enclosing window for the other three widgets. In general, though, by attaching widgets to frames, and frames to other frames, we can build up arbitrary GUI layouts. Simply divide the user interface into a set of increasingly smaller rectangles, implement each as a Tkinter...

Automating installation steps

But wait -- why do things the hard way Before you start installing scripts by hand, remember that Python programs can usually do much of your work for you. It's easy to write Python scripts that automate some of the CGI installation steps using the operating systems tools that we met earlier in the book. For instance, while developing the examples in this chapter, I did all editing on my PC (it's generally more dependable than a telnet client). To install, I put all the examples in a tar file,...

Automating menu construction

Menus are a powerful Tkinter interface device. If you're like me, though, the examples in this section probably seem like a lot of work. Menu construction can be both code-intensive and error-prone if done by calling Tkinter methods directly. A better approach might automatically build and link up menus from a higher-level description of their contents. In fact, we will -- in Chapter 9, we'll meet a tool called GuiMixin that automates the menu construction process, given a data structure that...

Avoiding DOS consoles on Windows

Earlier in this book we learned that if a program's name ends in a .pyw extension instead of .py, the Windows Python port does not pop up a DOS console box to serve as its standard streams when the file is launched by clicking its filename icon. Now that we've finally started making windows of our own, that filename trick will start to become even more useful. If you just want to see the windows that your script makes no matter how it is launched, be sure to name your GUI scripts with a .pyw if...

Configuration Settings

The Examples PP2E Config directory on the CD (see http examples.oreilly.com python2) contains example configuration files with comments for Python variable settings. On Windows NT, you can set these variables in the system settings GUI (more on this in a minute) on Windows 98, you can set them from DOS batch files, which can be run from your C autoexec.bat file to make sure they are set every time you start your compute. For example, my autoexec file includes this line which in turn invokes a...

Configuring from a Program

In all cases, sys.path represents the search path to Python scripts and is initialized from path settings in your environment plus standard defaults. This is a normal Python list of strings that may be changed by Python programs to configure the search path dynamically. To extend your search path within Python, do this Because shell variable settings are available to Python programs in the built-in os.environ dictionary, a Python script may also say something like sys.path.append(os.environ...

Python Internet Resources

Finally, Table B-1 lists some of the most useful Internet sites for Python information and resources. Nearly all of these are accessible from Python's home page (http www.python.org) and most are prone to change over time, so be sure to consult Python's home page for up-to-date details. comp.lang.python (python-list cwi.nl) http www.rmi. net lutz about-pp2e.html

Running Python Programs

Python code can be typed at a > > > interactive prompt, run from a C program, or placed in text files and run. There is a variety of ways to run code in files Python files can always be run by typing a command of the form python file.py in your system shell or console box, as long as the Python interpreter program is on your system's search path. On Windows, you can type this command in an MS-DOS console box on Linux, use an xterm. Depending on your platform, you can usually start Python...

Running the Demo Launcher Scripts

The top level of the CD's Examples PP2E package (see http examples.oreilly.com python2) includes Python self-configuring scripts that can be run to launch major book examples, even if you do not configure your environment. That is, they should work even of you don't set your PATH or PYTHONPATH shell variables. These two scripts, PyDemos and PyGadgets, are presented in Chapter 8, and described more fully in both this book's Preface and the CD's README files (see http examples.oreilly.com...

The Book Examples Package

The Examples PP2E CD directory is a Python module package that contains source code files for all examples presented in this book (and more). The PP2E package in turn contains nested module packages that partition the example files into subdirectories by topic. You can either run files straight off the CD, or copy the PP2E directory onto your machine's hard drive (copying over allows you to change the files, and lets Python store their compiled bytecode for faster startups). Either way, the...

Unix and Linux

Python may already be available on these platforms (it's often installed as a standard part of Linux these days) check your usr bin and usr local bin directories to see if a Python interpreter is lurking there. If not, Python is generally installed on these platforms from either an rpm package (which installs Python executables and libraries automatically) or the source code distribution package (which you unpack and compile locally on your computer). Compiling Python from its source on Linux...

Back to the big Internet picture

So where are we at in the Internet abstraction model now Because mail is transferred over sockets (remember sockets ), they are at the root of all of this email fetching and sending. All email read and written ultimately consists of formatted bytes shipped over sockets between computers on the Net. As we've seen, though, the POP and SMTP interfaces in Python hide all the details. Moreover, the scripts we've begun writing even hide the Python interfaces and provide higher-level interactive...

Basic ospath Tools

The nested module os.path provides a large set of directory-related tools of its own. For example, it includes portable functions for tasks such as checking a file's type (isdir, isfile, and others), testing file existence (exists), and fetching the size of a file by name (getsize) > > > os.path.isdir(r'C temp'), os.path.isfile(r'C temp') > > > os.path.isdir(r'C config.sys'), os.path.isfile(r'C config.sys') > > > os.path.isdir('nonesuch'), os.path.isfile('nonesuch') > >...

Basic Patterns

To illustrate how to combine RE operators, let's start with a few short test files that match simple pattern forms. Comments in Example 18-3 describe the operations exercised check Table 18-1 to see which operators are used in these patterns. Example 18-3. PP2E lang re-basics.py literals, sets, ranges (all print 2 import re pattern, string A.C., xxABCDxx themself matchobj re.search(pattern, string) if matchobj object or None pattobj re.compile(A.*C.*) matchobj pattobj.search(xxABCDxx) obj print...

Batteries Included

This chapter and those that follow deal with both the Python language and its standard library. Although Python itself provides an easy-to-use scripting language, much of the action in real Python development involves the vast library of programming tools (some 200 modules at last count) that ship with the Python package. In fact, the standard libraries are so powerful that it is not uncommon to hear Python described by the term batteries included -- a phrase generally credited to Frank...

Bidirectional IPC with Pipes

Pipes normally only let data flow in one direction -- one side is input, one is output. What if you need your programs to talk back and forth, though For example, one program might send another a request for information, and then wait for that information to be sent back. A single pipe can't generally handle such bidirectional conversations, but two pipes can -- one pipe can be used to pass requests to a program, and another can be used to ship replies back to the requestor.16 S This really...

Big Gui A Client Demo Program

Let's look at a program that makes better use of the two automation classes we just wrote. In the module in Example 9-3, the Hello class inherits from both GuiMixin and GuiMaker. GuiMaker provides the link to the Frame widget, plus the menu toolbar construction logic. GuiMixin provides extra common-behavior methods. Really, Hello is another kind of extended Frame widget because it is derived from GuiMaker. To get a menu and toolbar for free, it simply follows the protocols defined by GuiMaker...

Binary data files

The preceding examples all process simple text files. On most platforms, Python scripts can also open and process files containing binary data -- JPEG images, audio clips, and anything else that can be stored in files. The primary difference in terms of code is the mode argument passed to the built-in open function Once you've opened binary files in this way, you may read and write their contents using the same methods just illustrated read, write, and so on. (readline and readlines don't make...

Binary file access

This code is careful to open both input and output files in binary mode (rb, wb), because it needs to portably handle things like executables and audio files, not just text. In Chapter 2, we learned that on Windows, text-mode files automatically map r n end-of-line sequences to n on input, and map n to r n on output. For true binary data, we really don't want any r characters in the data to go away when read, and we don't want any superfluous r characters to be added on output. Binary-mode...

Binary file mode revisited

Because this script aims to be portable to Windows, it also takes care to open files in binary mode, even though they contain text data. As we've seen, when files are opened in text mode on Windows, r is stripped from r n markers on input, and r is added before n markers on output. This automatic conversion allows scripts to represent the end-of-line marker as n on all platforms. Here, though, it would also mean that the script would never see the r it's looking for to detect a DOS-encoded line...

Binding Events

We met the bind widget method in the last chapter, when we used it to catch button presses in the tutorial. Because bind is commonly used in conjunction with other widgets (e.g., to catch return key presses for input boxes), we're going to make a stop early on the tour here as well. Example 7-15 illustrates more bind event protocols. print 'Widget s X s Y s' (event.widget, event.x, event.y) def showAllEvent(event) print event print attr, ' > ', getattr(event, attr) print 'Got left mouse...

Binding events on specific items

Much like we did for the Text widget, it is also possible to bind events for one or more specific objects drawn on a Canvas with its tag_bind method. This call accepts either a tag name string or object ID in its first argument. For instance, you can register a different callback handler for mouseclicks on every drawn item, or on any in a group of drawn and tagged items, rather than for the entire canvas at large. Example 8-16 binds a double-click handler in both the canvas itself and on two...

Bound method callback handlers

Class bound methods work particularly well as callback handlers too they record both an instance to send the event to, and an associated method to call. As a preview, Example 6-14 shows Example 6-12 rewritten to register a bound class method, instead of a function or lambda result. widget Button(None, text 'Hello event world', command self.quit) print 'Hello class method world' self.quit is a bound method import sys sys.exit() retains the On a button press, Tkinter calls this class's quit...

Buffer size

The open call also takes an optional third buffer size argument, which lets you control stdio buffering for the file -- the way that data is queued up before being transferred to boost performance. If passed, means file operations are unbuffered (data is transferred immediately), 1 means they are line buffered, any other positive value means use a buffer of approximately that size, and a negative value means to use the system default (which you get if no third argument is passed, and generally...

Builtin File Objects

For most purposes, the open function is all you need to remember to process files in your scripts. The file object returned by open has methods for reading data (read, readline, readlines), writing data (write, writelines), freeing system resources (close), moving about in the file (seek), forcing data to be transferred out of buffers (flush), fetching the underlying file handle (fileno), and more. Since the built-in file object is so easy to use, though, let's jump right in to a few...

But here each Python operation is replaced by a C API call Example PPEIntegrate EmbedBasicsembeddictc

* make a new dictionary for code string namespace PyObject *pdict, *pval printf(embed-dict n) Py_Initialize( ) * make a new namespace * pdict PyDict_New( ) PyDict_SetItemString(pdict, _builtins_ PyDict_SetItemString(pdict, Y, PyInt_FromLong(2)) * dict 'Y' 2 * PyRun_String(X 99, Py_file_input, pdict, pdict) * run statements * PyRun_String(X X+Y, Py_file_input, pdict, pdict) * same X and Y * pval PyDict_GetItemString(pdict, X) * fetch dict 'X' * PyArg_Parse(pval, i, & cval) * convert to C *...

But Its 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,...

But What About That Bottleneck

Back to our original question how can the act of writing software be made easier At some level, Python is really just another computer language. It's certainly true that Python the language doesn't represent much that's radically new from a theoretical point of view. So why should we be excited about Python when so many languages have been tried already What makes Python of interest, and what may be its larger contribution to the development world, is not its syntax or semantics, but its world...

Extensions Overview

Because Python itself is coded in C today, compiled Python extensions can be coded in any language that is C-compatible in terms of call stacks and linking. That includes C, but also C++ with appropriate extern C declarations (which are automatically provided in Python header files). Python extensions coded in a C-compatible language can take two forms C modules, which look and feel to their clients like Python module files C types, which behave like standard built-in types (numbers, lists,...

C to Python Returning values to Python

There are two ways to convert C data to Python objects by using type-specific API functions, or the general object-builder function Py_BuiidVaiue. The latter is more general, and is essentially the inverse of PyArg_Parse, in that Py_BuiidVaiue converts C data to Python objects according to a format string. For instance, to make a Python string object from a C char*, the heiio module uses an s convert code return Py_BuiidVaiue(s, resuit) resuit is a C char More specific object constructors can...

Calling Python Objects

The last two sections dealt with running strings of code, but it's easy for C programs to deal in terms of Python objects too. Example 20-6 accomplishes the same task as Examples Example 20-2 and Example 20-5, but uses other API tools to interact with objects in the Python module directly PyImport_ImportModule imports the module from C as before PyObject_GetAttrString fetches an object's attribute value by name PyEval_CallObject calls a Python function (or class, or method) PyArg_Parse converts...

Canvas object tags

But it gets even better In addition to object identifiers, you can also perform canvas operations on entire sets of objects at once, by associating them all with a tag , a name that you make up and apply to objects on the display. Tagging objects in a Canvas is at least similar in spirit to tagging substrings in the Text widget we studied in the prior section. In general terms, canvas operation methods accept either a single object's identifier or a tag name. For example, you can move an entire...

Capturing the stderr Stream

We've been focusing on stdin and stdout redirection, but stderr can be similarly reset to files, pipes, and objects. This is straightforward within a Python script. For instance, assigning sys.stderr to another instance of a class like Output in the preceding example allows your script to intercept text written to standard error too. The popen3 call mentioned previously also allows stderr to be intercepted within a script. Redirecting standard error from a shell command line is a bit more...

CGI Script Tradeoffs

As shown in this chapter, PyMailCgi is still something of a system in the making, but it does work as advertised by pointing a browser at the main page's URL, I can check and send email from anywhere I happen to be, as long as I can find a machine with a web browser. In fact, any machine and browser will do Python doesn't even have to be installed.151 That's not the case with the PyMailGui client-side program we wrote in Chapter 11. 5 This property can be especially useful when visiting...

Chaining programs with pipes

On Windows and Unix-like platforms, it's also possible to send the standard output of one program to the standard input of another, using the shell character between two commands. This is usually called a pipe operation -- the shell creates a pipeline that connects the output and input of two commands. Let's send the output of the Python script to the standard more command-line program's input to see how this works C PP2E System Streams> python teststreams.py < input.txt more Hello stream...

Changing Classes of Stored Objects

Technically, Python reimports a class to recreate its stored instances as they are fetched and unpickled. Here's how this works When Python pickles a class instance to store it in a shelve, it saves the instance's attributes plus a reference to the instance's class. Really, Python serializes and stores the instance's_dict_attribute dictionary along with source file information When Python unpickles a class instance fetched from a shelve, it recreates the instance object in memory by reimporting...

Changing input layouts

Suppose that you've written a system like this, and your users, clients, and significant other start complaining that the input form is difficult to read. Don't worry. Because the CGI model naturally separates the user interface (the HTML page definition) from the processing logic (the CGI script), it's completely painless to change the form's layout. Simply modify the HTML file there's no need to change the CGI code at all. For instance, Example 12-14 contains a new definition of the input...

Changing serverside examples

Of course, running scripts in your browser isn't quite the same as writing scripts on your own. If you do decide to change these CGI programs or write new ones from scratch, you must be able to access web server machines To change server-side scripts, you need an account on a web server machine with an installed version of Python. A basic account on such a server is often enough. Then edit scripts on your machine and upload to the server by FTP. To type explicit command lines on a server...

Changing Shell Variables

Like normal dictionaries, the os.environ object supports both key indexing and assignment. As usual, assignments change the value of the key > > > os.environ 'TEMP' r'c temp' > > > os.environ 'TEMP' But something extra happens here. In recent Python releases, values assigned to os.environ keys in this fashion are automatically exported to other parts of the application. That is, key assignments change both the os.environ object in the Python program as well as the associated...