logo资料库

Python标准库查询手册(最新Python 3.3).pdf

第1页 / 共1626页
第2页 / 共1626页
第3页 / 共1626页
第4页 / 共1626页
第5页 / 共1626页
第6页 / 共1626页
第7页 / 共1626页
第8页 / 共1626页
资料共1626页,剩余部分请下载后查看
Introduction
Built-in Functions
Built-in Constants
Constants added by the site module
Built-in Types
Truth Value Testing
Boolean Operations — and, or, not
Comparisons
Numeric Types — int, float, complex
Iterator Types
Sequence Types — list, tuple, range
Text Sequence Type — str
Binary Sequence Types — bytes, bytearray, memoryview
Set Types — set, frozenset
Mapping Types — dict
Context Manager Types
Other Built-in Types
Special Attributes
Built-in Exceptions
Base classes
Concrete exceptions
Warnings
Exception hierarchy
Text Processing Services
string — Common string operations
re — Regular expression operations
difflib — Helpers for computing deltas
textwrap — Text wrapping and filling
unicodedata — Unicode Database
stringprep — Internet String Preparation
readline — GNU readline interface
rlcompleter — Completion function for GNU readline
Binary Data Services
struct — Interpret bytes as packed binary data
codecs — Codec registry and base classes
Data Types
datetime — Basic date and time types
calendar — General calendar-related functions
collections — Container datatypes
collections.abc — Abstract Base Classes for Containers
heapq — Heap queue algorithm
bisect — Array bisection algorithm
array — Efficient arrays of numeric values
weakref — Weak references
types — Dynamic type creation and names for built-in types
copy — Shallow and deep copy operations
pprint — Data pretty printer
reprlib — Alternate repr() implementation
enum — Support for enumerations
Numeric and Mathematical Modules
numbers — Numeric abstract base classes
math — Mathematical functions
cmath — Mathematical functions for complex numbers
decimal — Decimal fixed point and floating point arithmetic
fractions — Rational numbers
random — Generate pseudo-random numbers
statistics — Mathematical statistics functions
Functional Programming Modules
itertools — Functions creating iterators for efficient looping
functools — Higher-order functions and operations on callable objects
operator — Standard operators as functions
File and Directory Access
pathlib — Object-oriented filesystem paths
os.path — Common pathname manipulations
fileinput — Iterate over lines from multiple input streams
stat — Interpreting stat() results
filecmp — File and Directory Comparisons
tempfile — Generate temporary files and directories
glob — Unix style pathname pattern expansion
fnmatch — Unix filename pattern matching
linecache — Random access to text lines
shutil — High-level file operations
macpath — Mac OS 9 path manipulation functions
Data Persistence
pickle — Python object serialization
copyreg — Register pickle support functions
shelve — Python object persistence
marshal — Internal Python object serialization
dbm — Interfaces to Unix ``databases''
sqlite3 — DB-API 2.0 interface for SQLite databases
Data Compression and Archiving
zlib — Compression compatible with gzip
gzip — Support for gzip files
bz2 — Support for bzip2 compression
lzma — Compression using the LZMA algorithm
zipfile — Work with ZIP archives
tarfile — Read and write tar archive files
File Formats
csv — CSV File Reading and Writing
configparser — Configuration file parser
netrc — netrc file processing
xdrlib — Encode and decode XDR data
plistlib — Generate and parse Mac OS X .plist files
Cryptographic Services
hashlib — Secure hashes and message digests
hmac — Keyed-Hashing for Message Authentication
Generic Operating System Services
os — Miscellaneous operating system interfaces
io — Core tools for working with streams
time — Time access and conversions
argparse — Parser for command-line options, arguments and sub-commands
optparse — Parser for command line options
getopt — C-style parser for command line options
logging — Logging facility for Python
logging.config — Logging configuration
logging.handlers — Logging handlers
getpass — Portable password input
curses — Terminal handling for character-cell displays
curses.textpad — Text input widget for curses programs
curses.ascii — Utilities for ASCII characters
curses.panel — A panel stack extension for curses
platform — Access to underlying platform's identifying data
errno — Standard errno system symbols
ctypes — A foreign function library for Python
Concurrent Execution
threading — Thread-based parallelism
multiprocessing — Process-based parallelism
The concurrent package
concurrent.futures — Launching parallel tasks
subprocess — Subprocess management
sched — Event scheduler
queue — A synchronized queue class
dummy_threading — Drop-in replacement for the threading module
_thread — Low-level threading API
_dummy_thread — Drop-in replacement for the _thread module
Interprocess Communication and Networking
socket — Low-level networking interface
ssl — TLS/SSL wrapper for socket objects
select — Waiting for I/O completion
selectors – High-level I/O multiplexing
asyncio – Asynchronous I/O, event loop, coroutines and tasks
asyncore — Asynchronous socket handler
asynchat — Asynchronous socket command/response handler
signal — Set handlers for asynchronous events
mmap — Memory-mapped file support
Internet Data Handling
email — An email and MIME handling package
json — JSON encoder and decoder
mailcap — Mailcap file handling
mailbox — Manipulate mailboxes in various formats
mimetypes — Map filenames to MIME types
base64 — Base16, Base32, Base64, Base85 Data Encodings
binhex — Encode and decode binhex4 files
binascii — Convert between binary and ASCII
quopri — Encode and decode MIME quoted-printable data
uu — Encode and decode uuencode files
Structured Markup Processing Tools
html — HyperText Markup Language support
html.parser — Simple HTML and XHTML parser
html.entities — Definitions of HTML general entities
XML Processing Modules
xml.etree.ElementTree — The ElementTree XML API
xml.dom — The Document Object Model API
xml.dom.minidom — Minimal DOM implementation
xml.dom.pulldom — Support for building partial DOM trees
xml.sax — Support for SAX2 parsers
xml.sax.handler — Base classes for SAX handlers
xml.sax.saxutils — SAX Utilities
xml.sax.xmlreader — Interface for XML parsers
xml.parsers.expat — Fast XML parsing using Expat
Internet Protocols and Support
webbrowser — Convenient Web-browser controller
cgi — Common Gateway Interface support
cgitb — Traceback manager for CGI scripts
wsgiref — WSGI Utilities and Reference Implementation
urllib — URL handling modules
urllib.request — Extensible library for opening URLs
urllib.response — Response classes used by urllib
urllib.parse — Parse URLs into components
urllib.error — Exception classes raised by urllib.request
urllib.robotparser — Parser for robots.txt
http — HTTP modules
http.client — HTTP protocol client
ftplib — FTP protocol client
poplib — POP3 protocol client
imaplib — IMAP4 protocol client
nntplib — NNTP protocol client
smtplib — SMTP protocol client
smtpd — SMTP Server
telnetlib — Telnet client
uuid — UUID objects according to RFC 4122
socketserver — A framework for network servers
http.server — HTTP servers
http.cookies — HTTP state management
http.cookiejar — Cookie handling for HTTP clients
xmlrpc — XMLRPC server and client modules
xmlrpc.client — XML-RPC client access
xmlrpc.server — Basic XML-RPC servers
ipaddress — IPv4/IPv6 manipulation library
Multimedia Services
audioop — Manipulate raw audio data
aifc — Read and write AIFF and AIFC files
sunau — Read and write Sun AU files
wave — Read and write WAV files
chunk — Read IFF chunked data
colorsys — Conversions between color systems
imghdr — Determine the type of an image
sndhdr — Determine type of sound file
ossaudiodev — Access to OSS-compatible audio devices
Internationalization
gettext — Multilingual internationalization services
locale — Internationalization services
Program Frameworks
turtle — Turtle graphics
cmd — Support for line-oriented command interpreters
shlex — Simple lexical analysis
Graphical User Interfaces with Tk
tkinter — Python interface to Tcl/Tk
tkinter.ttk — Tk themed widgets
tkinter.tix — Extension widgets for Tk
tkinter.scrolledtext — Scrolled Text Widget
IDLE
Other Graphical User Interface Packages
Development Tools
pydoc — Documentation generator and online help system
doctest — Test interactive Python examples
unittest — Unit testing framework
unittest.mock — mock object library
unittest.mock — getting started
2to3 - Automated Python 2 to 3 code translation
test — Regression tests package for Python
test.support — Utilities for the Python test suite
Debugging and Profiling
bdb — Debugger framework
faulthandler — Dump the Python traceback
pdb — The Python Debugger
The Python Profilers
timeit — Measure execution time of small code snippets
trace — Trace or track Python statement execution
tracemalloc — Trace memory allocations
Examples
API
Software Packaging and Distribution
distutils — Building and installing Python modules
ensurepip — Bootstrapping the pip installer
venv — Creation of virtual environments
Python Runtime Services
sys — System-specific parameters and functions
sysconfig — Provide access to Python's configuration information
builtins — Built-in objects
__main__ — Top-level script environment
warnings — Warning control
contextlib — Utilities for with-statement contexts
abc — Abstract Base Classes
atexit — Exit handlers
traceback — Print or retrieve a stack traceback
__future__ — Future statement definitions
gc — Garbage Collector interface
inspect — Inspect live objects
site — Site-specific configuration hook
fpectl — Floating point exception control
Custom Python Interpreters
code — Interpreter base classes
codeop — Compile Python code
Importing Modules
imp — Access the import internals
zipimport — Import modules from Zip archives
pkgutil — Package extension utility
modulefinder — Find modules used by a script
runpy — Locating and executing Python modules
importlib – The implementation of import
Python Language Services
parser — Access Python parse trees
ast — Abstract Syntax Trees
symtable — Access to the compiler's symbol tables
symbol — Constants used with Python parse trees
token — Constants used with Python parse trees
keyword — Testing for Python keywords
tokenize — Tokenizer for Python source
tabnanny — Detection of ambiguous indentation
pyclbr — Python class browser support
py_compile — Compile Python source files
compileall — Byte-compile Python libraries
dis — Disassembler for Python bytecode
pickletools — Tools for pickle developers
Miscellaneous Services
formatter — Generic output formatting
MS Windows Specific Services
msilib — Read and write Microsoft Installer files
msvcrt – Useful routines from the MS VC++ runtime
winreg – Windows registry access
winsound — Sound-playing interface for Windows
Unix Specific Services
posix — The most common POSIX system calls
pwd — The password database
spwd — The shadow password database
grp — The group database
crypt — Function to check Unix passwords
termios — POSIX style tty control
tty — Terminal control functions
pty — Pseudo-terminal utilities
fcntl — The fcntl and ioctl system calls
pipes — Interface to shell pipelines
resource — Resource usage information
nis — Interface to Sun's NIS (Yellow Pages)
syslog — Unix syslog library routines
Undocumented Modules
Platform specific modules
Glossary
Bibliography
About these documents
Contributors to the Python Documentation
History and License
History of the software
Terms and conditions for accessing or otherwise using Python
Licenses and Acknowledgements for Incorporated Software
Copyright
Python Module Index
Index
The Python Library Reference Release 3.4.1 Guido van Rossum Fred L. Drake, Jr., editor May 22, 2014 Python Software Foundation Email: docs@python.org
CONTENTS 1 Introduction 2 Built-in Functions 3 Built-in Constants 3.1 Constants added by the site module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Built-in Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Truth Value Testing . Boolean Operations — and, or, not . . . . Comparisons . 4.1 4.2 4.3 4.4 Numeric Types — int, float, complex . . . . . 4.5 4.6 4.7 4.8 4.9 4.10 Mapping Types — dict . . 4.11 Context Manager Types . 4.12 Other Built-in Types . 4.13 Special Attributes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Iterator Types . Sequence Types — list, tuple, range . . . . . Text Sequence Type — str . Binary Sequence Types — bytes, bytearray, memoryview . . . . . Set Types — set, frozenset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Built-in Exceptions Base classes . Concrete exceptions . . . . Exception hierarchy . 5.1 5.2 5.3 Warnings . 5.4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 5 23 23 25 25 25 26 26 32 32 37 45 54 56 60 60 63 65 65 66 71 71 6 Text Processing Services 73 73 string — Common string operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 re — Regular expression operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . difflib — Helpers for computing deltas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 textwrap — Text wrapping and filling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 unicodedata — Unicode Database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 stringprep — Internet String Preparation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 readline — GNU readline interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 rlcompleter — Completion function for GNU readline . . . . . . . . . . . . . . . . . . . . 117 . 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 7 Binary Data Services 119 struct — Interpret bytes as packed binary data . . . . . . . . . . . . . . . . . . . . . . . . . . 119 codecs — Codec registry and base classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 7.1 7.2 8 Data Types 139 datetime — Basic date and time types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139 calendar — General calendar-related functions . . . . . . . . . . . . . . . . . . . . . . . . . 164 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167 collections — Container datatypes . 8.1 8.2 8.3 i
. . . . . . . . . . . . . . . . . . 181 collections.abc — Abstract Base Classes for Containers 8.4 heapq — Heap queue algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183 8.5 bisect — Array bisection algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 8.6 array — Efficient arrays of numeric values . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 8.7 weakref — Weak references . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 192 8.8 8.9 types — Dynamic type creation and names for built-in types . . . . . . . . . . . . . . . . . . . 199 8.10 copy — Shallow and deep copy operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202 8.11 pprint — Data pretty printer 8.12 reprlib — Alternate repr() implementation . . . . . . . . . . . . . . . . . . . . . 207 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 8.13 enum — Support for enumerations . . . . . . . . . . . . . 9 Numeric and Mathematical Modules 221 numbers — Numeric abstract base classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 221 math — Mathematical functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 224 cmath — Mathematical functions for complex numbers . . . . . . . . . . . . . . . . . . . . . . 228 decimal — Decimal fixed point and floating point arithmetic . . . . . . . . . . . . . . . . . . 230 fractions — Rational numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255 random — Generate pseudo-random numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 257 . . . . . . . . . . . . . . . . . . . . . . . . 261 statistics — Mathematical statistics functions . . . . . . 9.1 9.2 9.3 9.4 9.5 9.6 9.7 10 Functional Programming Modules 267 10.1 itertools — Functions creating iterators for efficient looping . . . . . . . . . . . . . . . . . 267 10.2 functools — Higher-order functions and operations on callable objects . . . . . . . . . . . . 280 10.3 operator — Standard operators as functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 286 11 File and Directory Access 293 11.1 pathlib — Object-oriented filesystem paths . . . . . . . . . . . . . . . . . . . . . . . . . . . 293 11.2 os.path — Common pathname manipulations . . . . . . . . . . . . . . . . . . . . . . . . . . 306 11.3 fileinput — Iterate over lines from multiple input streams . . . . . . . . . . . . . . . . . . . 310 . . . 11.4 stat — Interpreting stat() results . . . . . . . . . . . . . . . . . . . . . . . . 312 11.5 filecmp — File and Directory Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . 316 11.6 tempfile — Generate temporary files and directories . . . . . . . . . . . . . . . . . . . . . . 318 11.7 glob — Unix style pathname pattern expansion . . . . . . . . . . . . . . . . . . . . . . . . . . 321 11.8 fnmatch — Unix filename pattern matching . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322 11.9 linecache — Random access to text lines . . . . . . . . . . . . . . . . . . . . . . . . . . . . 323 11.10 shutil — High-level file operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 324 11.11 macpath — Mac OS 9 path manipulation functions . . . . . . . . . . . . . . . . . . . . . . . . 330 . . . . . 12 Data Persistence 333 12.1 pickle — Python object serialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 333 12.2 copyreg — Register pickle support functions . . . . . . . . . . . . . . . . . . . 345 12.3 shelve — Python object persistence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345 12.4 marshal — Internal Python object serialization . . . . . . . . . . . . . . . . . . . . . . . . . . 348 12.5 dbm — Interfaces to Unix “databases” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 . . . . . . . . . . . . . . . . . . . . . 352 12.6 sqlite3 — DB-API 2.0 interface for SQLite databases . . . . . . 13 Data Compression and Archiving 371 13.1 zlib — Compression compatible with gzip . . . . . . . . . . . . . . . 371 13.2 gzip — Support for gzip files . . . . . . . . . . . . . . . . . . . . . . 374 . . . . . 13.3 bz2 — Support for bzip2 compression . . . . . . . . . . . . . . . . . . . . . . . 376 13.4 lzma — Compression using the LZMA algorithm . . . . . . . . . . . . . . . . . . . . . . . . . 378 13.5 zipfile — Work with ZIP archives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 13.6 tarfile — Read and write tar archive files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 File Formats 399 14.1 csv — CSV File Reading and Writing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 399 14.2 configparser — Configuration file parser . . . . . . . . . . . . . . . . . . . . . . . . . . . 404 14.3 netrc — netrc file processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420 14.4 xdrlib — Encode and decode XDR data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 421 ii
14.5 plistlib — Generate and parse Mac OS X .plist files . . . . . . . . . . . . . . . . . . . . 424 15 Cryptographic Services 427 15.1 hashlib — Secure hashes and message digests . . . . . . . . . . . . . . . . . . . . . . . . . . 427 15.2 hmac — Keyed-Hashing for Message Authentication . . . . . . . . . . . . . . . . . . . . . . . 429 16 Generic Operating System Services 433 16.1 os — Miscellaneous operating system interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . 433 16.2 io — Core tools for working with streams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470 16.3 time — Time access and conversions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 480 16.4 argparse — Parser for command-line options, arguments and sub-commands . . . . . . . . . 487 16.5 optparse — Parser for command line options . . . . . . . . . . . . . . . . . . . . . . . . . . 514 16.6 getopt — C-style parser for command line options . . . . . . . . . . . . . . . . . . . . . . . . 539 16.7 logging — Logging facility for Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 541 16.8 logging.config — Logging configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . 554 16.9 logging.handlers — Logging handlers . . . . . . . . . . . . . . . . . . . . . . . . . 564 16.10 getpass — Portable password input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 574 16.11 curses — Terminal handling for character-cell displays . . . . . . . . . . . . . . . . . . . . . 574 16.12 curses.textpad — Text input widget for curses programs . . . . . . . . . . . . . . . . . . . 590 16.13 curses.ascii — Utilities for ASCII characters . . . . . . . . . . . . . . . . . . . . . . . . . 591 16.14 curses.panel — A panel stack extension for curses . . . . . . . . . . . . . . . . . . . . . . 593 16.15 platform — Access to underlying platform’s identifying data . . . . . . . . . . . . . . . . . . 594 16.16 errno — Standard errno system symbols . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597 16.17 ctypes — A foreign function library for Python . . . . . . . . . . . . . . . . . . . . . . . . . 603 . . . 17 Concurrent Execution 633 17.1 threading — Thread-based parallelism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 633 17.2 multiprocessing — Process-based parallelism . . . . . . . . . . . . . . . . . . . . . . . . 644 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 681 17.3 The concurrent package . . . . . . . . . . . . . . . . . . . . . . . 682 17.4 concurrent.futures — Launching parallel tasks . 17.5 subprocess — Subprocess management . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 686 17.6 sched — Event scheduler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 699 17.7 queue — A synchronized queue class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 701 17.8 dummy_threading — Drop-in replacement for the threading module . . . . . 703 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 703 17.9 _thread — Low-level threading API . . . . . . . 705 17.10 _dummy_thread — Drop-in replacement for the _thread module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Interprocess Communication and Networking 707 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 707 18.1 socket — Low-level networking interface 18.2 ssl — TLS/SSL wrapper for socket objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . 724 18.3 select — Waiting for I/O completion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 746 18.4 selectors – High-level I/O multiplexing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 751 18.5 asyncio – Asynchronous I/O, event loop, coroutines and tasks . . . . . . . . . . . . . . . . . . 754 18.6 asyncore — Asynchronous socket handler . . . . . . . . . . . . . . . . . . . . . . . . . . . . 791 18.7 asynchat — Asynchronous socket command/response handler . . . . . . . . . . . . . . . . . 794 18.8 signal — Set handlers for asynchronous events . . . . . . . . . . . . . . . . . . . . . . . . . 797 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 802 18.9 mmap — Memory-mapped file support 19 Internet Data Handling 807 19.1 email — An email and MIME handling package . . . . . . . . . . . . . . . . . . . . . . . . . 807 19.2 json — JSON encoder and decoder . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 857 19.3 mailcap — Mailcap file handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 864 19.4 mailbox — Manipulate mailboxes in various formats . . . . . . . . . . . . . . . . . . . . . . . 865 19.5 mimetypes — Map filenames to MIME types . . . . . . . . . . . . . . . . . . . . . . . . . . 880 19.6 base64 — Base16, Base32, Base64, Base85 Data Encodings . . . . . . . . . . . . . . . . . . . 883 19.7 binhex — Encode and decode binhex4 files . . . . . . . . . . . . . . . . . . . . . . . . . . . . 886 19.8 binascii — Convert between binary and ASCII . . . . . . . . . . . . . . . . . . . . . . . . . 886 19.9 quopri — Encode and decode MIME quoted-printable data . . . . . . . . . . . . . . . . . . . 888 19.10 uu — Encode and decode uuencode files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 889 iii
20 Structured Markup Processing Tools . . 891 20.1 html — HyperText Markup Language support . . . . . . . . . . . . . . . . . . . . . . . . . . . 891 20.2 html.parser — Simple HTML and XHTML parser . . . . . . . . . . . . . . . . . . . . . . 891 20.3 html.entities — Definitions of HTML general entities . . . . . . . . . . . . . . . . . . . . 896 20.4 XML Processing Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 896 20.5 xml.etree.ElementTree — The ElementTree XML API . . . . . . . . . . . . . . . . . . 898 20.6 xml.dom — The Document Object Model API . . . . . . . . . . . . . . . . . . . . . . . . . . 911 20.7 xml.dom.minidom — Minimal DOM implementation . . . . . . . . . . . . . . . . . . . . . 920 20.8 xml.dom.pulldom — Support for building partial DOM trees . . . . . . . . . . . . . . . . . 924 20.9 xml.sax — Support for SAX2 parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 926 20.10 xml.sax.handler — Base classes for SAX handlers . . . . . . . . . . . . . . . . . . . . . . 927 . . . . . . . . . . . . . . . . . . . . . . . . . . . 932 20.11 xml.sax.saxutils — SAX Utilities . . . . . . . . . . . . . . . . . . . . . . . . . 933 20.12 xml.sax.xmlreader — Interface for XML parsers 20.13 xml.parsers.expat — Fast XML parsing using Expat . . . . . . . . . . . . . . . . . . . . 936 21 Internet Protocols and Support . . . . . . . 947 21.1 webbrowser — Convenient Web-browser controller . . . . . . . . . . . . . . . . . . . . . . . 947 21.2 cgi — Common Gateway Interface support . . . . . . . . . . . . . . . . . . . . . . . . . . . . 949 21.3 cgitb — Traceback manager for CGI scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . 955 21.4 wsgiref — WSGI Utilities and Reference Implementation . . . . . . . . . . . . . . . . . . . . 956 21.5 urllib — URL handling modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 964 21.6 urllib.request — Extensible library for opening URLs . . . . . . . . . . . . . . . . . . . 964 21.7 urllib.response — Response classes used by urllib . . . . . . . . . . . . . . . . . . . . . 980 21.8 urllib.parse — Parse URLs into components . . . . . . . . . . . . . . . . . . . . . . . . . 980 21.9 urllib.error — Exception classes raised by urllib.request . . . . . . . . . . . . . . . . . . 986 . . . . . . . . . . . . . . . . . . . . . . 987 21.10 urllib.robotparser — Parser for robots.txt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 987 21.11 http — HTTP modules . 21.12 http.client — HTTP protocol client . . . . . . . . . . . . . . . . . . . . . . . . . . . 988 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 994 21.13 ftplib — FTP protocol client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 998 21.14 poplib — POP3 protocol client . . . 21.15 imaplib — IMAP4 protocol client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1001 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1006 21.16 nntplib — NNTP protocol client . . 21.17 smtplib — SMTP protocol client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1013 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1018 21.18 smtpd — SMTP Server . . 21.19 telnetlib — Telnet client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1020 21.20 uuid — UUID objects according to RFC 4122 . . . . . . . . . . . . . . . . . . . . . . . . . . . 1022 21.21 socketserver — A framework for network servers . . . . . . . . . . . . . . . . . . . . . . . 1025 21.22 http.server — HTTP servers . . . . . . . . . . . . . . . . . . . . . . . . . . . 1032 21.23 http.cookies — HTTP state management . . . . . . . . . . . . . . . . . . . . . . . . . 1037 21.24 http.cookiejar — Cookie handling for HTTP clients . . . . . . . . . . . . . . . . . . . . . 1040 21.25 xmlrpc — XMLRPC server and client modules . . . . . . . . . . . . . . . . . . . . . . . . . . 1048 21.26 xmlrpc.client — XML-RPC client access . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048 21.27 xmlrpc.server — Basic XML-RPC servers . . . . . . . . . . . . . . . . . . . . . . . . . . 1055 21.28 ipaddress — IPv4/IPv6 manipulation library . . . . . . . . . . . . . . . . . . . . . . . . . . 1060 . . . . . . . . . . . . . . . . . . . . 22 Multimedia Services 1071 22.1 audioop — Manipulate raw audio data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1071 22.2 aifc — Read and write AIFF and AIFC files . . . . . . . . . . . . . . . . . . . . . . . . . . . 1074 22.3 sunau — Read and write Sun AU files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1076 22.4 wave — Read and write WAV files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1078 22.5 chunk — Read IFF chunked data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1081 22.6 colorsys — Conversions between color systems . . . . . . . . . . . . . . . . . . . . . . . . . 1082 22.7 imghdr — Determine the type of an image . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083 22.8 sndhdr — Determine type of sound file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1083 22.9 ossaudiodev — Access to OSS-compatible audio devices . . . . . . . . . . . . . . . . . . . 1084 23 Internationalization 1089 23.1 gettext — Multilingual internationalization services . . . . . . . . . . . . . . . . . . . . . . 1089 23.2 locale — Internationalization services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1097 iv
24 Program Frameworks 1105 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1105 24.1 turtle — Turtle graphics . 24.2 cmd — Support for line-oriented command interpreters . . . . . . . . . . . . . . . . . . . . . . 1137 24.3 shlex — Simple lexical analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1142 . . . . 25 Graphical User Interfaces with Tk 1147 25.1 tkinter — Python interface to Tcl/Tk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1147 . . . . . . . . . . . . . . . . . . . . . . . . . . 1156 25.2 tkinter.ttk — Tk themed widgets . . 25.3 tkinter.tix — Extension widgets for Tk . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1172 25.4 tkinter.scrolledtext — Scrolled Text Widget . . . . . . . . . . . . . . . . . . . . . . . 1176 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1177 25.5 IDLE . 25.6 Other Graphical User Interface Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1182 . . . . . . . . . . . . . . . . 26 Development Tools 1185 26.1 pydoc — Documentation generator and online help system . . . . . . . . . . . . . . . . . . . . 1185 26.2 doctest — Test interactive Python examples . . . . . . . . . . . . . . . . . . . . . . . . . . . 1186 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1208 26.3 unittest — Unit testing framework . 26.4 unittest.mock — mock object library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1232 26.5 unittest.mock — getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1263 26.6 2to3 - Automated Python 2 to 3 code translation . . . . . . . . . . . . . . . . . . . . . . . . . . 1280 26.7 test — Regression tests package for Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1285 26.8 test.support — Utilities for the Python test suite . . . . . . . . . . . . . . . . . . . . . . . 1288 . . . 27 Debugging and Profiling . 1293 27.1 bdb — Debugger framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1293 27.2 faulthandler — Dump the Python traceback . . . . . . . . . . . . . . . . . . . . . . . . . . 1297 27.3 pdb — The Python Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1299 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1304 27.4 The Python Profilers . . . 27.5 timeit — Measure execution time of small code snippets . . . . . . . . . . . . . . . . . . . . 1312 27.6 trace — Trace or track Python statement execution . . . . . . . . . . . . . . . . . . . . . . . . 1316 27.7 tracemalloc — Trace memory allocations . . . . . . . . . . . . . . . . . . . . . . . . . . . 1318 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1318 27.8 Examples . 27.9 API . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1322 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 Software Packaging and Distribution 1327 28.1 distutils — Building and installing Python modules . . . . . . . . . . . . . . . . . . . . . . 1327 28.2 ensurepip — Bootstrapping the pip installer . . . . . . . . . . . . . . . . . . 1327 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1329 28.3 venv — Creation of virtual environments . . . . . . . . 29 Python Runtime Services . . 1337 29.1 sys — System-specific parameters and functions . . . . . . . . . . . . . . . . . . . . . . . . . 1337 29.2 sysconfig — Provide access to Python’s configuration information . . . . . . . . . . . . . . . 1349 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1353 29.3 builtins — Built-in objects 29.4 __main__ — Top-level script environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1353 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1353 29.5 warnings — Warning control 29.6 contextlib — Utilities for with-statement contexts . . . . . . . . . . . . . . . . . . . 1358 29.7 abc — Abstract Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1368 29.8 atexit — Exit handlers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1373 29.9 traceback — Print or retrieve a stack traceback . . . . . . . . . . . . . . . . . . . . . . . . . 1374 29.10 __future__ — Future statement definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . 1377 29.11 gc — Garbage Collector interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1378 29.12 inspect — Inspect live objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1381 29.13 site — Site-specific configuration hook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1392 29.14 fpectl — Floating point exception control . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1395 . . . . . . . . . . . . . . . 30 Custom Python Interpreters 30.1 code — Interpreter base classes 30.2 codeop — Compile Python code . . . . 1397 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1397 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1399 v
31 Importing Modules 1401 31.1 imp — Access the import internals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1401 31.2 zipimport — Import modules from Zip archives . . . . . . . . . . . . . . . . . . . . . . . . . 1405 31.3 pkgutil — Package extension utility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1407 31.4 modulefinder — Find modules used by a script . . . . . . . . . . . . . . . . . . . . . . . . 1409 31.5 runpy — Locating and executing Python modules . . . . . . . . . . . . . . . . . . . . . . . . . 1411 31.6 importlib – The implementation of import . . . . . . . . . . . . . 1413 . . . . . . . . . . . . . . . . 32 Python Language Services 1427 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1427 32.1 parser — Access Python parse trees 32.2 ast — Abstract Syntax Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1431 32.3 symtable — Access to the compiler’s symbol tables . . . . . . . . . . . . . . . . . . . . . . . 1435 32.4 symbol — Constants used with Python parse trees . . . . . . . . . . . . . . . . . . . . . . . . 1437 32.5 token — Constants used with Python parse trees . . . . . . . . . . . . . . . . . . . . . . . . . 1438 32.6 keyword — Testing for Python keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1439 32.7 tokenize — Tokenizer for Python source . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1439 32.8 tabnanny — Detection of ambiguous indentation . . . . . . . . . . . . . . . . . . . . . . . . 1443 32.9 pyclbr — Python class browser support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1443 32.10 py_compile — Compile Python source files . . . . . . . . . . . . . . . . . . . . . . . . . . . 1444 32.11 compileall — Byte-compile Python libraries . . . . . . . . . . . . . . . . . . . . . . . . . . 1445 32.12 dis — Disassembler for Python bytecode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1447 32.13 pickletools — Tools for pickle developers . . . . . . . . . . . . . . . . . . . . . . . . . . . 1457 33 Miscellaneous Services 1459 33.1 formatter — Generic output formatting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1459 34 MS Windows Specific Services 1463 . . . . . . . . . . . . . . . . . . . . . . . . 1463 34.1 msilib — Read and write Microsoft Installer files 34.2 msvcrt – Useful routines from the MS VC++ runtime . . . . . . . . . . . . . . . . . . . . . . 1468 34.3 winreg – Windows registry access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1469 34.4 winsound — Sound-playing interface for Windows . . . . . . . . . . . . . . . . . . . . . . . . 1477 35 Unix Specific Services . . 1479 35.1 posix — The most common POSIX system calls . . . . . . . . . . . . . . . . . . . . . . . . . 1479 35.2 pwd — The password database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1480 35.3 spwd — The shadow password database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1480 35.4 grp — The group database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1481 35.5 crypt — Function to check Unix passwords . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1482 35.6 termios — POSIX style tty control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1483 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1484 35.7 tty — Terminal control functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1485 35.8 pty — Pseudo-terminal utilities . 35.9 fcntl — The fcntl and ioctl system calls . . . . . . . . . . . . . . . . . . 1486 35.10 pipes — Interface to shell pipelines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1488 35.11 resource — Resource usage information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1489 35.12 nis — Interface to Sun’s NIS (Yellow Pages) . . . . . . . . . . . . . . . . . . . . . . . . . . . 1492 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1493 35.13 syslog — Unix syslog library routines . . . . . . . . . 36 Undocumented Modules 36.1 Platform specific modules . . . 1495 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1495 A Glossary Bibliography 1497 1507 B About these documents 1509 B.1 Contributors to the Python Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1509 C History and License 1511 C.1 History of the software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1511 C.2 Terms and conditions for accessing or otherwise using Python . . . . . . . . . . . . . . . . . . . 1511 . . . . . vi
分享到:
收藏