The Python 2.1 Bible provides the only complete Python language reference on the market and includes all the information and software that developers need to use Python as a rapid application development tool.
The Python 2.1 Bible fills a critical void in the Python reference market. Although it includes a complete Python language reference section, it is still geared towards those of you who already have some programming experience. This book explains each piece of technology in depth and shows through clear examples why each feature is useful. This is the manual you've been waiting for -- the one that covers all major Python components without glossing over how the various pieces fit together.
Author(s): Dave Brueck, Stephen Tanner
Edition: 1
Publisher: Wiley
Year: 2001
Language: English
Pages: 731
Python 2.1 Bible......Page 1
About the Authors......Page 7
About This Book......Page 10
Part I: The Python Language......Page 11
Part VII: Platform-Specific Support......Page 12
What the Icons Mean......Page 13
Visit Us!......Page 14
Acknowledgments......Page 15
Contents at a Glance......Page 16
Contents......Page 18
The Python Language......Page 38
Jumping In: Starting the Python Interpreter......Page 40
Pocket calculator......Page 41
Defining a Function......Page 42
Looping and Control......Page 43
Integer division......Page 44
Breaking and continuing......Page 45
Slicing and dicing......Page 46
Dictionaries......Page 47
Sample Program: Word Frequencies......Page 48
Some quick object jargon......Page 51
Example: the point class......Page 52
Summary......Page 54
Identifiers and Operators......Page 56
Operators......Page 57
Long integers......Page 58
Imaginary numbers......Page 59
Manipulating numeric types......Page 60
Simple assignment statements......Page 63
Augmented assignment......Page 64
Summary......Page 65
Comparing numeric types......Page 66
Compound expressions......Page 68
Complex expressions......Page 69
Operator precedence......Page 70
Strings......Page 71
String literals......Page 72
Manipulating strings......Page 74
Comparing strings......Page 79
Converting Between Simple Types......Page 80
Converting to numerical types......Page 81
Converting to strings......Page 82
Summary......Page 84
Grouping Data with Sequences......Page 86
Creating lists......Page 87
Joining and repeating with arithmetic operators......Page 89
Accessing parts of sequences......Page 90
Using sequence utility functions......Page 92
Additional operations......Page 94
List object methods......Page 95
Mapping Information with Dictionaries......Page 97
Accessing and updating dictionary mappings......Page 98
Additional dictionary operations......Page 99
Object identity......Page 100
Counting references......Page 101
Shallow copies......Page 102
Deep copies......Page 103
Identifying Data Types......Page 104
Creating arrays......Page 105
Converting between types......Page 106
Summary......Page 108
Making Decisions with If- Statements......Page 110
Anatomy of a for-loop......Page 111
Looping example: encoding strings......Page 112
Ranges and xranges......Page 113
Breaking, continuing, and else-clauses......Page 114
Changing horses in midstream......Page 115
Throwing and Catching Exceptions......Page 116
Handling an exception......Page 117
More on exceptions......Page 118
Cleaning up with finally......Page 119
Assertions in Python......Page 120
Example: Game of Life......Page 121
Summary......Page 123
Defining Functions......Page 124
All about parameters......Page 125
Arbitrary arguments......Page 126
A bit of functional programming......Page 127
Laying out a module......Page 128
Importing Modules......Page 129
Reimporting modules......Page 130
Python path......Page 131
Is it local or global?......Page 132
Grouping Modules into Packages......Page 133
Compiling and Running Programmatically......Page 134
Summary......Page 135
Overview of Object-Oriented Python......Page 136
Creating Classes and Instance Objects......Page 137
More on accessing attributes......Page 138
Deriving New Classes from Other Classes......Page 139
Multiple inheritance......Page 140
Creating a custom list class......Page 141
Creating a custom string class......Page 142
Hiding Private Data......Page 143
Identifying Class Membership......Page 144
Overloading Standard Behaviors......Page 145
Overloading basic functionality......Page 146
Overloading numeric operators......Page 148
Overloading sequence and dictionary operators......Page 149
Overloading bitwise operators......Page 151
Using Weak References......Page 152
Creating weak references......Page 153
Creating proxy objects......Page 154
Summary......Page 155
Printing to the Screen......Page 156
raw_input......Page 157
Opening, Closing, and Positioning Files......Page 158
open......Page 159
File position......Page 160
Writing Files......Page 161
Reading Files......Page 162
Accessing Standard I/O......Page 163
Using Filelike Objects......Page 164
Summary......Page 166
Files, Data Storage, and Operating System Services......Page 168
Using String Objects......Page 170
String case-changing methods......Page 171
String searching methods......Page 172
String manipulation methods......Page 174
Character categories......Page 175
Miscellaneous functions......Page 176
Regular expression syntax......Page 177
Character groups and other backslash magic......Page 179
Extensions......Page 180
Creating and Using Regular Expression Objects......Page 181
Using regular expression objects......Page 182
Using Match Objects......Page 184
start([groupid]), end([groupid]), span([groupid])......Page 185
Treating Strings as Files......Page 186
Reading and writing non-ASCII strings......Page 188
Using the Unicode database......Page 190
Summary......Page 191
Retrieving File and Directory Information......Page 192
The piecemeal approach......Page 193
The I-want-it-all approach......Page 196
Joining path parts......Page 198
Other path modifiers......Page 199
Listing Directories and Matching File Names......Page 200
Current working directory......Page 202
Example: Recursive Grep Utility......Page 203
Renaming......Page 205
Creating Directories and Temporary Files......Page 206
Comparing Files and Directories......Page 208
General file descriptor functions......Page 210
Randomly accessing lines in text files......Page 211
Using memory-mapped files......Page 212
Iterating over several files......Page 213
Summary......Page 214
Executing Shell Commands and Other Programs......Page 216
popen functions......Page 218
spawn functions......Page 219
Process management and termination......Page 220
Handling Process Information......Page 222
Retrieving System Information......Page 224
Managing Configuration Files......Page 225
Understanding Error Names......Page 227
Handling Asynchronous Signals......Page 228
Summary......Page 230
Text versus binary......Page 232
On the receiving end......Page 233
Pickling with pickle......Page 234
Example: Moving Objects Across a Network......Page 237
Using Database-Like Storage......Page 240
Converting to and from C Structures......Page 241
SunÌs XDR format......Page 245
Compressing Data......Page 247
zlib......Page 248
gzip......Page 250
zipfile......Page 251
Summary......Page 254
Ticks......Page 256
Stopwatch time......Page 257
Converting Between Time Formats......Page 258
Fancy formatting......Page 259
Localization......Page 260
Printing monthly and yearly calendars......Page 261
Calendar information......Page 262
Using Time Zones......Page 263
Summary......Page 264
Using Disk-Based Dictionaries......Page 266
DBM Example: Tracking Telephone Numbers......Page 268
gdbm......Page 269
Using BSD database objects......Page 270
Transactions......Page 271
Example: ÏSounds-LikeÓ Queries......Page 272
Examining Relational Metadata......Page 274
Example: Creating Auditing Tables......Page 275
Advanced Features of the DB API......Page 277
Input and output sizes......Page 278
Database library information......Page 279
Error hierarchy......Page 280
Summary......Page 281
Networking and the Internet......Page 282
Networking Background......Page 284
Working with Addresses and Host Names......Page 285
Creating and destroying sockets......Page 287
Connecting sockets......Page 288
Sending and receiving data......Page 289
Using socket options......Page 290
Example: A Multicast Chat Application......Page 293
The SocketServer family......Page 298
Request handlers......Page 300
Processing Web Browser Requests......Page 301
BaseHTTPRequestHandler......Page 302
SimpleHTTPRequestHandler......Page 303
Example: form handler CGI script......Page 304
Handling Multiple Requests Without Threads......Page 306
asyncore......Page 308
Summary......Page 310
PythonÌs Internet Protocol Support......Page 312
Manipulating URLs......Page 313
URLopeners......Page 314
Extended URL opening......Page 315
Building and using request objects......Page 316
Accessing POP3 accounts......Page 318
Accessing SMTP accounts......Page 320
Accessing IMAP accounts......Page 322
Transferring Files via FTP......Page 326
Retrieving Resources Using Gopher......Page 328
Working with Newsgroups......Page 329
Reading and writing......Page 333
Other methods......Page 334
Setting up CGI scripts......Page 335
Accessing form fields......Page 336
A note on debugging......Page 338
Summary......Page 339
Manipulating URLs......Page 340
Formatter interface......Page 341
Writer interface......Page 342
Other module resources......Page 343
Reading Web Spider Robot Files......Page 344
Viewing Files in a Web Browser......Page 345
Retrieving header values......Page 346
Address lists......Page 347
MIME messages......Page 348
Encoding and decoding MIME messages......Page 349
Writing out multipart MIME messages......Page 350
Handling document types......Page 353
Uuencode......Page 354
Base64......Page 355
Quoted-printable......Page 356
Working with MH mailboxes......Page 357
Using Web Cookies......Page 358
Morsels......Page 359
Example: a cookie importer......Page 360
Summary......Page 361
Markup Language Basics......Page 362
Tag rules......Page 363
Parsing HTML Files......Page 364
Other parsing methods......Page 365
Handling unknown or bogus elements......Page 366
Example: Bold Only......Page 367
Example: Web Robot......Page 368
Using a ContentHandler......Page 371
Example: blood-type extractor......Page 372
Using parser (XMLReader) objects......Page 373
SAX exceptions......Page 374
Elements, attributes, and text......Page 375
Example: data import and export with DOM......Page 376
Parsing XML with xmllib......Page 378
Elements and attributes......Page 379
Summary......Page 380
User Interfaces and Multimedia......Page 382
Getting Your Feet Wet......Page 384
Building an interface with widgets......Page 385
Laying Out Widgets......Page 386
Packer options......Page 387
Grid options......Page 388
Example: Breakfast Buttons......Page 389
Color options......Page 391
Behavior options......Page 392
Gathering User Input......Page 393
Example: Printing Fancy Text......Page 394
Using Text Widgets......Page 396
Building Menus......Page 397
Using Tkinter Dialogs......Page 398
Example: Text Editor......Page 399
Colors......Page 402
The canvas widget......Page 403
Manipulating canvas items......Page 404
Example: A Bouncing Picture......Page 405
Summary......Page 407
Creating event handlers......Page 408
Binding keyboard events......Page 409
Example: A Drawing Canvas......Page 410
Listbox......Page 412
Scrollbar......Page 413
Example: Color Scheme Customizer......Page 414
Creating Dialogs......Page 418
Supporting Drag-and-Drop Operations......Page 419
Using Cursors......Page 422
Designing New Widgets......Page 424
Learning more......Page 426
Summary......Page 427
Introducing wxPython......Page 428
Creating Simple wxPython Programs......Page 429
Managed windows......Page 431
Nonmanaged windows......Page 432
Common controls......Page 436
Tree controls......Page 437
Controlling Layout......Page 438
Specifying coordinates......Page 439
Sizers......Page 440
Layout constraints......Page 443
Using Built-in Dialogs......Page 444
Drawing with Device Contexts......Page 445
Adding Menus and Keyboard Shortcuts......Page 448
Other wxPython Features......Page 449
Fonts......Page 450
Summary......Page 451
A Curses Overview......Page 452
Displaying and Erasing Text......Page 453
Reading from the window (screen-scraping)......Page 454
The window background......Page 455
Example: masking a box......Page 456
Moving the Cursor......Page 457
Getting User Input......Page 458
Fancy characters......Page 459
Reading mouse input......Page 460
Example: yes, no, or maybe......Page 461
Pads......Page 462
Editing Text......Page 463
Numbering......Page 464
Example: A Simple Maze Game......Page 465
Summary......Page 469
Beginning with the End in Mind......Page 470
Understanding the Lepto Language......Page 472
The shlex module......Page 473
Putting shlex to work......Page 474
Using the cmd module......Page 477
Subclassing cmd.Cmd......Page 479
Executing Lepto Commands......Page 482
Summary......Page 488
Sound File Basics......Page 490
Playing sound on Windows......Page 491
Playing and recording sound on SunOS......Page 492
Reading and Writing Audio Files......Page 493
Reading and writing AIFF files with aifc......Page 494
Example: Reversing an audio file......Page 495
Reading IFF chunked data......Page 497
Examining a fragment......Page 498
Translating between storage formats......Page 499
Manipulating fragments......Page 500
Summary......Page 501
Advanced Python Programming......Page 502
Image Basics......Page 504
Identifying Image File Types......Page 505
Color systems......Page 506
Converting from one system to another......Page 507
Using the Python Imaging Library......Page 509
Retrieving image information......Page 510
Copying and converting images......Page 511
Using PIL with Tkinter......Page 512
Modifying pixel data......Page 513
Summary......Page 517
Understanding Threads......Page 518
Creating threads with the thread module......Page 519
Starting and stopping threads with the threading module......Page 520
Waiting for a thread to finish......Page 521
Locking with thread......Page 522
Locking with threading......Page 523
Preventing Deadlock......Page 525
Example: Downloading from Multiple URLs......Page 526
Porting Threaded Code......Page 531
Technical Note: How Simultaneous Is Simultaneous?......Page 532
Summary......Page 533
Starting and stopping the debugger......Page 534
Examining the state of things......Page 535
Setting breakpoints......Page 536
Debugging tips......Page 537
Working with docstrings......Page 538
Synching docstrings with code......Page 539
Unit testing......Page 540
Profiling code......Page 542
Using Profile objects......Page 543
Customizing statistics......Page 544
Sorting......Page 546
I/O......Page 547
Threads......Page 548
Reference counts and Python code......Page 549
Summary......Page 550
Checking Passwords......Page 552
Running in a Restricted Environment......Page 553
The rexec sandbox......Page 554
Using a class fortress......Page 557
Creating Message Fingerprints......Page 558
SHA......Page 559
Using 1940s-Era Encryption......Page 560
Summary......Page 562
Extending and Embedding Overview......Page 564
Writing a Simple Extension Module......Page 565
Building and Linking......Page 568
Unpacking normal arguments......Page 569
Using special format characters......Page 572
Unpacking keyword arguments......Page 574
Converting C Data to Python......Page 575
Creating simple Python objects......Page 576
Creating complex Python objects......Page 577
Shutting down......Page 578
System information functions......Page 579
Running Python Code from C......Page 580
SWIG......Page 583
CXX......Page 586
Extension classes......Page 587
Summary......Page 588
Types of reference ownership......Page 590
Reference conventions......Page 591
Using the Abstract and Concrete Object Layers......Page 592
Working with generic objects......Page 593
Any numerical type......Page 595
Longs......Page 597
Working with Sequence Objects......Page 598
Any sequence type......Page 599
Strings......Page 600
Lists......Page 601
Tuples......Page 602
Buffers......Page 603
Unicode strings......Page 604
Functions for any mapping type......Page 606
Dictionaries......Page 607
File......Page 608
Module......Page 609
Creating Threads and Sub-Interpreters......Page 611
Threads......Page 612
Handling Errors and Exceptions......Page 613
Signaling error conditions......Page 614
Raising warnings......Page 615
Managing Memory......Page 616
Summary......Page 617
Rounding and fractional parts......Page 618
Trigonometric functions......Page 619
Random numbers......Page 620
Random distributions......Page 622
Example: plotting distributions using Monte Carlo sampling......Page 623
Using Arbitrary-Precision Numbers......Page 624
Summary......Page 625
Installing NumPy......Page 626
Accessing and Slicing Arrays......Page 627
Converting arrays to lists and strings......Page 629
Calling Universal Functions......Page 630
Example: editing an audio stream......Page 631
Repeating ufuncs......Page 632
Array creation functions......Page 634
Seeding arrays with functions......Page 635
Reshaping and Resizing Arrays......Page 637
sort(array,[axis=-1])......Page 638
Matrix operations......Page 639
Array Example: Analyzing Price Trends......Page 640
Summary......Page 641
Printing a traceback Ûprint_exc and friends......Page 642
Extracting and formatting exceptions......Page 643
Eating arbitrary exceptions is bad for you......Page 644
Review: basic introspection......Page 645
Browsing function information......Page 646
Tokenizing Python Code......Page 648
Example: Syntax-Highlighting Printer......Page 649
Creating an AST......Page 650
Low-Level Object Creation......Page 651
Disassembling Python Code......Page 652
Summary......Page 653
Deploying Python Applications......Page 654
Internationalization and Localization......Page 656
An NLS example......Page 657
What it all means......Page 660
Changing the locale......Page 661
Locale-specific formatting......Page 662
Properties of locales......Page 663
Summary......Page 664
Understanding Module Importing......Page 666
Finding and Loading Modules with imp......Page 668
Importing Encrypted Modules......Page 670
Subclassing Importer......Page 673
Creating the remote Importer......Page 674
Testing the remote Importer......Page 677
Summary......Page 679
Understanding distutils......Page 680
Creating a simple distribution......Page 681
Installing the simple distribution......Page 682
Distributing packages......Page 684
Including other files......Page 685
Distributing Extension Modules......Page 687
Creating Source and Binary Distributions......Page 688
Source distributions......Page 689
Installers......Page 690
py2exe......Page 692
Freeze......Page 693
Summary......Page 694
Platform-Specific Support......Page 696
Data types......Page 698
Example: Using Some Windows APIs......Page 699
Accessing the registry with win32all......Page 701
Creating, deleting, and navigating keys......Page 703
Example: recursive deletion of a key......Page 704
Accessing the registry with _winreg......Page 705
Console I/O......Page 706
Summary......Page 707
Checking UNIX Passwords and Groups......Page 708
Accessing the System Logger......Page 710
Providing Identifier and Keyword Completion......Page 712
Retrieving File System and Resource Information......Page 714
Resource usage......Page 715
Resource limits......Page 716
Controlling File Descriptors......Page 717
Handling Terminals and Pseudo- Terminals......Page 718
Interfacing with SunÌs NIS ÏYellow PagesÓ......Page 719
Summary......Page 720
Installing Software......Page 722
Finding Answers to Questions......Page 723
Understanding PEPs: Python Enhancement Proposals......Page 724
Overview of Python IDEs......Page 726
Using Python mode......Page 728
Exploring the IDLE Python shell......Page 729
More IDLE shortcuts......Page 731
Debugging with IDLE......Page 732
Editing source in PythonWin......Page 735
Debugging with PythonWin......Page 736
Index......Page 738