Python_Standard_Library.pdf

(1857 KB) Pobierz
734968327 UNPDF
734968327.001.png
Preface
"We'd like to pretend that "Fredrik" is a role, but even hundreds of
volunteers couldn't possibly keep up. No, "Fredrik" is the result of
crossing an http server with a spam filter with an emacs whatsit and
some other stuff besides"
Gordon McMillan, June 1998
The Python 2.0 distribution comes with an extensive standard library, comprising over 200 modules.
This book provides a brief description of each module, plus one or more sample scripts showing how to
use it. All in all, this book contains 360 sample scripts.
About this Book
"Those people who have nothing better to do than post on the Internet
all day long are rarely the ones who have the most insights"
Jacob Nielsen, December 1998
Since I first stumbled upon Python some five years ago, I've spent hundreds of hours answering
questions on the comp.lang.python newsgroup. Maybe someone found a module that might be
exactly what he wanted, but he couldn't really figure out how to use it. Maybe someone had picked the
wrong module for the task. Or maybe someone tried to reinvent the wheel. Often, a short sample script
could be much more helpful than a pointer to the reference documentation.
After posting a couple of scripts each week, for a number of years, you end up with a rather large
collection of potentially useful scripts. What you'll find in this book are the best parts from over 3,000
newsgroup messages. You'll also find hundreds of new scripts added to make sure every little nook and
cranny of the standard library has been fully covered.
I've worked hard to make the scripts easy to understand and adaptable. I've intentionally kept the
annotations as short as possible. If you want more background, there's plenty of reference material
shipped with most Python distributions. In this book, the emphasis is on the code.
Comments, suggestions, and bug reports are welcome. Send them to fredrik@pythonware.com . I read
all mail as soon as it arrives, but it might take a while until I get around to answer.
For updates, addenda, and other information related to this book, point your favorite web browser to
http://www.pythonware.com/people/fredrik/librarybook.htm
Python Standard Library
Copyright (c) 1999-2003 by Fredrik Lundh. All rights reserved.
734968327.002.png
What about Tkinter?
This book covers the entire standard library, except the (optional) Tkinter user interface library. There
are several reasons for this, mostly related to time, space, and the fact that I'm working on several
other Tkinter documentation projects.
For current status on these projects, see http://www.pythonware.com/people/fredrik/tkinterbook.htm
Production details
This book was written in DocBook SGML. I used a variety of tools, including Secret Labs'
PythonWorks, and Excosoft Documentor, James Clark's Jade DSSSL processor, and Norm Walsh's
DocBook stylesheets. And a bunch of Python scripts, of course.
Thanks to my referees: Tim Peters, Guido van Rossum, David Ascher, Mark Lutz, and Rael Dornfest,
and the PythonWare crew: Matthew Ellis, Håkan Karlsson, and Rune Uhlin.
Thanks to Lenny Muellner, who turned my SGML files into the book you see before you, and to
Christien Shanreaw, who pulled all the different text and code files together for the book and the CD-
ROM.
About the Examples
Unless otherwise noted, all examples run under Python 1.5.2 and Python 2.0. I've tried not to depend
on internal details, and I expect most scripts to work with upcoming 2.x versions as well.
The examples have been tested on Windows, Solaris, and Linux. Except for a few scripts that depend
on platform specific modules, the examples should work right out of the box on most other platforms
as well. (If you find something that doesn't work as expected, let me know!)
All code is copyrighted. Of course, you're free to use one or more modules in your own programs, just
don't forget where you got them.
Most script files are named after the module they're using, followed by the string " -example- " and a
unique "serial number". Note that the scripts sometimes appear out of order; it's done this way on
purpose, to match the filenames used in an earlier version of this book, (the eff-bot guide to) The
Standard Python Library .
You'll find copies of all scripts on the CD provided with this book. For updates and more information,
see http://www.pythonware.com/people/fredrik/librarybook.htm.
Python Standard Library: Core Modules
1-1
Core Modules
"Since the functions in the C runtime library are not part of the Win32
API, we believe the number of applications that will be affected by this
bug to be very limited"
Microsoft, January 1999
Overview
Python's standard library covers a wide range of modules. Everything from modules that are as much a
part of the Python language as the types and statements defined by the language specification, to
obscure modules that are probably useful only to a small number of programs.
This section describes a number of fundamental standard library modules. Any larger Python program
is likely to use most of these modules, either directly or indirectly.
Built-in Functions and Exceptions
Two modules are even more basic than all other modules combined: the __builtin__ module defines
built-in functions (like len , int , and range ), and the exceptions module defines all built-in
exceptions.
Python imports both modules when it starts up, and makes their content available for all programs.
Operating System Interface Modules
There are a number of modules providing platform-independent interfaces to the underlying operating
system. They are modeled after the POSIX standard API and the standard C library.
The modules in this group include os , which provides file and process operations, os.path which
offers a platform-independent way to pull apart and put together file names, and time which provides
functions to work with dates and times.
To some extent, networking and thread support modules could also belong in this group, but they are
not supported by all Python implementations.
Python Standard Library
Copyright (c) 1999-2003 by Fredrik Lundh. All rights reserved.
734968327.003.png
Python Standard Library: Core Modules
1-2
Type Support Modules
Several built-in types have support modules in the standard library. The string module implements
commonly used string operations, the math module provides math operations and constants, and the
cmath module does the same for complex numbers.
Regular Expressions
The re module provides regular expressions support for Python. Regular expressions are string
patterns written in a special syntax, which can be used to match strings, and extract substrings.
Language Support Modules
sys gives you access to various interpreter variables, such as the module search path, and the
interpreter version. operator provides functional equivalents to many built-in operators. copy allows
you to copy objects. And finally, gc gives you more control over the garbage collector facilities in
Python 2.0.
Zgłoś jeśli naruszono regulamin