List membership with the in operator 58, List concatenation with the +
operator 59, List initialization with the * operator 59, List minimum or
maximum with min and max 59, List search with index 60, List matches with
count 60
6.6 Nested lists and deep copies 60
6.7 Tuples 63
Tuple basics 63, Tuple packing and unpacking 65, Converting between
lists and tuples 66
6.8 Summary 66
7 Strings 67
7.1 Strings as sequences of characters 67
7.2 Basic string operations 68
7.3 Special characters and escape sequences 68
Basic escape sequences 69, Numeric (octal and hexadecimal) escape
sequences 69, Printing vs. evaluating strings with special characters 70
7.4 The ?string? module 70
string.split and string.join 71, Converting strings to numbers 72,
Getting rid of extra whitespace 73, String searching 74, Modifying strings
75, Modifying strings with list manipulations 76, Useful constants 77
7.5 Converting from objects to strings 77
7.6 Formatting strings 79
Using formatting sequences 80, Named parameters and formatting sequences
80
8 Dictionaries 82
8.1 What is a dictionary? 82
Why dictionaries are called dictionaries 84
8.2 Other dictionary operations 84
8.3 Word counting 86
8.4 What can be used as a key? 87
8.5 Sparse matrices 87
8.6 Dictionaries as caches 88
8.7 Efficiency of dictionaries 89
9 Control flow 90
9.1 The while loop 90
9.2 The if-elif-else statement 91
9.3 The for loop 92
The range function 92, The for loop and tuple unpacking 93
9.4 Statements, blocks, and indentation 94
9.5 Boolean values and expressions 96
Most Python objects can be used as booleans 97, Comparison and boolean
operators 97
10 Functions and procedures 99
10.1 Basic function and procedure definitions 99
10.2 Assigning functions to variables 100
10.3 Lambda expressions 100
10.4 Function parameter options 101
Default values 101, Passing arguments by parameter name 102, Variable
numbers of arguments 102, Mixing argument-passing techniques 104
10.5 Mutable objects as arguments 104
10.6 Local and global variables 104
10.7 Summary 105
11 Modules and scoping rules 106
11.1 What is a module? 106
11.2 A first module 107
11.3 The import statement 109
11.4 The module search path 110
Where to place your own modules 111
11.5 Private names in modules 112
11.6 Library and third-party modules 113
11.7 Python scoping rules and namespaces 114
12 Using the filesystem 120
12.1 Paths and pathnames 120
Absolute and relative paths 121, The current working directory 122,
Manipulating pathnames 123, Useful constants and functions 126
12.2 Getting information about files 127
12.3 More filesystem operations 128
12.4 Processing all files in a directory subtree 129
12.5 Summary 130
13 Reading and writing files 131
13.1 Opening files and file objects 131
13.2 Closing files 132
13.3 Opening files in write or other modes 132
13.4 Functions to read and write text or binary data 133
13.5 Screen input/output and redirection 134
13.6 The struct module 137
13.7 Pickling objects into files 138
13.8 Shelving objects 141
13.9 Summary 143
14 Exceptions 144
14.1 Introduction to exceptions 144
General philosophy of errors and exception handling 145, A more formal
definition of exceptions 147, User-defined exceptions 148
14.2 Exceptions in Python 148
Types of Python exceptions 148, Raising exceptions 149, Catching and
handling exceptions 150, Defining new exceptions 151, Debugging programs
with the assert statement 152, The exception inheritance hierarchy 153,
Example: our disk writing program in Python 153, Example: exceptions in
normal evaluation 154, Where to use exceptions 155
15 Scripts 156
15.1 Creating a very basic script 157
Starting a script from a command line 157, Command line arguments 157,
Redirecting the input and output of a script 158, The getopt module 159,
Using the fileinput module 159
15.2 Making a script directly executable on UNIX 161
15.3 Script execution options in Windows 161
Starting a script as a document or shortcut 162, Starting a script from
the Windows run box 163, Starting a script from an MS-DOS Prompt window 164,
Other Windows options 164
15.4 Scripts on Windows versus scripts on UNIX 165
15.5 Scripts and modules 167
15.6 Creating executable programs with freeze 171
15.7 Summary 171
16 Classes and object-oriented programming 173
16.1 Defining classes 174
Using a class instance as a structure or record 174, Objects, class
instances, and instances of other Python types 175
16.2 Instance variables 175
16.3 Methods 175
16.4 Class variables 177
An oddity with class variables 178
16.5 Class methods 179
16.6 Inheritance 180
16.7 Inheritance with class and instance variables 182
16.8 Private variables and private methods 184
16.9 Scoping rules and namespaces for class instances 185
16.10 Destructors and memory management 189
16.11 Multiple inheritance 192
16.12 Summary 193
17 Graphical User Interfaces and Tk 194
17.1 Installing Tk 195
17.2 Starting Tk and using Tkinter 195
17.3 Principles of Tk 196
Widgets 196, Named attributes 197, Geometry management and widget
placement 198
17.4 A simple Tkinter application 198
17.5 Creating widgets 200
17.6 Widget placement 201
17.7 What else can Tkinter do? 203
Event handling 203, Canvas and text widgets 204, Extensions to Tk and
Tkinter 205
17.8 Alternatives to Tk and Tkinter 206
17.9 Summary 206
Part 3 Advanced language features 209
18 Packages 211
18.1 What is a package? 211
18.2 A first example 211
18.3 A concrete example 213
Basic use of the mathproj package 214, Loading subpackages and
submodules 214, Import statements within packages 215, __init__.py files in
packages 216
18.4 The __all__ attribute 216
18.5 Proper use of packages 218
19 Data types as objects 219
19.1 Types are objects, too 219
19.2 Using types 220
19.3 The types module 220
19.4 Types and user-defined classes 221
20 Special method attributes 224
20.1 What is a special method attribute? 224
20.2 Making an object behave like a list 226
The __getitem__ special method attribute 226, How it works 227,
Implementing full list functionality 228
20.3 Sample problem 2 229
20.4 When to use special method attributes 231
21 Regular expressions 232
21.1 What is a regular expression? 232
21.2 Regular expressions with metacharacters 233
21.3 Regular expressions and raw strings 234
Raw strings to the rescue 235
21.4 Extracting matched text from strings 236
21.5 Substituting text with regular expressions 238
21.6 What else can regular expressions do? 239
Part 4 Advanced topics and applications 241
22 Python, Windows, and COM 243
22.1 Introduction 243
22.2 How to use Python with COM 244
22.3 Installation and setup 246
22.4 Anatomy of a Python COM server 246
Importing the Python COM Modules 246, Setting up the Python COM server
class 247, Writing server class methods 248, Registering the Python server
class with COM 248, Test code 248, Creating a module body 249, The Python
COM server template as a whole 250
22.5 Creating and using the server from Visual Basic 251
Troubleshooting the server 252, Using the server 252, Server shutdown
253
22.6 Passing data in and out of the server 253
Numbers 253, Strings 254, Retrieving lists 254, Passing in lists 254,
Retrieving tabular data 255, More about variant arrays 255, Odd-shaped lists
256, Passing objects 256
22.7 Callbacks 257
22.8 Using Excel as a client 258
Setting up an Excel workbook with a server 259, Fetching data 259, What
next? 261
22.9 Distributed COM 261
22.10 Client side COM?automating Office 261
Fetching data from Access 262, Update an Access record 262, Start Excel
from Python, push in some data 263, Start Word, push in some data 263
22.11 Type libraries 263
22.12 Guidelines 264
22.13 Other goodies 265
Windows Scripting Host 265, ODBC 266, MFC wrappers 267, Other Windows
API wrappers 267, Call any DLL 267
22.14 Sources of information 267
23 Extending Python with C and C++ 269
23.1 Using this chapter 269
23.2 Compilation details 270
Which compiler? 270, Shared versus static extensions 270, Compilation
details for your system 270
23.3 A first example 271
Putting it into a template 274
23.4 Memory management 275
The need for memory management 275, A basic example 276, When are
reference counts decremented? 278, Reference counting is a tricky business
278
23.5 Writing extensions in C++ 278
23.6 Where to go from here 278
24 Integration with the Java Virtual Machine: JPython 279
24.1 What is JPython? 279
24.2 Prologue: the false hope for a single language 280
So, what about Java? 281
24.3 JPython, the killer scripting language 281
24.4 Downloading and installing JPython 283
Test driving JPython 284
24.5 Using Java from JPython 285
Extending Java classes 287
24.6 Using JPython from Java 289
24.7 Compiling JPython classes 293
Notes on using jpythonc 295
24.8 Using Java and JPython together 295
24.9 Conclusion 296
25 HTML and Python?the HTMLgen package 297
25.1 Uses for HTMLgen 298
An HTMLgen example 298
25.2 How HTMLgen renders objects 304
25.3 Document model 304
Document class hierarchy 304
25.4 Tables 306
Quick and dirty tables 306, Full table component version 307
25.5 Lists 308
25.6 Frames 309
25.7 Images 310
25.8 Image maps 311
25.9 The <A> tag 312
25.10 Call protocol 313
25.11 CSS1 support 313
Advanced style features 315
25.12 CGI and forms 315
CGI with the StickyForm class 319, CGI with Zope 319
25.13 Auxiliary modules 319
25.14 The future 320
26 Using ?Zope? 321
26.1 Introduction 321
26.2 Object publishing 322
What is object publishing? 322, Object traversal: from URLs to object
calls 323, Object publishing details 324, A simple example 325, Guest books
and ad generators 327
26.3 Advanced Zope 332
HTML generation with DocumentTemplate 332, Conditionals, sequences, and
expressions 334, Object persistence with BoboPOS 336, More features 338, The
Web Job Board 339