Python Library Reference Release 2.1.1
Total Page:16
File Type:pdf, Size:1020Kb
Python Library Reference Release 2.1.1 Guido van Rossum Fred L. Drake, Jr., editor July 20, 2001 PythonLabs E-mail: [email protected] Copyright c 2001 Python Software Foundation. All rights reserved. Copyright c 2000 BeOpen.com. All rights reserved. Copyright c 1995-2000 Corporation for National Research Initiatives. All rights reserved. Copyright c 1991-1995 Stichting Mathematisch Centrum. All rights reserved. See the end of this document for complete license and permissions information. Abstract Python is an extensible, interpreted, object-oriented programming language. It supports a wide range of applications, from simple text processing scripts to interactive WWW browsers. While the Python Reference Manual describes the exact syntax and semantics of the language, it does not describe the standard library that is distributed with the language, and which greatly enhances its immediate usability. This library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming. Some of these modules are explicitly designed to encourage and enhance the portability of Python programs. This library reference manual documents Python’s standard library, as well as many optional library modules (which may or may not be available, depending on whether the underlying platform supports them and on the configuration choices made at compile time). It also documents the standard types of the language and its built-in functions and exceptions, many of which are not or incompletely documented in the Reference Manual. This manual assumes basic knowledge about the Python language. For an informal introduction to Python, see the Python Tutorial; the Python Reference Manual remains the highest authority on syntactic and semantic questions. Finally, the manual entitled Extending and Embedding the Python Interpreter describes how to add new extensions to Python and how to embed it in other applications. CONTENTS 1 Introduction 1 2 Built-in Types, Exceptions and Functions 3 2.1 Built-in Types .............................................. 3 2.2 Built-in Exceptions ............................................ 16 2.3 Built-in Functions ............................................ 20 3 Python Runtime Services 29 3.1 sys — System-specific parameters and functions ........................... 29 3.2 gc — Garbage Collector interface .................................... 34 3.3 weakref — Weak references ...................................... 35 3.4 fpectl — Floating point exception control .............................. 39 3.5 atexit — Exit handlers ........................................ 40 3.6 types — Names for all built-in types ................................. 41 3.7 UserDict — Class wrapper for dictionary objects .......................... 43 3.8 UserList — Class wrapper for list objects .............................. 43 3.9 UserString — Class wrapper for string objects ........................... 44 3.10 operator — Standard operators as functions. ............................. 44 3.11 inspect — Inspect live objects .................................... 47 3.12 traceback — Print or retrieve a stack traceback ........................... 52 3.13 linecache — Random access to text lines .............................. 53 3.14 pickle — Python object serialization ................................. 54 3.15 cPickle — Alternate implementation of pickle .......................... 59 3.16 copy reg — Register pickle support functions .......................... 59 3.17 shelve — Python object persistence .................................. 59 3.18 copy — Shallow and deep copy operations ............................... 60 3.19 marshal — Alternate Python object serialization ........................... 61 3.20 warnings — Warning control ..................................... 62 3.21 imp — Access the import internals .................................. 64 3.22 code — Interpreter base classes .................................... 67 3.23 codeop — Compile Python code .................................... 69 3.24 pprint — Data pretty printer ..................................... 69 3.25 repr — Alternate repr() implementation .............................. 71 3.26 new — Creation of runtime internal objects ............................... 73 3.27 site — Site-specific configuration hook ................................ 73 3.28 user — User-specific configuration hook ............................... 74 3.29 builtin — Built-in functions .................................. 75 3.30 main — Top-level script environment .............................. 75 i 4 String Services 77 4.1 string — Common string operations ................................. 77 4.2 re — Regular expression operations .................................. 80 4.3 struct — Interpret strings as packed binary data ........................... 88 4.4 difflib — Helpers for computing deltas ............................... 90 4.5 fpformat — Floating point conversions ............................... 94 4.6 StringIO — Read and write strings as files .............................. 94 4.7 cStringIO — Faster version of StringIO ............................. 95 4.8 codecs — Codec registry and base classes .............................. 95 4.9 unicodedata — Unicode Database ................................. 100 5 Miscellaneous Services 101 5.1 doctest — Test docstrings represent reality ............................. 101 5.2 unittest — Unit testing framework ................................. 108 5.3 math — Mathematical functions .................................... 117 5.4 cmath — Mathematical functions for complex numbers . 119 5.5 random — Generate pseudo-random numbers ............................. 120 5.6 whrandom — Pseudo-random number generator . 123 5.7 bisect — Array bisection algorithm ................................. 124 5.8 array — Efficient arrays of numeric values .............................. 125 5.9 ConfigParser — Configuration file parser ............................. 127 5.10 fileinput — Iterate over lines from multiple input streams . 129 5.11 xreadlines — Efficient iteration over a file ............................. 131 5.12 calendar — General calendar-related functions . 131 5.13 cmd — Support for line-oriented command interpreters . 132 5.14 shlex — Simple lexical analysis .................................... 134 6 Generic Operating System Services 137 6.1 os — Miscellaneous OS interfaces ................................... 137 6.2 os.path — Common pathname manipulations ............................ 149 6.3 dircache — Cached directory listings ................................ 151 6.4 stat — Interpreting stat() results .................................. 151 6.5 statcache — An optimization of os.stat() . 153 6.6 statvfs — Constants used with os.statvfs() . 154 6.7 filecmp — File and Directory Comparisons ............................. 154 6.8 popen2 — Subprocesses with accessible I/O streams . 156 6.9 time — Time access and conversions ................................. 157 6.10 sched — Event scheduler ....................................... 161 6.11 mutex — Mutual exclusion support .................................. 162 6.12 getpass — Portable password input .................................. 163 6.13 curses — Terminal handling for character-cell displays . 163 6.14 curses.textpad — Text input widget for curses programs . 178 6.15 curses.wrapper — Terminal handler for curses programs . 179 6.16 curses.ascii — Utilities for ASCII characters . 179 6.17 curses.panel — A panel stack extension for curses. 182 6.18 getopt — Parser for command line options .............................. 183 6.19 tempfile — Generate temporary file names ............................. 185 6.20 errno — Standard errno system symbols ............................... 185 6.21 glob —UNIX style pathname pattern expansion ............................ 191 6.22 fnmatch —UNIX filename pattern matching ............................. 192 6.23 shutil — High-level file operations .................................. 192 6.24 locale — Internationalization services ................................ 194 6.25 gettext — Multilingual internationalization services . 198 ii 7 Optional Operating System Services 207 7.1 signal — Set handlers for asynchronous events . 207 7.2 socket — Low-level networking interface .............................. 209 7.3 select — Waiting for I/O completion ................................. 214 7.4 thread — Multiple threads of control ................................. 215 7.5 threading — Higher-level threading interface ............................ 217 7.6 Queue — A synchronized queue class ................................. 223 7.7 mmap — Memory-mapped file support ................................. 224 7.8 anydbm — Generic access to DBM-style databases . 225 7.9 dumbdbm — Portable DBM implementation .............................. 226 7.10 dbhash — DBM-style interface to the BSD database library . 226 7.11 whichdb — Guess which DBM module created a database . 227 7.12 bsddb — Interface to Berkeley DB library ............................... 228 7.13 zlib — Compression compatible with gzip .............................. 229 7.14 gzip — Support for gzip files ..................................... 231 7.15 zipfile — Work with ZIP archives .................................. 232 7.16 readline — GNU readline interface ................................