Python - Core

Transcription

PYTHONIN A NUTSHELLSecond EditionAlex MartelliO'REILLY Beijing Cambridge Farnham Köln Sebastopol Taipei Tokyo

Table of ContentsPrefaceixPart 1. Getting Started with Python1. Introduction to PythonThe Python LanguageThe Python Standard Library and Extension ModulesPython ImplementationsPython Development and VersionsPython Resources2. InstallationInstalling Python from Source CodeInstalling Python from BinariesInstalling JythonInstalling IronPython3. The Python InterpreterThe python ProgramPython Development EnvironmentsRunning Python ProgramsThe jython InterpreterThe IronPython Interpreter3355891414182021222226282930iii

Part II. Core Python Language and Built-ins4. The Python LanguageLexical StructureData TypesVariables and Other ReferencesExpressions and OperatorsNumeric OperationsSequence OperationsSet OperationsDictionary OperationThe print StatementControl Flow StatementsFunctions5. Object-Oriented PythonClasses and InstancesSpecial MethodsDecoratorsMetaclasses6. ExceptionsThe try StatementException PropagationThe raise StatementException ObjectsCustom Exception ClassesError-Checking Strategies7. ModulesModule ObjectsModule LoadingPackagesThe Distribution Utilities (distutils)8. Core Built-insBuilt-in TypesBuilt-in FunctionsThe sys ModuleThe copy ModuleThe collections Moduleiv ITal* of 1126128129132134139139144149150153154158168172173

The functional ModuleThe bisect ModuleThe heapq ModuleThe UserDict ModuleThe optparse ModuleThe itertools Module9. Strings and Regular ExpressionsMethods of String ObjectsThe string ModuleString FormattingThe pprint ModuleThe repr ModuleUnicodeRegular Expressions and the re rt III. Python Library and Extension Modules10.File and Text OperationsOther Chapters That Also Deal with FilesOrganization of This ChapterFile ObjectsAuxiliary Modules for File I/OThe StringlO and cStringlO ModulesCompressed FilesThe os ModuleFilesystem OperationsText Input and OutputRicher-Text I/OInteractive Command SessionsInternationalization11. Persistence and DatabasesSerializationDBM ModulesBerkeley DB InterfacingThe Python Database API (DBAPI) 5288292Table of Contents1 v

12. Time OperationsThe time ModuleThe datetime ModuleThe pytz ModuleThe dateutil ModuleThe sched ModuleThe calendar ModuleThe mx.DateTime Module30230230631331331631731913. Controlling Execution32832833133233733814. Threads and Processes340341341342344350353354360Dynamic Execution and the exec StatementInternal TypesGarbage CollectionTermination FunctionsSite and User CustomizationThreads in PythonThe thread ModuleThe Queue ModuleThe threading ModuleThreaded Program ArchitectureProcess EnvironmentRunning Other ProgramsThe mmap Module15. Numeric ProcessingThe math and cmath ModulesThe Operator ModuleRandom and Pseudorandom NumbersThe decimal ModuleThe gmpy Module16. Array ProcessingThe array ModuleExtensions for Numeric Array ComputationThe Numeric PackageArray ObjectsUniversal Functions (ufuncs)Auxiliary Numeric Modulesvi I Table nf Cnntents365365368370372373375375377378378399403

17. Tkinter GUIsTkinter FundamentalsWidget FundamentalsCommonly Used Simple WidgetsContainer WidgetsMenusThe Text WidgetThe Canvas WidgetLayout ManagementTkinter Events18. Testing, Debugging, and OptimizingTestingDebuggingThe warnings 1452461471474Part IV. Network and Web Programming19. Client-Side Network Protocol ModulesURL AccessEmail ProtocolsThe HTTP and FTP ProtocolsNetwork NewsTelnetDistributed ComputingOther Protocols20. Sockets and Server-Side Network Protocol ModulesThe socket ModuleThe SocketServer ModuleEvent-Driven Socket Programs21. CGI Scripting and AlternativesCGI in PythonCookiesOther Server-Side 46553557Table of Contents I vii

22. MIME and Network EncodingsEncoding Binary Data as TextMIME and Email Format Handling23. Structured Text: HTMLThe sgmllib ModuleThe htmllib ModuleThe HTMLParser ModuleThe BeautifulSoup ExtensionGenerating HTML24. Structured Text: XMLAn Overview of XML ParsingParsing XML with SAXParsing XML with DOMChanging and Generating XML561561564575576580583585586591592593598606Part V. Extending and Embedding25. Extending and Embedding Classic PythonExtending Python with Python's C APIExtending Python Without Python's C APIEmbedding PythonPyrex26. Extending and Embedding JythonImporting Java Packages in JythonEmbedding Jython in JavaCompiling Python into Java27. Distributing Extensionsand ProgramsPython's distutilspy2exepy2appcx FreezePylnstallerIndexviii I Table of 676677

PYTHON IN A NUTSHELL Second Edition . 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 Pythons distutils 666