Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
4807-7 FM.F 5/24/01 8:57 AM Page xvii Contents Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ix Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiv Part I: The Python Language 1 Chapter 1: Python in an Hour . . . . . . . . . . . . . . . . . . . . . . . . 3 Jumping In: Starting the Python Interpreter . . . . . . . . . . . . . . . . . . . 3 Experimenting with Variables and Expressions . . . . . . . . . . . . . . . . . 4 Pocket calculator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Defining a Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Running a Python Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Looping and Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 Integer division . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Branching with if-statements . . . . . . . . . . . . . . . . . . . . . . . . 8 Breaking and continuing . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 Lists and Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Slicing and dicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Reading and Writing Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Sample Program: Word Frequencies . . . . . . . . . . . . . . . . . . . . . . . 11 Loading and Using Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Creating a Class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Some quick object jargon . . . . . . . . . . . . . . . . . . . . . . . . . . 14 Object orientation, Python style . . . . . . . . . . . . . . . . . . . . . . 15 Keep off the grass — Accessing class members . . . . . . . . . . . . . 15 Example: the point class . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Recommended Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17 Chapter 2: Identifiers, Variables, and Numeric Types . . . . . . . . . . 19 Identifiers and Operators . . Reserved words . . . . Operators . . . . . . . . Numeric Types . . . . . . . . Integers . . . . . . . . . Long integers . . . . . . Floating point numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 20 20 21 21 21 22 4807-7 FM.F xviii 5/24/01 8:57 AM Page xviii Python 2.1 Bible Imaginary numbers . . . . . . Manipulating numeric types . Assigning Values to Variables . . . . Simple assignment statements Multiple assignment . . . . . . Augmented assignment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 23 26 26 27 27 Chapter 3: Expressions and Strings . . . . . . . . . . . . . . . . . . . . 29 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comparing numeric types . . . . . . . . . . . . . . . . . . . . . . . . . Compound expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . Complex expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . Operator precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . String literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Manipulating strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comparing strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Unicode string literals . . . . . . . . . . . . . . . . . . . . . . . . . . . . Converting Between Simple Types . . . . . . . . . . . . . . . . . . . . . . . . Converting to numerical types . . . . . . . . . . . . . . . . . . . . . . . Converting to strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 29 31 32 33 34 35 37 42 43 43 44 45 Chapter 4: Advanced Data Types . . . . . . . . . . . . . . . . . . . . . . 49 Grouping Data with Sequences . . . . . . . . . . . . . . Creating lists . . . . . . . . . . . . . . . . . . . . . Creating tuples . . . . . . . . . . . . . . . . . . . . Working with Sequences . . . . . . . . . . . . . . . . . Joining and repeating with arithmetic operators Comparing and membership testing . . . . . . . Accessing parts of sequences . . . . . . . . . . . Iterating with for...in . . . . . . . . . . . . . . . . . Using sequence utility functions . . . . . . . . . . Using Additional List Object Features . . . . . . . . . . Additional operations . . . . . . . . . . . . . . . . List object methods . . . . . . . . . . . . . . . . . Mapping Information with Dictionaries . . . . . . . . . Creating and adding to dictionaries . . . . . . . . Accessing and updating dictionary mappings . . Additional dictionary operations . . . . . . . . . Understanding References . . . . . . . . . . . . . . . . Object identity . . . . . . . . . . . . . . . . . . . . Counting references . . . . . . . . . . . . . . . . . Copying Complex Objects . . . . . . . . . . . . . . . . . Shallow copies . . . . . . . . . . . . . . . . . . . . Deep copies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 50 52 52 52 53 53 55 55 57 57 58 60 61 61 62 63 63 64 65 65 66 4807-7 FM.F 5/24/01 8:57 AM Page xix Contents Identifying Data Types . . . . . . . . . Working with Array Objects . . . . . . Creating arrays . . . . . . . . . . Converting between types . . . Array methods and operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 68 68 69 71 Chapter 5: Control Flow . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 Making Decisions with If-Statements . . . . . . . Using For-Loops . . . . . . . . . . . . . . . . . . Anatomy of a for-loop . . . . . . . . . . . . Looping example: encoding strings . . . . Ranges and xranges . . . . . . . . . . . . . Breaking, continuing, and else-clauses . . Changing horses in midstream . . . . . . . Using While-Loops . . . . . . . . . . . . . . . . . Throwing and Catching Exceptions . . . . . . . Passing the buck: propagating exceptions Handling an exception . . . . . . . . . . . More on exceptions . . . . . . . . . . . . . Defining and raising exceptions . . . . . . Cleaning up with finally . . . . . . . . . . . Debugging with Assertions . . . . . . . . . . . . Assertions in Python . . . . . . . . . . . . Toggling assertions . . . . . . . . . . . . . Example: Game of Life . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 74 74 75 76 77 78 79 79 80 80 81 82 82 83 83 84 84 Chapter 6: Program Organization . . . . . . . . . . . . . . . . . . . . . 87 Defining Functions . . . . . . . . . . . . . . . Pass by object reference . . . . . . . . All about parameters . . . . . . . . . . Arbitrary arguments . . . . . . . . . . Apply: passing arguments from a tuple A bit of functional programming . . . . Grouping Code with Modules . . . . . . . . . Laying out a module . . . . . . . . . . . Taking inventory of a module . . . . . Importing Modules . . . . . . . . . . . . . . . What else happens upon import? . . . Reimporting modules . . . . . . . . . . Exotic imports . . . . . . . . . . . . . . Locating Modules . . . . . . . . . . . . . . . Python path . . . . . . . . . . . . . . . Compiled files . . . . . . . . . . . . . . Understanding Scope Rules . . . . . . . . . Is it local or global? . . . . . . . . . . . Listing namespace contents . . . . . . Grouping Modules into Packages . . . . . . Compiling and Running Programmatically . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 88 88 89 90 90 91 91 92 92 93 93 94 94 94 95 95 95 96 96 97 xix 4807-7 FM.F xx 5/24/01 8:57 AM Page xx Python 2.1 Bible Chapter 7: Object-Oriented Python . . . . . . . . . . . . . . . . . . . . 99 Overview of Object-Oriented Python . . . . . . . . . . . . . . . . . . . . . . 99 Creating Classes and Instance Objects . . . . . . . . . . . . . . . . . . . . . 100 Creating instance objects . . . . . . . . . . . . . . . . . . . . . . . . . 101 More on accessing attributes . . . . . . . . . . . . . . . . . . . . . . . 101 Deriving New Classes from Other Classes . . . . . . . . . . . . . . . . . . . 102 Multiple inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 Creating a custom list class . . . . . . . . . . . . . . . . . . . . . . . . 104 Creating a custom string class . . . . . . . . . . . . . . . . . . . . . . 105 Creating a custom dictionary class . . . . . . . . . . . . . . . . . . . 106 Hiding Private Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Identifying Class Membership . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Overloading Standard Behaviors . . . . . . . . . . . . . . . . . . . . . . . . 108 Overloading basic functionality . . . . . . . . . . . . . . . . . . . . . 109 Overloading numeric operators . . . . . . . . . . . . . . . . . . . . . 111 Overloading sequence and dictionary operators . . . . . . . . . . . 112 Overloading bitwise operators . . . . . . . . . . . . . . . . . . . . . . 114 Overloading type conversions . . . . . . . . . . . . . . . . . . . . . . 115 Using Weak References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Creating weak references . . . . . . . . . . . . . . . . . . . . . . . . . 116 Creating proxy objects . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Chapter 8: Input and Output . . . . . . . . . . . . . . . . . . . . . . . 119 Printing to the Screen . . . . . . . . . . Accessing Keyboard Input . . . . . . . . raw_input . . . . . . . . . . . . . . input . . . . . . . . . . . . . . . . . Opening, Closing, and Positioning Files open . . . . . . . . . . . . . . . . . File object information . . . . . . close . . . . . . . . . . . . . . . . . File position . . . . . . . . . . . . . Writing Files . . . . . . . . . . . . . . . . Reading Files . . . . . . . . . . . . . . . Accessing Standard I/O . . . . . . . . . Using Filelike Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part II: Files, Data Storage, and Operating System Services . . . . . . . . . . . . . 119 120 120 121 121 122 123 123 123 124 125 126 127 131 Chapter 9: Processing Strings and Regular Expressions . . . . . . . 133 Using String Objects . . . . . . . . . . . . String formatting methods . . . . . String case-changing methods . . . String format tests (the is-methods) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 134 134 135 4807-7 FM.F 5/24/01 8:57 AM Page xxi Contents String searching methods . . . . . . . . . . . . . . . String manipulation methods . . . . . . . . . . . . . Using the String Module . . . . . . . . . . . . . . . . . . . Character categories . . . . . . . . . . . . . . . . . . Miscellaneous functions . . . . . . . . . . . . . . . . Defining Regular Expressions . . . . . . . . . . . . . . . . Regular expression syntax . . . . . . . . . . . . . . Backslashes and raw strings . . . . . . . . . . . . . Character groups and other backslash magic . . . Nongreedy matching . . . . . . . . . . . . . . . . . . Extensions . . . . . . . . . . . . . . . . . . . . . . . . Creating and Using Regular Expression Objects . . . . . Using regular expression objects . . . . . . . . . . . Applying regular expressions without compiling . Using Match Objects . . . . . . . . . . . . . . . . . . . . . group([groupid,...]) . . . . . . . . . . . . . . . . . . groups([nomatch]) . . . . . . . . . . . . . . . . . . . groupdict([nomatch]) . . . . . . . . . . . . . . . . . start([groupid]), end([groupid]), span([groupid]) re,string,pos,endpos, . . . . . . . . . . . . . . . . . Treating Strings as Files . . . . . . . . . . . . . . . . . . . Encoding Text . . . . . . . . . . . . . . . . . . . . . . . . . Using Unicode strings . . . . . . . . . . . . . . . . . Reading and writing non-ASCII strings . . . . . . . . Using the Unicode database . . . . . . . . . . . . . Formatting Floating Point Numbers . . . . . . . . . . . . fix(number,precision) . . . . . . . . . . . . . . . . . sci(number,precision) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 137 138 138 139 140 140 142 142 143 143 144 145 147 147 148 148 148 148 149 149 151 151 151 153 154 154 154 Chapter 10: Working with Files and Directories . . . . . . . . . . . . 155 Retrieving File and Directory Information . . . . . . The piecemeal approach . . . . . . . . . . . . The I-want-it-all approach . . . . . . . . . . . . Building and Dissecting Paths . . . . . . . . . . . . . Joining path parts . . . . . . . . . . . . . . . . Breaking paths into pieces . . . . . . . . . . . Other path modifiers . . . . . . . . . . . . . . Listing Directories and Matching File Names . . . . Obtaining Environment and Argument Information Environment variables . . . . . . . . . . . . . Current working directory . . . . . . . . . . . Command-line parameters . . . . . . . . . . . Example: Recursive Grep Utility . . . . . . . . . . . Copying, Renaming, and Removing Paths . . . . . . Copying and linking . . . . . . . . . . . . . . . Renaming . . . . . . . . . . . . . . . . . . . . . Removing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 156 159 161 161 162 162 163 165 165 165 166 166 168 168 168 169 xxi 4807-7 FM.F xxii 5/24/01 8:57 AM Page xxii Python 2.1 Bible Creating Directories and Temporary Files . Comparing Files and Directories . . . . . . Working with File Descriptors . . . . . . . . General file descriptor functions . . . Pipes . . . . . . . . . . . . . . . . . . . Other File Processing Techniques . . . . . Randomly accessing lines in text files Using memory-mapped files . . . . . Iterating over several files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 171 173 173 174 174 174 175 176 Chapter 11: Using Other Operating System Services . . . . . . . . . 179 Executing Shell Commands and Other Programs Spawning Child Processes . . . . . . . . . . . . . popen functions . . . . . . . . . . . . . . . spawn functions . . . . . . . . . . . . . . . fork . . . . . . . . . . . . . . . . . . . . . . . Process management and termination . . Handling Process Information . . . . . . . . . . . Retrieving System Information . . . . . . . . . . Managing Configuration Files . . . . . . . . . . . Understanding Error Names . . . . . . . . . . . . Handling Asynchronous Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 181 181 182 183 183 185 187 188 190 191 Chapter 12: Storing Data and Objects . . . . . . . . . . . . . . . . . . 195 Data Storage Overview . . . . . . . . . . . . . Text versus binary . . . . . . . . . . . . Compression . . . . . . . . . . . . . . . Byte order (“Endianness”) . . . . . . . Object state . . . . . . . . . . . . . . . . Destination . . . . . . . . . . . . . . . . On the receiving end . . . . . . . . . . . Loading and Saving Objects . . . . . . . . . . Pickling with pickle . . . . . . . . . . . The marshal module . . . . . . . . . . . Example: Moving Objects Across a Network Using Database-Like Storage . . . . . . . . . Converting to and from C Structures . . . . . Converting Data to Standard Formats . . . . Sun’s XDR format . . . . . . . . . . . . . Other formats . . . . . . . . . . . . . . . Compressing Data . . . . . . . . . . . . . . . zlib . . . . . . . . . . . . . . . . . . . . . gzip . . . . . . . . . . . . . . . . . . . . . zipfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195 195 196 196 196 196 196 197 197 200 200 203 204 208 208 210 210 211 213 214 4807-7 FM.F 5/24/01 8:57 AM Page xxiii Contents Chapter 13: Accessing Date and Time . . . . . . . . . . . . . . . . . . 219 Telling Time in Python . . . . . . . . . . . . . Ticks . . . . . . . . . . . . . . . . . . . . TimeTuple . . . . . . . . . . . . . . . . . Stopwatch time . . . . . . . . . . . . . . Converting Between Time Formats . . . . . . Parsing and Printing Dates and Times . . . . Fancy formatting . . . . . . . . . . . . . Parsing time . . . . . . . . . . . . . . . . Localization . . . . . . . . . . . . . . . . Accessing the Calendar . . . . . . . . . . . . Printing monthly and yearly calendars Calendar information . . . . . . . . . . Leap years . . . . . . . . . . . . . . . . . Using Time Zones . . . . . . . . . . . . . . . . Allowing Two-Digit Years . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219 219 220 220 221 222 222 223 223 224 224 225 226 226 227 Chapter 14: Using Databases . . . . . . . . . . . . . . . . . . . . . . . 229 Using Disk-Based Dictionaries . . . . . . . . . DBM Example: Tracking Telephone Numbers Advanced Disk-Based Dictionaries . . . . . . dbm . . . . . . . . . . . . . . . . . . . . gdbm . . . . . . . . . . . . . . . . . . . . dbhash . . . . . . . . . . . . . . . . . . . Using BSD database objects . . . . . . Accessing Relational Databases . . . . . . . . Connection objects . . . . . . . . . . . Transactions . . . . . . . . . . . . . . . Cursor objects . . . . . . . . . . . . . . Example: “Sounds-Like” Queries . . . . . . . Examining Relational Metadata . . . . . . . . Example: Creating Auditing Tables . . . . . . Advanced Features of the DB API . . . . . . . Input and output sizes . . . . . . . . . . Reusable SQL statements . . . . . . . . Database library information . . . . . . Error hierarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part III: Networking and the Internet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 229 231 232 232 232 233 233 234 234 234 235 235 237 238 240 241 242 242 243 245 Chapter 15: Networking . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Networking Background . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 247 Working with Addresses and Host Names . . . . . . . . . . . . . . . . . . . 248 xxiii 4807-7 FM.F xxiv 5/24/01 8:57 AM Page xxiv Python 2.1 Bible Communicating with Low-Level Sockets . . . . Creating and destroying sockets . . . . . Connecting sockets . . . . . . . . . . . . Sending and receiving data . . . . . . . . Using socket options . . . . . . . . . . . . Converting numbers . . . . . . . . . . . . Example: A Multicast Chat Application . . . . Using SocketServers . . . . . . . . . . . . . . . The SocketServer family . . . . . . . . . . Request handlers . . . . . . . . . . . . . . Processing Web Browser Requests . . . . . . . BaseHTTPRequestHandler . . . . . . . . SimpleHTTPRequestHandler . . . . . . . CGIHTTPRequestHandler . . . . . . . . . Example: form handler CGI script . . . . Handling Multiple Requests Without Threads asyncore . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250 250 251 252 253 256 256 261 261 263 264 265 266 267 267 269 271 Chapter 16: Speaking Internet Protocols . . . . . . . . . . . . . . . . 275 Python’s Internet Protocol Support . . . Retrieving Internet Resources . . . . . . . Manipulating URLs . . . . . . . . . . Treating a URL as a file . . . . . . . URLopeners . . . . . . . . . . . . . Extended URL opening . . . . . . . Sending HTTP Requests . . . . . . . . . . Building and using request objects Sending and Receiving E-Mail . . . . . . . Accessing POP3 accounts . . . . . . Accessing SMTP accounts . . . . . Accessing IMAP accounts . . . . . . Transferring Files via FTP . . . . . . . . . Retrieving Resources Using Gopher . . . Working with Newsgroups . . . . . . . . . Using the Telnet Protocol . . . . . . . . . Connecting . . . . . . . . . . . . . . Reading and writing . . . . . . . . . Watching and waiting . . . . . . . . Other methods . . . . . . . . . . . . Writing CGI Scripts . . . . . . . . . . . . . Setting up CGI scripts . . . . . . . . Accessing form fields . . . . . . . . Advanced CGI functions . . . . . . . A note on debugging . . . . . . . . . A note on security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 276 276 277 277 278 279 279 281 281 283 285 289 291 292 296 296 296 297 297 298 298 299 301 301 302 4807-7 FM.F 5/24/01 8:57 AM Page xxv Contents Chapter 17: Handling Internet Data . . . . . . . . . . . . . . . . . . . 303 Manipulating URLs . . . . . . . . . . . . . . . . Formatting Text . . . . . . . . . . . . . . . . . . Formatter interface . . . . . . . . . . . . Writer interface . . . . . . . . . . . . . . . Other module resources . . . . . . . . . . Reading Web Spider Robot Files . . . . . . . . Viewing Files in a Web Browser . . . . . . . . . Dissecting E-Mail Messages . . . . . . . . . . . Parsing a message . . . . . . . . . . . . . Retrieving header values . . . . . . . . . Other members . . . . . . . . . . . . . . . Address lists . . . . . . . . . . . . . . . . rfc822 utility functions . . . . . . . . . . . MIME messages . . . . . . . . . . . . . . . Working with MIME Encoding . . . . . . . . . . Encoding and decoding MIME messages Parsing multipart MIME messages . . . . Writing out multipart MIME messages . . Handling document types . . . . . . . . . Encoding and Decoding Message Data . . . . . Uuencode . . . . . . . . . . . . . . . . . . Base64 . . . . . . . . . . . . . . . . . . . . Quoted-printable . . . . . . . . . . . . . . Working with UNIX Mailboxes . . . . . . . . . . Working with MH mailboxes . . . . . . . Using Web Cookies . . . . . . . . . . . . . . . . Cookies . . . . . . . . . . . . . . . . . . . Morsels . . . . . . . . . . . . . . . . . . . Example: a cookie importer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303 304 304 305 306 307 308 309 309 309 310 310 311 311 312 312 313 313 316 317 317 318 319 320 320 321 322 322 323 Chapter 18: Parsing XML and Other Markup Languages . . . . . . . 325 Markup Language Basics . . . . . . . . . . . . Tags are for metatext . . . . . . . . . . Tag rules . . . . . . . . . . . . . . . . . . Namespaces . . . . . . . . . . . . . . . . Processing XML . . . . . . . . . . . . . Parsing HTML Files . . . . . . . . . . . . . . . HTMLParser methods . . . . . . . . . . Handling tags . . . . . . . . . . . . . . . Other parsing methods . . . . . . . . . Handling unknown or bogus elements Example: Bold Only . . . . . . . . . . . . . . . Example: Web Robot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325 326 326 327 327 327 328 328 328 329 330 331 xxv 4807-7 FM.F xxvi 5/24/01 8:57 AM Page xxvi Python 2.1 Bible Parsing XML with SAX . . . . . . . . . . . . . . . . Using a ContentHandler . . . . . . . . . . . . Example: blood-type extractor . . . . . . . . Using parser (XMLReader) objects . . . . . SAX exceptions . . . . . . . . . . . . . . . . . Parsing XML with DOM . . . . . . . . . . . . . . . . DOM nodes . . . . . . . . . . . . . . . . . . . Elements, attributes, and text . . . . . . . . The document node (DOM) . . . . . . . . . . Example: data import and export with DOM Parsing XML with xmllib . . . . . . . . . . . . . . . Elements and attributes . . . . . . . . . . . . XML handlers . . . . . . . . . . . . . . . . . . Other XMLParser members . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part IV: User Interfaces and Multimedia . . . . . . . . . . . . . . 334 334 335 336 337 338 338 338 339 339 341 342 343 343 345 Chapter 19: Tinkering with Tkinter . . . . . . . . . . . . . . . . . . . . 347 Getting Your Feet Wet . . . . . . . . . . Creating a GUI . . . . . . . . . . . . . . . Building an interface with widgets Widget options . . . . . . . . . . . Laying Out Widgets . . . . . . . . . . . . Packer options . . . . . . . . . . . Grid options . . . . . . . . . . . . . Example: Breakfast Buttons . . . . . . . Using Common Options . . . . . . . . . Color options . . . . . . . . . . . . Size options . . . . . . . . . . . . . Appearance options . . . . . . . . Behavior options . . . . . . . . . . Gathering User Input . . . . . . . . . . . Example: Printing Fancy Text . . . . . . Using Text Widgets . . . . . . . . . . . . Building Menus . . . . . . . . . . . . . . Using Tkinter Dialogs . . . . . . . . . . . File dialogs . . . . . . . . . . . . . Example: Text Editor . . . . . . . . . . . Handling Colors and Fonts . . . . . . . . Colors . . . . . . . . . . . . . . . . Fonts . . . . . . . . . . . . . . . . . Drawing Graphics . . . . . . . . . . . . . The canvas widget . . . . . . . . . Manipulating canvas items . . . . Using Timers . . . . . . . . . . . . . . . . Example: A Bouncing Picture . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 348 348 349 349 350 351 352 354 354 355 355 355 356 357 359 360 361 362 362 365 365 366 366 366 367 368 368 4807-7 FM.F 5/24/01 8:57 AM Page xxvii Contents Chapter 20: Using Advanced Tkinter Widgets . . . . . . . . . . . . . 371 Handling Events . . . . . . . . . . . . . Creating event handlers . . . . . Binding mouse events . . . . . . Binding keyboard events . . . . Event objects . . . . . . . . . . . Example: A Drawing Canvas . . . . . . Advanced Widgets . . . . . . . . . . . Listbox . . . . . . . . . . . . . . . Scale . . . . . . . . . . . . . . . . Scrollbar . . . . . . . . . . . . . . Example: Color Scheme Customizer . Creating Dialogs . . . . . . . . . . . . . Supporting Drag-and-Drop Operations Using Cursors . . . . . . . . . . . . . . Designing New Widgets . . . . . . . . Further Tkinter Adventures . . . . . . Additional widgets . . . . . . . . Learning more . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 371 371 372 372 373 373 375 375 376 376 377 381 382 385 387 389 389 389 Chapter 21: Building User Interfaces with wxPython . . . . . . . . . 391 Introducing wxPython . . . . . . . . . . . . . Creating Simple wxPython Programs . . . . . Choosing Different Window Types . . . . . . Managed windows . . . . . . . . . . . . Nonmanaged windows . . . . . . . . . . Using wxPython Controls . . . . . . . . . . . Common controls . . . . . . . . . . . . Tree controls . . . . . . . . . . . . . . . Editor controls . . . . . . . . . . . . . . Controlling Layout . . . . . . . . . . . . . . . Specifying coordinates . . . . . . . . . Sizers . . . . . . . . . . . . . . . . . . . . Layout constraints . . . . . . . . . . . . Layout algorithms . . . . . . . . . . . . Using Built-in Dialogs . . . . . . . . . . . . . . Drawing with Device Contexts . . . . . . . . Adding Menus and Keyboard Shortcuts . . . Accessing Mouse and Keyboard Input . . . . Other wxPython Features . . . . . . . . . . . Clipboard, drag and drop, and cursors Graphics . . . . . . . . . . . . . . . . . . Date and time . . . . . . . . . . . . . . . Fonts . . . . . . . . . . . . . . . . . . . . HTML . . . . . . . . . . . . . . . . . . . Printing . . . . . . . . . . . . . . . . . . Other . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391 392 394 394 395 399 399 400 401 401 402 403 406 407 407 408 411 412 412 413 413 413 413 414 414 414 xxvii 4807-7 FM.F xxviii 5/24/01 8:57 AM Page xxviii Python 2.1 Bible Chapter 22: Using Curses . . . . . . . . . . . . . . . . . . . . . . . . . . 415 A Curses Overview . . . . . . . . . . . . . . . . . . Starting Up and Shutting Down . . . . . . . . . . . Displaying and Erasing Text . . . . . . . . . . . . . Reading from the window (screen-scraping) Erasing . . . . . . . . . . . . . . . . . . . . . . Refreshing . . . . . . . . . . . . . . . . . . . . Boxes and lines . . . . . . . . . . . . . . . . . The window background . . . . . . . . . . . Example: masking a box . . . . . . . . . . . . Moving the Cursor . . . . . . . . . . . . . . . . . . Getting User Input . . . . . . . . . . . . . . . . . . Reading keys . . . . . . . . . . . . . . . . . . Other keyboard-related functions . . . . . . Fancy characters . . . . . . . . . . . . . . . . Reading mouse input . . . . . . . . . . . . . Example: yes, no, or maybe . . . . . . . . . . Managing Windows . . . . . . . . . . . . . . . . . . Pads . . . . . . . . . . . . . . . . . . . . . . . Stacking windows . . . . . . . . . . . . . . . Editing Text . . . . . . . . . . . . . . . . . . . . . . Using Color . . . . . . . . . . . . . . . . . . . . . . Numbering . . . . . . . . . . . . . . . . . . . Setting colors . . . . . . . . . . . . . . . . . . Tweaking the colors . . . . . . . . . . . . . . Example: A Simple Maze Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 415 416 416 417 418 418 418 418 419 420 421 422 422 422 423 424 425 425 426 426 427 427 428 428 428 Chapter 23: Building Simple Command Interpreters . . . . . . . . . 433 Beginning with the End in Mind . . . Understanding the Lepto Language Creating a Lepto Lexical Analyzer . The shlex module . . . . . . . Putting shlex to work . . . . . Adding Interactive-Mode Features . Using the cmd module . . . . . Subclassing cmd.Cmd . . . . . Executing Lepto Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433 435 436 436 437 440 440 442 445 Chapter 24: Playing Sound . . . . . . . . . . . . . . . . . . . . . . . . . 453 Sound File Basics . . . . . . . . . . . . . . . . Playing Sounds . . . . . . . . . . . . . . . . . Playing sound on Windows . . . . . . . Playing and recording sound on SunOS Examining Audio Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453 454 454 455 456 4807-7 FM.F 5/24/01 8:57 AM Page xxix Contents Reading and Writing Audio Files . . . . . . . . Reading and writing AIFF files with aifc . Reading and writing AU files with sunau Reading and writing WAV files with wave Example: Reversing an audio file . . . . . Reading IFF chunked data . . . . . . . . . Handling Raw Audio Data . . . . . . . . . . . . Examining a fragment . . . . . . . . . . . Searching and matching . . . . . . . . . . Translating between storage formats . . Manipulating fragments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part V: Advanced Python Programming . . . . . . . . . . . 456 457 458 458 458 460 461 461 462 462 463 465 Chapter 25: Processing Images . . . . . . . . . . . . . . . . . . . . . . 467 Image Basics . . . . . . . . . . . . . . . . . . . . Identifying Image File Types . . . . . . . . . . . Converting Between Color Systems . . . . . . Color systems . . . . . . . . . . . . . . . . Converting from one system to another Handling Raw Image Data . . . . . . . . . . . . Using the Python Imaging Library . . . . . . . Retrieving image information . . . . . . . Copying and converting images . . . . . Using PIL with Tkinter . . . . . . . . . . . Cropping and resizing images . . . . . . Modifying pixel data . . . . . . . . . . . . Other PIL features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 467 468 469 469 470 472 472 473 474 475 476 476 480 Chapter 26: Multithreading . . . . . . . . . . . . . . . . . . . . . . . . 481 Understanding Threads . . . . . . . . . . . . . . . . . . . . . . . Spawning, Tracking, and Killing Threads . . . . . . . . . . . . . Creating threads with the thread module . . . . . . . . . . Starting and stopping threads with the threading module Thread status and information under threading . . . . . . Finding threads under threading . . . . . . . . . . . . . . . Waiting for a thread to finish . . . . . . . . . . . . . . . . . Avoiding Concurrency Issues . . . . . . . . . . . . . . . . . . . . Locking with thread . . . . . . . . . . . . . . . . . . . . . . Locking with threading . . . . . . . . . . . . . . . . . . . . Preventing Deadlock . . . . . . . . . . . . . . . . . . . . . . . . . Example: Downloading from Multiple URLs . . . . . . . . . . . . Porting Threaded Code . . . . . . . . . . . . . . . . . . . . . . . Weaving Threads Together with Queues . . . . . . . . . . . . . . Technical Note: How Simultaneous Is Simultaneous? . . . . . . For More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 481 482 482 483 484 484 484 485 485 486 488 489 494 495 495 496 xxix 4807-7 FM.F xxx 5/24/01 8:57 AM Page xxx Python 2.1 Bible Chapter 27: Debugging, Profiling, and Optimization . . . . . . . . . 497 Debugging Python Code . . . . . . . . . . . . . Starting and stopping the debugger . . . Examining the state of things . . . . . . . Setting breakpoints . . . . . . . . . . . . Running . . . . . . . . . . . . . . . . . . . Aliases . . . . . . . . . . . . . . . . . . . . Debugging tips . . . . . . . . . . . . . . . Working with docstrings . . . . . . . . . . . . . Automating Tests . . . . . . . . . . . . . . . . . Synching docstrings with code . . . . . . Unit testing . . . . . . . . . . . . . . . . . Finding Bottlenecks . . . . . . . . . . . . . . . . Profiling code . . . . . . . . . . . . . . . . Using Profile objects . . . . . . . . . . . . Calibrating the profiler . . . . . . . . . . Customizing statistics . . . . . . . . . . . Common Optimization Tricks . . . . . . . . . . Sorting . . . . . . . . . . . . . . . . . . . . Looping . . . . . . . . . . . . . . . . . . . I/O . . . . . . . . . . . . . . . . . . . . . . Strings . . . . . . . . . . . . . . . . . . . . Threads . . . . . . . . . . . . . . . . . . . Taking out the Trash — the Garbage Collector Reference counts and Python code . . . Reference counts and C/C++ code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497 497 498 499 500 500 500 501 502 502 503 505 505 506 507 507 509 509 510 510 511 511 512 512 513 Chapter 28: Security and Encryption . . . . . . . . . . . . . . . . . . . 515 Checking Passwords . . . . . . . . . . Running in a Restricted Environment The rexec sandbox . . . . . . . . Using a class fortress . . . . . . Creating Message Fingerprints . . . . MD5 . . . . . . . . . . . . . . . . SHA . . . . . . . . . . . . . . . . . Other uses . . . . . . . . . . . . . Using 1940s-Era Encryption . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 515 516 517 520 521 522 522 523 523 Chapter 29: Writing Extension Modules . . . . . . . . . . . . . . . . . 527 Extending and Embedding Overview . Writing a Simple Extension Module . . Building and Linking . . . . . . . . . . Converting Python Data to C . . . . . Unpacking normal arguments . Using special format characters Unpacking keyword arguments . Unpacking zero arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 527 528 531 532 532 535 537 538 4807-7 FM.F 5/24/01 8:57 AM Page xxxi Contents Converting C Data to Python . . . . . . Creating simple Python objects . Creating complex Python objects Embedding the Interpreter . . . . . . . A simple example . . . . . . . . . . Shutting down . . . . . . . . . . . Other setup functions . . . . . . . System information functions . . Running Python Code from C . . . . . . Using Extension Tools . . . . . . . . . . SWIG . . . . . . . . . . . . . . . . . CXX . . . . . . . . . . . . . . . . . . Extension classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 538 539 540 541 541 541 542 542 543 546 546 549 550 Chapter 30: Embedding the Python Interpreter . . . . . . . . . . . . 553 Tracking Reference Counts . . . . . . . . . . . . Types of reference ownership . . . . . . . Reference conventions . . . . . . . . . . . Common pitfalls . . . . . . . . . . . . . . . Using the Abstract and Concrete Object Layers Object layers . . . . . . . . . . . . . . . . . Working with generic objects . . . . . . . . Working with Number Objects . . . . . . . . . . Any numerical type . . . . . . . . . . . . . Integers . . . . . . . . . . . . . . . . . . . . Longs . . . . . . . . . . . . . . . . . . . . . Floating-point numbers . . . . . . . . . . . Complex numbers . . . . . . . . . . . . . . Working with Sequence Objects . . . . . . . . . Any sequence type . . . . . . . . . . . . . . Strings . . . . . . . . . . . . . . . . . . . . . Lists . . . . . . . . . . . . . . . . . . . . . . Tuples . . . . . . . . . . . . . . . . . . . . . Buffers . . . . . . . . . . . . . . . . . . . . . Unicode strings . . . . . . . . . . . . . . . . Working with Mapping Objects . . . . . . . . . . Functions for any mapping type . . . . . . Dictionaries . . . . . . . . . . . . . . . . . . Using Other Object Types . . . . . . . . . . . . . Type . . . . . . . . . . . . . . . . . . . . . . None . . . . . . . . . . . . . . . . . . . . . . File . . . . . . . . . . . . . . . . . . . . . . . Module . . . . . . . . . . . . . . . . . . . . . CObjects . . . . . . . . . . . . . . . . . . . . Creating Threads and Sub-Interpreters . . . . . Threads . . . . . . . . . . . . . . . . . . . . Sub-interpreters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 553 553 554 555 555 556 556 558 558 560 560 561 561 561 562 563 564 565 566 567 569 569 570 571 571 571 571 572 574 574 575 576 xxxi 4807-7 FM.F xxxii 5/24/01 8:57 AM Page xxxii Python 2.1 Bible Handling Errors and Exceptions . Checking for errors . . . . . Signaling error conditions . . Creating custom exceptions Raising warnings . . . . . . . Managing Memory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 576 577 577 578 578 579 Chapter 31: Number Crunching . . . . . . . . . . . . . . . . . . . . . . 581 Using Math Routines . . . . . . . . . . . . . . . . . . . . . . . . . . . Rounding and fractional parts . . . . . . . . . . . . . . . . . . General math routines . . . . . . . . . . . . . . . . . . . . . . . Logarithms and exponentiation . . . . . . . . . . . . . . . . . Trigonometric functions . . . . . . . . . . . . . . . . . . . . . . Computing with Complex Numbers . . . . . . . . . . . . . . . . . . Generating Random Numbers . . . . . . . . . . . . . . . . . . . . . . Random numbers . . . . . . . . . . . . . . . . . . . . . . . . . Example: shuffling a deck . . . . . . . . . . . . . . . . . . . . . Random distributions . . . . . . . . . . . . . . . . . . . . . . . Example: plotting distributions using Monte Carlo sampling Using Arbitrary-Precision Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 581 581 582 582 582 583 583 583 585 585 586 587 Chapter 32: Using NumPy . . . . . . . . . . . . . . . . . . . . . . . . . 589 Introducing Numeric Python . . . . . . . . Installing NumPy . . . . . . . . . . . . Some quick definitions . . . . . . . . Meet the array . . . . . . . . . . . . . Accessing and Slicing Arrays . . . . . . . . Contiguous arrays . . . . . . . . . . . Converting arrays to lists and strings Calling Universal Functions . . . . . . . . . Ufunc destinations . . . . . . . . . . . Example: editing an audio stream . . Repeating ufuncs . . . . . . . . . . . . Creating Arrays . . . . . . . . . . . . . . . . Array creation functions . . . . . . . Seeding arrays with functions . . . . Using Element Types . . . . . . . . . . . . . Reshaping and Resizing Arrays . . . . . . . Using Other Array Functions . . . . . . . . sort(array,[axis=-1]) . . . . . . . . . . where(condition,X,Y) . . . . . . . . . swapaxes(array,axis1,axis2) . . . . . Matrix operations . . . . . . . . . . . Array Example: Analyzing Price Trends . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 589 589 590 590 590 592 592 593 594 594 595 597 597 598 600 600 601 601 602 602 602 603 4807-7 FM.F 5/24/01 8:57 AM Page xxxiii Contents Chapter 33: Parsing and Interpreting Python Code . . . . . . . . . . 605 Examining Tracebacks . . . . . . . . . . . . . . . . . Printing a traceback — print_exc and friends Extracting and formatting exceptions . . . . . Example: reporting exceptions in a GUI . . . . Eating arbitrary exceptions is bad for you . . Introspection . . . . . . . . . . . . . . . . . . . . . . Review: basic introspection . . . . . . . . . . . Browsing classes . . . . . . . . . . . . . . . . . Browsing function information . . . . . . . . . Checking Indentation . . . . . . . . . . . . . . . . . . Tokenizing Python Code . . . . . . . . . . . . . . . . Example: Syntax-Highlighting Printer . . . . . . . . Inspecting Python Parse Trees . . . . . . . . . . . . Creating an AST . . . . . . . . . . . . . . . . . . ASTs and sequences . . . . . . . . . . . . . . . Using ASTs . . . . . . . . . . . . . . . . . . . . Low-Level Object Creation . . . . . . . . . . . . . . . Disassembling Python Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part VI: Deploying Python Applications . . . . . . . . . . . . . . . . . . 605 605 606 607 607 608 608 609 609 611 611 612 613 613 614 614 614 615 617 Chapter 34: Creating Worldwide Applications . . . . . . . . . . . . . 619 Internationalization and Localization . . . . . Preparing Applications for Multiple Languages An NLS example . . . . . . . . . . . . . . What it all means . . . . . . . . . . . . . . Formatting Locale-Specific Output . . . . . . . Changing the locale . . . . . . . . . . . . Locale-specific formatting . . . . . . . . . Properties of locales . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 619 620 620 623 624 624 625 626 Chapter 35: Customizing Import Behavior . . . . . . . . . . . . . . . 629 Understanding Module Importing . . . . . Finding and Loading Modules with imp . . Importing Encrypted Modules . . . . . . . Retrieving Modules from a Remote Source Subclassing Importer . . . . . . . . . Creating the remote Importer . . . . Testing the remote Importer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 629 631 633 636 636 637 640 xxxiii 4807-7 FM.F xxxiv 5/24/01 8:57 AM Page xxxiv Python 2.1 Bible Chapter 36: Distributing Modules and Applications . . . . . . . . . 643 Understanding distutils . . . . . . . . . . Creating a simple distribution . . . Installing the simple distribution . . Other distutils Features . . . . . . . . . . Distributing packages . . . . . . . . Including other files . . . . . . . . . Customizing setup . . . . . . . . . . Distributing Extension Modules . . . . . . Creating Source and Binary Distributions Source distributions . . . . . . . . . Binary distributions . . . . . . . . . Installers . . . . . . . . . . . . . . . . Building Standalone Executables . . . . . py2exe . . . . . . . . . . . . . . . . . Freeze . . . . . . . . . . . . . . . . . Other tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Part VII: Platform-Specific Support . . . . . . . . . . . . . . . . 643 644 645 647 647 648 650 650 651 652 653 653 655 655 656 657 659 Chapter 37: Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661 Using win32all . . . . . . . . . . . . . . . . . . . . . . . . . Data types . . . . . . . . . . . . . . . . . . . . . . . . Error handling . . . . . . . . . . . . . . . . . . . . . Finding what you need . . . . . . . . . . . . . . . . . Example: Using Some Windows APIs . . . . . . . . . . . . Accessing the Windows Registry . . . . . . . . . . . . . . Accessing the registry with win32all . . . . . . . . . Example: setting the Internet Explorer home page Creating, deleting, and navigating keys . . . . . . . Example: recursive deletion of a key . . . . . . . . Other registry functions . . . . . . . . . . . . . . . . Accessing the registry with _winreg . . . . . . . . . Using msvcrt Goodies . . . . . . . . . . . . . . . . . . . . Console I/O . . . . . . . . . . . . . . . . . . . . . . . Other functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 661 661 662 662 662 664 664 666 666 667 668 668 669 669 670 Chapter 38: UNIX-Compatible Modules . . . . . . . . . . . . . . . . . 671 Checking UNIX Passwords and Groups . . . . Accessing the System Logger . . . . . . . . . . Calling Shared Library Functions . . . . . . . . Providing Identifier and Keyword Completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 671 673 675 675 4807-7 FM.F 5/24/01 8:57 AM Page xxxv Contents Retrieving File System and Resource Information File system information . . . . . . . . . . . . Resource usage . . . . . . . . . . . . . . . . . Resource limits . . . . . . . . . . . . . . . . . Controlling File Descriptors . . . . . . . . . . . . . Handling Terminals and Pseudo-Terminals . . . . Interfacing with Sun’s NIS “Yellow Pages” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 677 678 678 679 680 681 682 Appendix A: Online Resources . . . . . . . . . . . . . . . . . . . . . . 685 Appendix B: Python Development Environments . . . . . . . . . . . 689 Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701 xxxv