PYTHON IN A NUTSHELL Second Edition Alex Martelli O'REILLY Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo
iii Table of Contents Preface ix Part 1. Getting Started with Python 1. Introduction to Python 3 The Python Language 3 The Python Standard Library and Extension Modules 5 Python Implementations 5 Python Development and Versions 8 Python Resources 9 2. Installation 14 Installing Python from Source Code 14 Installing Python from Binaries 18 Installing Jython 20 Installing IronPython 21 3. The Python Interpreter 22 The python Program 22 Python Development Environments 26 Running Python Programs 28 The jython Interpreter 29 The IronPython Interpreter 30
iv I Tal* of Contents Part II. Core Python Language and Built-ins 4. The Python Language 33 Lexical Structure 33 Data Types 38 Variables and Other References 46 Expressions and Operators 50 Numeric Operations 52 Sequence Operations 53 Set Operations 58 Dictionary Operation 59 The print Statement 61 Control Flow Statements 62 Functions 70 5. Object-Oriented Python 81 Classes and Instances 82 Special Methods 104 Decorators 115 Metaclasses 116 6. Exceptions 121 The try Statement 121 Exception Propagation 126 The raise Statement 128 Exception Objects 129 Custom Exception Classes 132 Error-Checking Strategies 134 7. Modules 139 Module Objects 139 Module Loading 144 Packages 149 The Distribution Utilities (distutils) 150 8. Core Built-ins 153 Built-in Types 154 Built-in Functions 158 The sys Module 168 The copy Module 172 The collections Module 173
Table of Contents 1 v The functional Module 175 The bisect Module 176 The heapq Module 177 The UserDict Module 178 The optparse Module 179 The itertools Module 183 9. Strings and Regular Expressions 186 Methods of String Objects 186 The string Module 191 String Formatting 193 The pprint Module 197 The repr Module 198 Unicode 198 Regular Expressions and the re Module 201 Part III. Python Library and Extension Modules 10. File and Text Operations 215 Other Chapters That Also Deal with Files 215 Organization of This Chapter 215 File Objects 216 Auxiliary Modules for File I/O 224 The StringlO and cstringlo Modules 229 Compressed Files 230 The os Module 240 Filesystem Operations 241 Text Input and Output 256 Richer-Text I/O 258 Interactive Command Sessions 265 Internationalization 269 11. Persistence and Databases 277 Serialization 278 DBM Modules 285 Berkeley DB Interfacing 288 The Python Database API (DBAPI) 2.0 292
vi I Table nf Cnntents 12. Time Operations 302 The time Module 302 The datetime Module 306 The pytz Module 313 The dateutil Module 313 The sched Module 316 The calendar Module 317 The mx.datetime Module 319 13. Controlling Execution 328 Dynamic Execution and the exec Statement 328 Internal Types 331 Garbage Collection 332 Termination Functions 337 Site and User Customization 338 14. Threads and Processes 340 Threads in Python 341 The thread Module 341 The Queue Module 342 The threading Module 344 Threaded Program Architecture 350 Process Environment 353 Running Other Programs 354 The mmap Module 360 15. Numeric Processing 365 The math and cmath Modules 365 The Operator Module 368 Random and Pseudorandom Numbers 370 The decimal Module 372 The gmpy Module 373 16. Array Processing 375 The array Module 375 Extensions for Numeric Array Computation 377 The Numeric Package 378 Array Objects 378 Universal Functions (ufuncs) 399 Auxiliary Numeric Modules 403
Table of Contents I vii 17. Tkinter GUIs 405 Tkinter Fundamentals 406 Widget Fundamentals 408 Commonly Used Simple Widgets 415 Container Widgets 420 Menus 423 The Text Widget 426 The Canvas Widget 436 Layout Management 442 Tkinter Events 446 18. Testing, Debugging, and Optimizing 451 Testing 452 Debugging 461 The warnings Module 471 Optimization 474 Part IV. Network and Web Programming 19. Client-Side Network Protocol Modules 493 URL Access 493 Email Protocols 503 The HTTP and FTP Protocols 506 Network News 511 Telnet 515 Distributed Computing 517 Other Protocols 519 20. Sockets and Server-Side Network Protocol Modules 520 The socket Module 521 The SocketServer Module 528 Event-Driven Socket Programs 533 21. CGI Scripting and Alternatives 545 CGI in Python 546 Cookies 553 Other Server-Side Approaches 557
22. MIME and Network Encodings 561 Encoding Binary Data as Text 561 MIME and Email Format Handling 564 23. Structured Text: HTML 575 The sgmllib Module 576 The htmllib Module 580 The HTMLParser Module 583 The BeautifulSoup Extension 585 Generating HTML 586 24. Structured Text: XML 591 An Overview of XML Parsing 592 Parsing XML with SAX 593 Parsing XML with DOM 598 Changing and Generating XML 606 Part V. Extending and Embedding 25. Extending and Embedding Classic Python 613 Extending Python with Python's C API 614 Extending Python Without Python's C API 645 Embedding Python 647 Pyrex 650 26. Extending and Embedding Jython 655 Importing Java Packages in Jython 656 Embedding Jython in Java 659 Compiling Python into Java 662 27. Distributing Extensions and Programs 666 Python's distutils 666 py2exe 675 py2app 676 cx_freeze 676 Pylnstaller 676 Index 677 viii I Table of Contents