Showing posts with label Addison-Wesley. Show all posts

David M. Beazley

Contents at a Glance

Part I: The Python Language
A Tutorial Introduction
Lexical Conventions and Syntax
Types and Objects
Operators and Expressions
Program Structure and Control Flow
Functions and Functional Programming
Classes and Object-Oriented Programming
Modules, Packages, and Distribution
Input and Output
Execution Environment
Testing, Debugging, Profiling, and Tuning
Part II: The Python Library
Built-In Functions
Python Runtime Services
Mathematics
Data Structures, Algorithms, and Code Simplification
String and Text Handling
Python Database Access
File and Directory Handling
Operating System Services
Threads and Concurrency
Network Programming and Sockets
Internet Application Programming
Web Programming 
Internet Data Handling and Encoding
Miscellaneous Library Modules
Part III: Extending and Embedding
Extending and Embedding Python 
Appendix: Python 3

e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 3.00
 Pages
 745 p
 File Size 
 3,580 KB
 File Type
 PDF format
 ISBN-13
 ISBN-10
 978-0-672-32978-4
 0-672-32978-6
 Copyright©   
 2009 by Pearson Education, Inc 

About the Author
David M. Beazley is a long-time Python enthusiast, having been involved with the
Python community since 1996. He is probably best known for his work on SWIG, a
popular software package for integrating C/C++ programs with other programming
languages, including Python, Perl, Ruby,Tcl, and Java. He has also written a number of
other programming tools, including PLY, a Python implementation of lex and yacc.
Dave spent seven years working in the Theoretical Physics Division at Los Alamos
National Laboratory, where he helped pioneer the use of Python with massively parallel
supercomputers. After that, Dave went off to work as an evil professor, where he
enjoyed tormenting college students with a variety of insane programming projects.
However, he has since seen the error of his ways and is now working as an independent
software developer, consultant, Python trainer, and occasional jazz musician living in
Chicago. He can be contacted at http://www.dabeaz.com.

About the Technical Editor
Noah Gift is the co-author of Python For UNIX and Linux System Administration
(O’Reilly) and is also working on Google App Engine In Action (Manning). He is an
author, speaker, consultant, and community leader, writing for publications such as IBM
developerWorks, Red Hat Magazine, O’Reilly, and MacTech. His consulting company’s
website is http://www.giftcs.com, and much of his writing can be found at
http://noahgift.com.You can also follow Noah on Twitter.
Noah has a master’s degree in CIS from Cal State, Los Angeles, a B.S. in nutritional
science from Cal Poly San Luis Obispo, is an Apple and LPI-certified SysAdmin, and
has worked at companies such as Caltech, Disney Feature Animation, Sony Imageworks,
and Turner Studios. He is currently working at Weta Digital in New Zealand. In his free
time he enjoys spending time with his wife Leah and their son Liam, composing for the
piano, running marathons, and exercising religiously.

Table of Contents
Introduction 1
I: The Python Language
1 A Tutorial Introduction 5
Running Python 5
Variables and Arithmetic Expressions 7
Conditionals 9
File Input and Output 10
Strings 11
Lists 12
Tuples 14
Sets 15
Dictionaries 16
Iteration and Looping 17
Functions 18
Generators 19
Coroutines 20
Objects and Classes 21
Exceptions 22
Modules 23
Getting Help 24
2 Lexical Conventions and Syntax 25
Line Structure and Indentation 25
Identifiers and Reserved Words 26
Numeric Literals 26
String Literals 27
Containers 29
Operators, Delimiters, and Special Symbols 30
Documentation Strings 30
Decorators 30
Source Code Encoding 31
3 Types and Objects 33
Terminology 33
Object Identity and Type 33
Reference Counting and Garbage Collection 34
References and Copies 35
From the Library of Lee Bogdanoff
viii Contents
First-Class Objects 36
Built-in Types for Representing Data 37
The None Type 38
Numeric Types 38
Sequence Types 39
Mapping Types 44
Set Types 46
Built-in Types for Representing Program Structure 47
Callable Types 47
Classes, Types, and Instances 50
Modules 50
Built-in Types for Interpreter Internals 51
Code Objects 51
Frame Objects 52
Traceback Objects 52
Generator Objects 53
Slice Objects 53
Ellipsis Object 54
Object Behavior and Special Methods 54
Object Creation and Destruction 54
Object String Representation 55
Object Comparison and Ordering 56
Type Checking 57
Attribute Access 57
Attribute Wrapping and Descriptors 58
Sequence and Mapping Methods 58
Iteration 59
Mathematical Operations 60
Callable Interface 62
Context Management Protocol 62
Object Inspection and dir() 63
4 Operators and Expressions 65
Operations on Numbers 65
Operations on Sequences 67
String Formatting 70
Advanced String Formatting 72
Operations on Dictionaries 74
Operations on Sets 75
Augmented Assignment 75
From the Library of Lee Bogdanoff
Contents ix
The Attribute (.) Operator 76
The Function Call () Operator 76
Conversion Functions 76
Boolean Expressions and Truth Values 77
Object Equality and Identity 78
Order of Evaluation 78
Conditional Expressions 79
5 Program Structure and Control Flow 81
Program Structure and Execution 81
Conditional Execution 81
Loops and Iteration 82
Exceptions 84
Built-in Exceptions 86
Defining New Exceptions 88
Context Managers and the with Statement 89
Assertions and __debug__ 91
6 Functions and Functional Programming 93
Functions 93
Parameter Passing and Return Values 95
Scoping Rules 96
Functions as Objects and Closures 98
Decorators 101
Generators and yield 102
Coroutines and yield Expressions 104
Using Generators and Coroutines 106
List Comprehensions 108
Generator Expressions 109
Declarative Programming 110
The lambda Operator 112
Recursion 112
Documentation Strings 113
Function Attributes 114
eval(), exec(), and compile() 115
7 Classes and Object-Oriented Programming 117
The class Statement 117
Class Instances 118
Scoping Rules 118
Inheritance 119
From the Library of Lee Bogdanoff
Polymorphism Dynamic Binding and Duck Typing 122
Static Methods and Class Methods 123
Properties 124
Descriptors 126
Data Encapsulation and Private Attributes 127
Object Memory Management 128
Object Representation and Attribute Binding 131
__slots__ 132
Operator Overloading 133
Types and Class Membership Tests 134
Abstract Base Classes 136
Metaclasses 138
Class Decorators 141
8 Modules, Packages, and Distribution 143
Modules and the import Statement 143
Importing Selected Symbols from a Module 145
Execution as the Main Program 146
The Module Search Path 147
Module Loading and Compilation 147
Module Reloading and Unloading 149
Packages 149
Distributing Python Programs and Libraries 152
Installing Third-Party Libraries 154
9 Input and Output 157
Reading Command-Line Options 157
Environment Variables 158
Files and File Objects 158
Standard Input, Output, and Error 161
The print Statement 162
The print() Function 163
Variable Interpolation in Text Output 163
Generating Output 164
Unicode String Handling 165
Unicode I/O 167
Unicode Data Encodings 168
Unicode Character Properties 170
Object Persistence and the pickle Module 171
x Contents
From the Library of Lee Bogdanoff
10 Execution Environment 173
Interpreter Options and Environment 173
Interactive Sessions 175
Launching Python Applications 176
Site Configuration Files 177
Per-user Site Packages 177
Enabling Future Features 178
Program Termination 179
11 Testing, Debugging, Profiling, and Tuning 181
Documentation Strings and the doctest Module 181
Unit Testing and the unittest Module 183
The Python Debugger and the pdb Module 186
Debugger Commands 187
Debugging from the Command Line 189
Configuring the Debugger 190
Program Profiling 190
Tuning and Optimization 191
Making Timing Measurements 191
Making Memory Measurements 192
Disassembly 193
Tuning Strategies 194
II: The Python Library 199
12 Built-In Functions and Exceptions 201
Built-in Functions and Types 201
Built-In Exceptions 212
Exception Base Classes 212
Exception Instances 212
Predefined Exception Classes 213
Built-In Warnings 216
future_builtins 217
13 Python Runtime Services 219
atexit 219
copy 219
Notes 220
Contents xi
From the Library of Lee Bogdanoff
gc 220
Notes 222
inspect 222
marshal 226
Notes 226
pickle 226
Notes 229
sys 229
Variables 229
Functions 233
traceback 235
types 237
Notes 237
warnings 238
Notes 239
weakref 240
Example 242
Notes 242
14 Mathematics 243
decimal 243
Decimal Objects 244
Context Objects 244
Functions and Constants 247
Examples 248
Notes 249
fractions 250
math 251
Notes 252
numbers 252
Notes 253
random 254
Seeding and Initialization 254
Random Integers 254
Random Sequences 254
Real-Valued Random Distributions 255
Notes 256
xii Contents
From the Library of Lee Bogdanoff
15 Data Structures, Algorithms, and Code Simplification 257
abc 257
array 259
Notes 261
bisect 261
collections 262
deque and defaultdict 262
Named Tuples 263
Abstract Base Classes 265
contextlib 267
functools 268
heapq 269
itertools 270
Examples 273
operator 273
16 String and Text Handling 277
codecs 277
Low-Level codecs Interface 277
I/O-Related Functions 279
Useful Constants 279
Standard Encodings 280
Notes 280
re 281
Pattern Syntax 281
Functions 283
Regular Expression Objects 284
Match Objects 285
Example 286
Notes 287
string 287
Constants 287
Formatter Objects 288
Template Strings 289
Utility Functions 290
struct 290
Packing and Unpacking Functions 290
Struct Objects 291
Contents xiii
From the Library of Lee Bogdanoff
Format Codes 291
Notes 292
unicodedata 293
17 Python Database Access 297
Relational Database API Specification 297
Connections 297
Cursors 298
Forming Queries 300
Type Objects 301
Error Handling 302
Multithreading 302
Mapping Results into Dictionaries 303
Database API Extensions 303
sqlite3 Module 303
Module-Level Functions 304
Connection Objects 305
Cursors and Basic Operations 308
DBM-Style Database Modules 310
shelve Module 311
18 File and Directory Handling 313
bz2 313
filecmp 314
fnmatch 316
Examples 316
glob 317
Example 317
gzip 317
Notes 317
shutil 318
tarfile 319
Exceptions 322
Example 322
tempfile 323
zipfile 324
zlib 328
xiv Contents
From the Library of Lee Bogdanoff
19 Operating System Services 331
commands 331
Notes 332
configParser, Configparser 332
The ConfigParser Class 332
Example 334
Notes 336
datetime 336
date Objects 336
time Objects 338
datetime objects 339
timedelta objects 340
Mathematical Operations Involving Dates 341
tzinfo Objects 342
Date and Time Parsing 343
errno 343
POSIX Error Codes 344
Windows Error Codes 346
fcntl 347
Example 348
Notes 349
io 349
Base I/O Interface 349
Raw I/O 350
Buffered Binary I/O 351
Text I/O 353
The open() Function 354
Abstract Base Classes 354
logging 355
Logging Levels 355
Basic Configuration 355
Logger Objects 356
Handler Objects 362
Message Formatting 364
Miscellaneous Utility Functions 366
Logging Configuration 366
Performance Considerations 369
Notes 369
Contents xv
From the Library of Lee Bogdanoff
mmap 369
Notes 372
msvcrt 372
optparse 374
Example 377
Notes 378
os 378
Process Environment 379
File Creation and File Descriptors 381
Files and Directories 386
Process Management 390
System Configuration 395
Exceptions 396
os.path 396
signal 399
Example 401
Notes 401
subprocess 402
Examples 404
Notes 404
time 405
Notes 407
winreg 408
Notes 411
20 Threads and Concurrency 413
Basic Concepts 413
Concurrent Programming and Python 414
multiprocessing 415
Processes 415
Interprocess Communication 417
Process Pools 424
Shared Data and Synchronization 426
Managed Objects 428
Connections 433
Miscellaneous Utility Functions 434
General Advice on Multiprocessing 435
threading 436
Thread Objects 436
Timer Objects 437
xvi Contents
From the Library of Lee Bogdanoff
Lock Objects 438
RLock 438
Semaphore and Bounded Semaphore 439
Events 440
Condition Variables 441
Working with Locks 442
Thread Termination and Suspension 443
Utility Functions 443
The Global Interpreter Lock 444
Programming with Threads 444
queue, Queue 444
Queue Example with Threads 445
Coroutines and Microthreading 446
21 Network Programming and Sockets 449
Network Programming Basics 449
asynchat 452
asyncore 455
Example 457
select 459
Advanced Module Features 460
Advanced Asynchronous I/O Example 460
When to Consider Asynchronous Networking 467
socket 469
Address Families 470
Socket Types 470
Addressing 471
Functions 473
Exceptions 485
Example 485
Notes 486
ssl 486
Examples 489
SocketServer 489
Handlers 490
Servers 491
Defining Customized Servers 492
Customization of Application Servers 494
Contents xvii
From the Library of Lee Bogdanoff
22 Internet Application Programming 497
ftplib 497
Example 500
http Package 500
http.client (httplib) 502
http.server (BaseHTTPServer, CGIHTTPServer,
SimpleHTTPServer) 506
http.cookies (Cookie) 511
http.cookiejar (cookielib) 513
smtplib 513
Example 514
urllib Package 514
urllib.request (urllib2) 515
urllib.response 520
urllib.parse 520
urllib.error 523
urllib.robotparser (robotparser) 523
Notes 524
xmlrpc Package 524
xmlrpc.client (xmlrpclib) 524
xmlrpc.server (SimpleXMLRPCServer, DocXMLRPCServer)
527
23 Web Programming 531
cgi 533
CGI Programming Advice 537
Notes 538
cgitb 539
wsgiref 540
The WSGI Specification 540
wsgiref Package 542
webbrowser 544
24 Internet Data Handling and Encoding 545
base64 545
binascii 547
csv 548
Dialects 551
Example 551
xviii Contents
From the Library of Lee Bogdanoff
email Package 552
Parsing Email 552
Composing Email 555
Notes 558
hashlib 559
hmac 559
Example 560
HTMLParser 561
Example 562
json 563
mimetypes 566
quopri 567
xml Package 568
XML Example Document 569
xml.dom.minidom 570
xml.etree.ElementTree 573
xml.sax 580
xml.sax.saxutils 583
25 Miscellaneous Library Modules 585
Python Services 585
String Processing 586
Operating System Modules 586
Network 587
Internet Data Handling 587
Internationalization 587
Multimedia Services 588
Miscellaneous 588
III: Extending and Embedding 589
26 Extending and Embedding Python 591
Extension Modules 591
An Extension Module Prototype 593
Naming Extension Modules 595
Compiling and Packaging Extensions 596
Type Conversion from Python to C 597
Type Conversion from C to Python 602
Contents xix
From the Library of Lee Bogdanoff
Adding Values to a Module 604
Error Handling 605
Reference Counting 607
Threads 607
Embedding the Python Interpreter 608
An Embedding Template 608
Compilation and Linking 608
Basic Interpreter Operation and Setup 608
Accessing Python from C 610
Converting Python Objects to C 611
ctypes 612
Loading Shared Libraries 612
Foreign Functions 612
Datatypes 613
Calling Foreign Functions 615
Alternative Type Construction Methods 616
Utility Functions 617
Example 618
Advanced Extending and Embedding 619
Jython and IronPython 620
Appendix Python 3 621
Who Should Be Using Python 3? 621
New Language Features 622
Source Code Encoding and Identifiers 622
Set Literals 622
Set and Dictionary Comprehensions 623
Extended Iterable Unpacking 623
Nonlocal Variables 624
Function Annotations 624
Keyword-Only Arguments 625
Ellipsis as an Expression 626
Chained Exceptions 626
Improved super() 627
Advanced Metaclasses 627
Common Pitfalls 629
Text Versus Bytes 629
New I/O System 631
xx Contents
From the Library of Lee Bogdanoff
print() and exec() Functions 631
Use of Iterators and Views 632
Integers and Integer Division 633
Comparisons 633
Iterators and Generators 633
File Names, Arguments, and Environment Variables 633
Library Reorganization 634
Absolute Imports 634
Code Migration and 2to3 634
Porting Code to Python 2.6 634
Providing Test Coverage 635
Using the 2to3 Tool 635
A Practical Porting Strategy 637
Simultaneous Python 2 and Python 3 Support 638
Participate 638
Index 639
Contents xxi


Bookscreen
e-books shop

Introduction
This book is intended to be a concise reference to the Python programming language.
Although an experienced programmer will probably be able to learn Python from this
book, it’s not intended to be an extended tutorial or a treatise on how to program.
Rather, the goal is to present the core Python language, and the most essential parts of
the Python library in a manner that’s accurate and concise.This book assumes that the
reader has prior programming experience with Python or another language such as C
or Java. In addition, a general familiarity with systems programming topics (for example,
basic operating system concepts and network programming) may be useful in understanding
certain parts of the library reference.
Python is freely available for download at http://www.python.org.Versions are available
for almost every operating system, including UNIX,Windows, and Macintosh. In
addition, the Python website includes links to documentation, how-to guides, and a
wide assortment of third-party software.

This edition of Python Essential Reference comes at a pivotal time in Python’s evolution.
Python 2.6 and Python 3.0 are being released almost simultaneously.Yet, Python 3
is a release that breaks backwards compatibility with prior Python versions. As an author
and programmer, I’m faced with a dilemma: do I simply jump forward to Python 3.0 or
do I build upon the Python 2.x releases that are more familiar to most programmers?
Years ago, as a C programmer I used to treat certain books as the ultimate authority
on what programming language features should be used. For example, if you were using
something that wasn’t documented in the K&R book, it probably wasn’t going to be
portable and should be approached with caution.This approach served me very well as
a programmer and it’s the approach I have decided to take in this edition of the
Essential Reference. Namely, I have chosen to omit features of Python 2 that have been
removed from Python 3. Likewise, I don’t focus on features of Python 3 that have not
been back-ported (although such features are still covered in an appendix). As a result, I
hope this book can be a useful companion for Python programmers, regardless of what
Python version is being used.

The fourth edition of Python Essential Reference also includes some of the most exciting
changes since its initial publication nearly ten years ago. Much of Python’s development
throughout the last few years has focused on new programming language features—
especially related to functional and meta programming. As a result, the chapters
on functions and object-oriented programming have been greatly expanded to cover
topics such as generators, iterators, coroutines, decorators, and metaclasses.The library
chapters have been updated to focus on more modern modules. Examples and code
fragments have also been updated throughout the book. I think most programmers will
be quite pleased with the expanded coverage.

Finally, it should be noted that Python already includes thousands of pages of useful
documentation.The contents of this book are largely based on that documentation, but
with a number of key differences. First, this reference presents information in a much
more compact form, with different examples and alternative descriptions of many topics.
Second, a significant number of topics in the library reference have been expanded
From the Library of Lee Bogdanoff
to include outside reference material.This is especially true for low-level system and
networking modules in which effective use of a module normally relies on a myriad of
options listed in manuals and outside references. In addition, in order to produce a more
concise reference, a number of deprecated and relatively obscure library modules have
been omitted.
In writing this book, it has been my goal to produce a reference containing virtually
everything I have needed to use Python and its large collection of modules. Although
this is by no means a gentle introduction to the Python language, I hope that you find
the contents of this book to be a useful addition to your programming reference library
for many years to come. I welcome your comments.
David Beazley
Chicago, Illinois
June, 2009

Tony Gaddis

1. Python (Computer program language)


e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 2.50
 Pages
 632 p
 File Size 
 3,794 KB
 File Type
 PDF format
 ISBN-13
 ISBN-10
 978-0-13-257637-6
 0-13-257637-6
 Copyright©   
 2012, 2009 Pearson Education, Inc 

Preface
Welcome to Starting Out with Python, Second Edition. This book uses the Python language
to teach programming concepts and problem-solving skills, without assuming any previous
programming experience. With easy-to-understand examples, pseudocode, flowcharts, and
other tools, the student learns how to design the logic of programs and then implement
those programs using Python. This book is ideal for an introductory programming course
or a programming logic and design course using Python as the language.

As with all the books in the Starting Out With series, the hallmark of this text is its clear,
friendly, and easy-to-understand writing. In addition, it is rich in example programs that
are concise and practical. The programs in this book include short examples that highlight
specific programming topics, as well as more involved examples that focus on problem
solving. Each chapter provides one or more case studies that provide step-by-step analysis
of a specific problem and shows the student how to solve it.

Control Structures First, Then Classes
Python is a fully object-oriented programming language, but students do not have to understand
object-oriented concepts to start programming in Python. This text first introduces the student
to the fundamentals of data storage, input and output, control structures, functions, sequences
and lists, file I/O, and objects that are created from standard library classes. Then the student
learns to write classes, explores the topics of inheritance and polymorphism, and learns to write
recursive functions. Finally, the student learns to develop simple event-driven GUI applications.

Table of Contents
Preface xi
Chapter 1 Introduction to Computers and Programming 1
1.1 Introduction 1
1.2 Hardware and Software 2
1.3 How Computers Store Data 8
1.4 How a Program Works 13
1.5 Using Python 20
Chapter 2 Input, Processing, and Output 31
2.1 Designing a Program 31
2.2 Input, Processing, and Output 35
2.3 Displaying Output with the print Function 36
2.4 Comments 39
2.5 Variables 40
2.6 Reading Input from the Keyboard 49
2.7 Performing Calculations 53
2.8 More About Data Output 65
Chapter 3 Simple Functions 81
3.1 Introduction to Functions 81
3.2 Defining and Calling a Function 83
3.3 Designing a Program to Use Functions 89
3.4 Local Variables 95
3.5 Passing Arguments to Functions 97
3.6 Global Variables and Global Constants 107
Chapter 4 Decision Structures and Boolean Logic 117
4.1 The if Statement 117
4.2 The if-else Statement 125
4.3 Comparing Strings 130
4.4 Nested Decision Structures and the if-elif-else Statement 134
4.5 Logical Operators 142
4.6 Boolean Variables 149
Chapter 5 Repetition Structures 157
5.1 Introduction to Repetition Structures 157
5.2 The while Loop: a Condition-Controlled Loop 158
5.3 The for Loop: a Count-Controlled Loop 167
5.4 Calculating a Running Total 179
5.5 Sentinels 182
5.6 Input Validation Loops 185
5.7 Nested Loops 190
Chapter 6 Value-Returning Functions and Modules 203
6.1 Introduction to Value-Returning Functions:
Generating Random Numbers 203
6.2 Writing Your Own Value-Returning Functions 214
6.3 The math Module 225
6.4 Storing Functions in Modules 228
Chapter 7 Files and Exceptions 239
7.1 Introduction to File Input and Output 239
7.2 Using Loops to Process Files 256
7.3 Processing Records 263
7.4 Exceptions 276
Chapter 8 Lists and Tuples 295
8.1 Sequences 295
8.2 Introduction to Lists 295
8.3 List Slicing 303
8.4 Finding Items in Lists with the in Operator 306
8.5 List Methods and Useful Built-in Functions 307
8.6 Copying Lists 314
8.7 Processing Lists 316
8.8 Two-Dimensional Lists 328
8.9 Tuples 332
Chapter 9 More About Strings 341
9.1 Basic String Operations 341
9.2 String Slicing 349
9.3 Testing, Searching, and Manipulating Strings 353
Chapter 10 Dictionaries and Sets 371
10.1 Dictionaries 371
10.2 Sets 394
10.3 Serializing Objects 406
Chapter 11 Classes and Object-Oriented Programming 421
11.1 Procedural and Object-Oriented Programming 421
11.2 Classes 425
11.3 Working with Instances 442
11.4 Techniques for Designing Classes 464
Chapter 12 Inheritance 483
12.1 Introduction to Inheritance 483
12.2 Polymorphism 498
Chapter 13 Recursion 509
13.1 Introduction to Recursion 509
13.2 Problem Solving with Recursion 512
13.3 Examples of Recursive Algorithms 516
Chapter 14 GUI Programming 529
14.1 Graphical User Interfaces 529
14.2 Using the tkinter Module 531
14.3 Display Text with Label Widgets 534
14.4 Organizing Widgets with Frames 537
14.5 Button Widgets and Info Dialog Boxes 540
14.6 Getting Input with the Entry Widget 543
14.7 Using Labels as Output Fields 546
14.8 Radio Buttons and Check Buttons 554
Appendix A Installing Python 567
Appendix B Introduction to IDLE 569
Appendix C The ASCII Character Set 577
Appendix D Answers to Checkpoints 579
Index 595


Bookscreen
e-books shop

About the Author
Tony Gaddis is the principal author of the Starting Out With series of textbooks. Tony has
nearly two decades of experience teaching computer science courses, primarily at Haywood
Community College. He is a highly acclaimed instructor who was previously selected as the
North Carolina Community College “Teacher of the Year” and has received the Teaching
Excellence award from the National Institute for Staff and Organizational Development.
The Starting Out With series includes introductory books covering C++, Java™, Microsoft®
Visual Basic®, Microsoft® C#®, Python®, Programming Logic and Design, and Alice, all
published by Addison-Wesley. More information about all these books can be found at
www.pearsonhighered.com/gaddisbooks.

Doug Hellmann

Contents at a Glance

Chapter 1 Text 
Chapter 2 Data Structures
Chapter 3 Algorithms 
Chapter 4 Dates and Times 
Chapter 5 Mathematics 
Chapter 6 The File System
Chapter 7 Data Persistence and Exchange
Chapter 8 Data Compression and Archiving
Chapter 9 Cryptography
Chapter 10 Concurrency with Processes, Threads, and Coroutines
Chapter 11 Networking
Chapter 12 The Internet
Chapter 13 Email 
Chapter 14 Application Building Blocks
Chapter 15 Internationalization and Localization 
Chapter 16 Developer Tools
Chapter 17 Runtime Features
Chapter 18 Language Tools
Chapter 19 Modules and Packages 
Appendix A Porting Notes
Appendix B Outside of the Standard Library
Index of Python Modules 
Index

e-books shop
e-books shop
Purchase Now !
Just with Paypal



Book Details
 Price
 4.00
 Pages
 1454 p
 File Size 
 14,484 KB
 File Type
 PDF format
 ISBN-13
 ISBN-10
 978-0-13-429105-5
 0-13-429105-0
 Copyright©   
 2017 Pearson Education, Inc  

About the Author
Doug Hellmann is currently employed by Red Hat to work
on OpenStack. He is on the OpenStack Technical Committee
and contributes to many aspects of the project. He has been
programming in Python since version 1.4, and has worked on
a variety of Unix and non-Unix platforms for projects in fields
such as mapping, medical news publishing, banking, and data
center automation. Doug is a Fellow of the Python Software
Foundation, and served as its Communications Director from
2010 to 2012. After a year as a regular columnist for Python
Magazine, he served as Editor-in-Chief from 2008 to 2009.
Between 2007 and 2011, Doug published the popular Python Module of the Week series
on his blog, and an earlier version of this book for Python 2 called The Python Standard
Library by Example. He lives in Athens, Georgia.

Introduction
Distributed with every copy of Python, the standard library contains hundreds of modules
that provide tools for interacting with the operating system, interpreter, and Internet—all
of them tested and ready to be used to jump-start the development of your applications.

This book presents selected examples demonstrating how to use the most commonly used
features of the modules that support Python’s “batteries included” slogan, taken from the
popular Python Module of the Week (PyMOTW) blog series.

This Book's Target Audience
The audience for this book consists of intermediate-level Python programmers. Thus,
although all of the source code is presented with discussion, only a few cases include lineby-
line explanations. Every section focuses on the features of the modules, illustrated by
the source code and output from fully independent example programs. Each feature is
presented as concisely as possible, so the reader can focus on the module or function being
demonstrated without being distracted by the supporting code.

An experienced programmer who is familiar with other languages may be able to learn
Python from this book, but the text is not intended to be an introduction to the language.
Some prior experience writing Python programs will be useful when studying the examples.
Several sections, such as the description of network programming with sockets or hmac
encryption, require domain-specific knowledge. The basic information needed to explain the
examples is included here, but the range of topics covered by the modules in the standard
library makes it impossible to cover every topic comprehensively in a single volume. The
discussion of each module is followed by a list of suggested sources for more information and
further reading, including online resources, RFC standards documents, and related books.

Table of Contents
Introduction xxxi
Acknowledgments xxxiii
About the Author xxxv
Chapter 1 Text 1
1.1 string: Text Constants and Templates 1
1.1.1 Functions 1
1.1.2 Templates 2
1.1.3 Advanced Templates 4
1.1.4 Formatter 6
1.1.5 Constants 6
1.2 textwrap: Formatting Text Paragraphs 7
1.2.1 Example Data 8
1.2.2 Filling Paragraphs 8
1.2.3 Removing Existing Indentation 8
1.2.4 Combining Dedent and Fill 9
1.2.5 Indenting Blocks 10
1.2.6 Hanging Indents 12
1.2.7 Truncating Long Text 12
1.3 re: Regular Expressions 13
1.3.1 Finding Patterns in Text 14
1.3.2 Compiling Expressions 15
1.3.3 Multiple Matches 16
1.3.4 Pattern Syntax 17
1.3.5 Constraining the Search 28
1.3.6 Dissecting Matches with Groups 30
1.3.7 Search Options 36
1.3.8 Looking Ahead or Behind 44
1.3.9 Self-referencing Expressions 48
1.3.10 Modifying Strings with Patterns 53
1.3.11 Splitting with Patterns 55
1.4 difflib: Compare Sequences 58
1.4.1 Comparing Bodies of Text 58
1.4.2 Junk Data 61
1.4.3 Comparing Arbitrary Types 62
Chapter 2 Data Structures 65
2.1 enum: Enumeration Type 66
2.1.1 Creating Enumerations 66
2.1.2 Iteration 67
2.1.3 Comparing Enums 67
2.1.4 Unique Enumeration Values 69
2.1.5 Creating Enumerations Programmatically 71
2.1.6 Non-integer Member Values 72
2.2 collections: Container Data Types 75
2.2.1 ChainMap: Search Multiple Dictionaries 75
2.2.2 Counter: Count Hashable Objects 79
2.2.3 defaultdict: Missing Keys Return a Default Value 83
2.2.4 deque: Double-Ended Queue 84
2.2.5 namedtuple: Tuple Subclass with Named Fields 89
2.2.6 OrderedDict: Remember the Order Keys Are Added to a Dictionary 94
2.2.7 collections.abc: Abstract Base Classes for Containers 97
2.3 array: Sequence of Fixed-Type Data 98
2.3.1 Initialization 98
2.3.2 Manipulating Arrays 99
2.3.3 Arrays and Files 100
2.3.4 Alternative Byte Ordering 101
2.4 heapq: Heap Sort Algorithm 103
2.4.1 Example Data 103
2.4.2 Creating a Heap 104
2.4.3 Accessing the Contents of a Heap 105
2.4.4 Data Extremes from a Heap 107
2.4.5 Efficiently Merging Sorted Sequences 108
2.5 bisect: Maintain Lists in Sorted Order 109
2.5.1 Inserting in Sorted Order 109
2.5.2 Handling Duplicates 110
2.6 queue: Thread-Safe FIFO Implementation 111
2.6.1 Basic FIFO Queue 112
2.6.2 LIFO Queue 112
2.6.3 Priority Queue 113
2.6.4 Building a Threaded Podcast Client 114
2.7 struct: Binary Data Structures 117
2.7.1 Functions Versus Struct Class 117
2.7.2 Packing and Unpacking 117
2.7.3 Endianness 118
2.7.4 Buffers 120
2.8 weakref: Impermanent References to Objects 121
2.8.1 References 122
2.8.2 Reference Callbacks 122
2.8.3 Finalizing Objects 123
2.8.4 Proxies 126
2.8.5 Caching Objects 127
2.9 copy: Duplicate Objects 130
2.9.1 Shallow Copies 130
2.9.2 Deep Copies 131
2.9.3 Customizing Copy Behavior 132
2.9.4 Recursion in Deep Copy 133
2.10 pprint: Pretty-Print Data Structures 136
2.10.1 Printing 136
2.10.2 Formatting 137
2.10.3 Arbitrary Classes 138
2.10.4 Recursion 139
2.10.5 Limiting Nested Output 139
2.10.6 Controlling Output Width 140
Chapter 3 Algorithms 143
3.1 functools: Tools for Manipulating Functions 143
3.1.1 Decorators 143
3.1.2 Comparison 151
3.1.3 Caching 155
3.1.4 Reducing a Data Set 158
3.1.5 Generic Functions 161
3.2 itertools: Iterator Functions 163
3.2.1 Merging and Splitting Iterators 164
3.2.2 Converting Inputs 167
3.2.3 Producing New Values 169
3.2.4 Filtering 172
3.2.5 Grouping Data 175
3.2.6 Combining Inputs 176
3.3 operator: Functional Interface to Built-in Operators 183
3.3.1 Logical Operations 183
3.3.2 Comparison Operators 184
3.3.3 Arithmetic Operators 184
3.3.4 Sequence Operators 186
3.3.5 In-Place Operators 187
3.3.6 Attribute and Item “Getters” 188
3.3.7 Combining Operators and Custom Classes 190
3.4 contextlib: Context Manager Utilities 191
3.4.1 Context Manager API 191
3.4.2 Context Managers as Function Decorators 194
3.4.3 From Generator to Context Manager 196
3.4.4 Closing Open Handles 198
3.4.5 Ignoring Exceptions 199
3.4.6 Redirecting Output Streams 201
3.4.7 Dynamic Context Manager Stacks 202
Chapter 4 Dates and Times 211
4.1 time: Clock Time 211
4.1.1 Comparing Clocks 211
4.1.2 Wall Clock Time 213
4.1.3 Monotonic Clocks 214
4.1.4 Processor Clock Time 214
4.1.5 Performance Counter 216
4.1.6 Time Components 217
4.1.7 Working with Time Zones 218
4.1.8 Parsing and Formatting Times 219
4.2 datetime: Date and Time Value Manipulation 221
4.2.1 Times 221
4.2.2 Dates 222
4.2.3 timedeltas 225
4.2.4 Date Arithmetic 226
4.2.5 Comparing Values 228
4.2.6 Combining Dates and Times 228
4.2.7 Formatting and Parsing 230
4.2.8 Time Zones 231
4.3 calendar: Work with Dates 233
4.3.1 Formatting Examples 233
4.3.2 Locales 236
4.3.3 Calculating Dates 236
Chapter 5 Mathematics 239
5.1 decimal: Fixed- and Floating-Point Math 239
5.1.1 Decimal 239
5.1.2 Formatting 241
5.1.3 Arithmetic 242
5.1.4 Special Values 243
5.1.5 Context 244
5.2 fractions: Rational Numbers 250
5.2.1 Creating Fraction Instances 250
5.2.2 Arithmetic 252
5.2.3 Approximating Values 253
5.3 random: Pseudorandom Number Generators 254
5.3.1 Generating Random Numbers 254
5.3.2 Seeding 255
5.3.3 Saving State 255
5.3.4 Random Integers 257
5.3.5 Picking Random Items 258
5.3.6 Permutations 258
5.3.7 Sampling 260
5.3.8 Multiple Simultaneous Generators 261
5.3.9 SystemRandom 262
5.3.10 Non-uniform Distributions 263
5.4 math: Mathematical Functions 264
5.4.1 Special Constants 265
5.4.2 Testing for Exceptional Values 265
5.4.3 Comparing 267
5.4.4 Converting Floating-Point Values to Integers 270
5.4.5 Alternative Representations of Floating-Point Values 271
5.4.6 Positive and Negative Signs 272
5.4.7 Commonly Used Calculations 274
5.4.8 Exponents and Logarithms 278
5.4.9 Angles 282
5.4.10 Trigonometry 284
5.4.11 Hyperbolic Functions 288
5.4.12 Special Functions 289
5.5 statistics: Statistical Calculations 290
5.5.1 Averages 290
5.5.2 Variance 292
Chapter 6 The File System 295
6.1 os.path: Platform-Independent Manipulation of Filenames 296
6.1.1 Parsing Paths 296
6.1.2 Building Paths 300
6.1.3 Normalizing Paths 301
6.1.4 File Times 302
6.1.5 Testing Files 303
6.2 pathlib: File System Paths as Objects 305
6.2.1 Path Representations 305
6.2.2 Building Paths 305
6.2.3 Parsing Paths 307
6.2.4 Creating Concrete Paths 309
6.2.5 Directory Contents 309
6.2.6 Reading and Writing Files 312
6.2.7 Manipulating Directories and Symbolic Links 312
6.2.8 File Types 313
6.2.9 File Properties 315
6.2.10 Permissions 317
6.2.11 Deleting 318
6.3 glob: Filename Pattern Matching 319
6.3.1 Example Data 320
6.3.2 Wildcards 320
6.3.3 Single-Character Wildcard 321
6.3.4 Character Ranges 322
6.3.5 Escaping Meta-characters 322
6.4 fnmatch: Unix-Style Glob Pattern Matching 323
6.4.1 Simple Matching 323
6.4.2 Filtering 325
6.4.3 Translating Patterns 325
6.5 linecache: Read Text Files Efficiently 326
6.5.1 Test Data 326
6.5.2 Reading Specific Lines 327
6.5.3 Handling Blank Lines 328
6.5.4 Error Handling 328
6.5.5 Reading Python Source Files 329
6.6 tempfile: Temporary File System Objects 330
6.6.1 Temporary Files 331
6.6.2 Named Files 333
6.6.3 Spooled Files 333
6.6.4 Temporary Directories 335
6.6.5 Predicting Names 335
6.6.6 Temporary File Location 336
6.7 shutil: High-Level File Operations 337
6.7.1 Copying Files 337
6.7.2 Copying File Metadata 340
6.7.3 Working with Directory Trees 342
6.7.4 Finding Files 345
6.7.5 Archives 346
6.7.6 File System Space 350
6.8 filecmp: Compare Files 351
6.8.1 Example Data 351
6.8.2 Comparing Files 353
6.8.3 Comparing Directories 355
6.8.4 Using Differences in a Program 357
6.9 mmap: Memory-Map Files 361
6.9.1 Reading 361
6.9.2 Writing 362
6.9.3 Regular Expressions 364
6.10 codecs: String Encoding and Decoding 365
6.10.1 Unicode Primer 365
6.10.2 Working with Files 368
6.10.3 Byte Order 370
6.10.4 Error Handling 372
6.10.5 Encoding Translation 376
6.10.6 Non-Unicode Encodings 377
6.10.7 Incremental Encoding 378
6.10.8 Unicode Data and Network Communication 380
6.10.9 Defining a Custom Encoding 383
6.11 io: Text, Binary, and Raw Stream I/O Tools 390
6.11.1 In-Memory Streams 390
6.11.2 Wrapping Byte Streams for Text Data 392
Chapter 7 Data Persistence and Exchange 395
7.1 pickle: Object Serialization 396
7.1.1 Encoding and Decoding Data in Strings 396
7.1.2 Working with Streams 397
7.1.3 Problems Reconstructing Objects 399
7.1.4 Unpicklable Objects 400
7.1.5 Circular References 402
7.2 shelve: Persistent Storage of Objects 405
7.2.1 Creating a New Shelf 405
7.2.2 Writeback 406
7.2.3 Specific Shelf Types 408
7.3 dbm: Unix Key–Value Databases 408
7.3.1 Database Types 408
7.3.2 Creating a New Database 409
7.3.3 Opening an Existing Database 410
7.3.4 Error Cases 411
7.4 sqlite3: Embedded Relational Database 412
7.4.1 Creating a Database 412
7.4.2 Retrieving Data 415
7.4.3 Query Metadata 417
7.4.4 Row Objects 417
7.4.5 Using Variables with Queries 419
7.4.6 Bulk Loading 421
7.4.7 Defining New Column Types 422
7.4.8 Determining Types for Columns 426
7.4.9 Transactions 428
7.4.10 Isolation Levels 431
7.4.11 In-Memory Databases 434
7.4.12 Exporting the Contents of a Database 435
7.4.13 Using Python Functions in SQL 436
7.4.14 Querying with Regular Expressions 439
7.4.15 Custom Aggregation 440
7.4.16 Threading and Connection Sharing 441
7.4.17 Restricting Access to Data 442
7.5 xml.etree.ElementTree: XML Manipulation API 445
7.5.1 Parsing an XML Document 445
7.5.2 Traversing the Parsed Tree 446
7.5.3 Finding Nodes in a Document 447
7.5.4 Parsed Node Attributes 449
7.5.5 Watching Events While Parsing 451
7.5.6 Creating a Custom Tree Builder 453
7.5.7 Parsing Strings 455
7.5.8 Building Documents With Element Nodes 457
7.5.9 Pretty-Printing XML 458
7.5.10 Setting Element Properties 459
7.5.11 Building Trees from Lists of Nodes 461
7.5.12 Serializing XML to a Stream 464
7.6 csv: Comma-Separated Value Files 466
7.6.1 Reading 466
7.6.2 Writing 467
7.6.3 Dialects 469
7.6.4 Using Field Names 474
Chapter 8 Data Compression and Archiving 477
8.1 zlib: GNU zlib Compression 477
8.1.1 Working with Data in Memory 477
8.1.2 Incremental Compression and Decompression 479
8.1.3 Mixed Content Streams 480
8.1.4 Checksums 481
8.1.5 Compressing Network Data 482
8.2 gzip: Read and Write GNU zip Files 486
8.2.1 Writing Compressed Files 486
8.2.2 Reading Compressed Data 489
8.2.3 Working with Streams 490
8.3 bz2: bzip2 Compression 491
8.3.1 One-Shot Operations in Memory 492
8.3.2 Incremental Compression and Decompression 493
8.3.3 Mixed-Content Streams 494
8.3.4 Writing Compressed Files 495
8.3.5 Reading Compressed Files 497
8.3.6 Reading and Writing Unicode Data 498
8.3.7 Compressing Network Data 499
8.4 tarfile: Tar Archive Access 503
8.4.1 Testing Tar Files 503
8.4.2 Reading Metadata from an Archive 504
8.4.3 Extracting Files from an Archive 506
8.4.4 Creating New Archives 508
8.4.5 Using Alternative Archive Member Names 508
8.4.6 Writing Data from Sources Other Than Files 509
8.4.7 Appending to Archives 510
8.4.8 Working with Compressed Archives 510
8.5 zipfile: ZIP Archive Access 511
8.5.1 Testing ZIP Files 512
8.5.2 Reading Metadata from an Archive 512
8.5.3 Extracting Archived Files From an Archive 514
8.5.4 Creating New Archives 514
8.5.5 Using Alternative Archive Member Names 516
8.5.6 Writing Data from Sources Other Than Files 517
8.5.7 Writing with a ZipInfo Instance 517
8.5.8 Appending to Files 518
8.5.9 Python ZIP Archives 519
8.5.10 Limitations 521
Chapter 9 Cryptography 523
9.1 hashlib: Cryptographic Hashing 523
9.1.1 Hash Algorithms 523
9.1.2 Sample Data 524
9.1.3 MD5 Example 524
9.1.4 SHA1 Example 525
9.1.5 Creating a Hash by Name 525
9.1.6 Incremental Updates 526
9.2 hmac: Cryptographic Message Signing and Verification 528
9.2.1 Signing Messages 528
9.2.2 Alternative Digest Types 528
9.2.3 Binary Digests 529
9.2.4 Applications of Message Signatures 530
Chapter 10 Concurrency with Processes, Threads, and Coroutines 535
10.1 subprocess: Spawning Additional Processes 535
10.1.1 Running External Command 536
10.1.2 Working with Pipes Directly 542
10.1.3 Connecting Segments of a Pipe 545
10.1.4 Interacting with Another Command 546
10.1.5 Signaling Between Processes 548
10.2 signal: Asynchronous System Events 553
10.2.1 Receiving Signals 554
10.2.2 Retrieving Registered Handlers 555
10.2.3 Sending Signals 556
10.2.4 Alarms 556
10.2.5 Ignoring Signals 557
10.2.6 Signals and Threads 558
10.3 threading: Manage Concurrent Operations Within a Process 560
10.3.1 Thread Objects 560
10.3.2 Determining the Current Thread 562
10.3.3 Daemon Versus Non-daemon Threads 564
10.3.4 Enumerating All Threads 567
10.3.5 Subclassing Thread 568
10.3.6 Timer Threads 570
10.3.7 Signaling Between Threads 571
10.3.8 Controlling Access to Resources 572
10.3.9 Synchronizing Threads 578
10.3.10 Limiting Concurrent Access to Resources 581
10.3.11 Thread Specific Data 583
10.4 multiprocessing: Manage Processes Like Threads 586
10.4.1 multiprocessing Basics 586
10.4.2 Importable Target Functions 587
10.4.3 Determining the Current Process 588
10.4.4 Daemon Processes 589
10.4.5 Waiting for Processes 591
10.4.6 Terminating Processes 593
10.4.7 Process Exit Status 594
10.4.8 Logging 596
10.4.9 Subclassing Process 597
10.4.10 Passing Messages to Processes 598
10.4.11 Signaling Between Processes 602
10.4.12 Controlling Access to Resources 603
10.4.13 Synchronizing Operations 604
10.4.14 Controlling Concurrent Access to Resources 605
10.4.15 Managing Shared State 608
10.4.16 Shared Namespaces 608
10.4.17 Process Pools 611
10.4.18 Implementing MapReduce 613
10.5 asyncio: Asynchronous I/O, Event Loop, and Concurrency Tools 617
10.5.1 Asynchronous Concurrency Concepts 618
10.5.2 Cooperative Multitasking with Coroutines 618
10.5.3 Scheduling Calls to Regular Functions 622
10.5.4 Producing Results Asynchronously 625
10.5.5 Executing Tasks Concurrently 628
10.5.6 Composing Coroutines with Control Structures 632
10.5.7 Synchronization Primitives 637
10.5.8 Asynchronous I/O with Protocol Class Abstractions 644
10.5.9 Asynchronous I/O Using Coroutines and Streams 650
10.5.10 Using SSL 656
10.5.11 Interacting with Domain Name Services 658
10.5.12 Working with Subprocesses 661
10.5.13 Receiving Unix Signals 668
10.5.14 Combining Coroutines with Threads and Processes 670
10.5.15 Debugging with asyncio 673
10.6 concurrent.futures: Manage Pools of Concurrent Tasks 677
10.6.1 Using map() with a Basic Thread Pool 677
10.6.2 Scheduling Individual Tasks 678
10.6.3 Waiting for Tasks in Any Order 679
10.6.4 Future Callbacks 680
10.6.5 Canceling Tasks 681
10.6.6 Exceptions in Tasks 683
10.6.7 Context Manager 683
10.6.8 Process Pools 684
Chapter 11 Networking 687
11.1 ipaddress: Internet Addresses 687
11.1.1 Addresses 687
11.1.2 Networks 688
11.1.3 Interfaces 692
11.2 socket: Network Communication 693
11.2.1 Addressing, Protocol Families, and Socket Types 693
11.2.2 TCP/IP Client and Server 704
11.2.3 User Datagram Client and Server 711
11.2.4 Unix Domain Sockets 714
11.2.5 Multicast 717
11.2.6 Sending Binary Data 721
11.2.7 Non-blocking Communication and Timeouts 723
11.3 selectors: I/O Multiplexing Abstractions 724
11.3.1 Operating Model 724
11.3.2 Echo Server 724
11.3.3 Echo Client 726
11.3.4 Server and Client Together 727
11.4 select: Wait for I/O Efficiently 728
11.4.1 Using select() 729
11.4.2 Non-blocking I/O with Timeouts 734
11.4.3 Using poll() 737
11.4.4 Platform-Specific Options 742
11.5 socketserver: Creating Network Servers 742
11.5.1 Server Types 742
11.5.2 Server Objects 743
11.5.3 Implementing a Server 743
11.5.4 Request Handlers 743
11.5.5 Echo Example 744
11.5.6 Threading and Forking 749
Chapter 12 The Internet 753
12.1 urllib.parse: Split URLs into Components 753
12.1.1 Parsing 754
12.1.2 Unparsing 756
12.1.3 Joining 758
12.1.4 Encoding Query Arguments 759
12.2 urllib.request: Network Resource Access 761
12.2.1 HTTP GET 761
12.2.2 Encoding Arguments 763
12.2.3 HTTP POST 764
12.2.4 Adding Outgoing Headers 765
12.2.5 Posting Form Data from a Request 766
12.2.6 Uploading Files 767
12.2.7 Creating Custom Protocol Handlers 770
12.3 urllib.robotparser: Internet Spider Access Control 773
12.3.1 robots.txt 773
12.3.2 Testing Access Permissions 774
12.3.3 Long-Lived Spiders 775
12.4 base64: Encode Binary Data with ASCII 776
12.4.1 Base 64 Encoding 777
12.4.2 Base64 Decoding 778
12.4.3 URL-Safe Variations 778
12.4.4 Other Encodings 779
12.5 http.server: Base Classes for Implementing Web Servers 781
12.5.1 HTTP GET 781
12.5.2 HTTP POST 784
12.5.3 Threading and Forking 786
12.5.4 Handling Errors 787
12.5.5 Setting Headers 788
12.5.6 Command-Line Use 789
12.6 http.cookies: HTTP Cookies 790
12.6.1 Creating and Setting a Cookie 790
12.6.2 Morsels 791
12.6.3 Encoded Values 793
12.6.4 Receiving and Parsing Cookie Headers 794
12.6.5 Alternative Output Formats 795
12.7 webbrowser: Displays Web Pages 796
12.7.1 Simple Example 796
12.7.2 Windows Versus Tabs 796
12.7.3 Using a Specific Browser 796
12.7.4 BROWSER Variable 797
12.7.5 Command-Line Interface 797
12.8 uuid: Universally Unique Identifiers 797
12.8.1 UUID 1: IEEE 802 MAC Address 798
12.8.2 UUID 3 and 5: Name-Based Values 800
12.8.3 UUID 4: Random Values 802
12.8.4 Working with UUID Objects 802
12.9 json: JavaScript Object Notation 803
12.9.1 Encoding and Decoding Simple Data Types 804
12.9.2 Human-Consumable Versus Compact Output 805
12.9.3 Encoding Dictionaries 807
12.9.4 Working with Custom Types 807
12.9.5 Encoder and Decoder Classes 810
12.9.6 Working with Streams and Files 813
12.9.7 Mixed Data Streams 813
12.9.8 JSON at the Command Line 815
12.10 xmlrpc.client: Client Library for XML-RPC 816
12.10.1 Connecting to a Server 817
12.10.2 Data Types 819
12.10.3 Passing Objects 822
12.10.4 Binary Data 823
12.10.5 Exception Handling 825
12.10.6 Combining Calls into One Message 826
12.11 xmlrpc.server: An XML-RPC Server 827
12.11.1 A Simple Server 828
12.11.2 Alternate API Names 829
12.11.3 Dotted API Names 830
12.11.4 Arbitrary API Names 831
12.11.5 Exposing Methods of Objects 832
12.11.6 Dispatching Calls 834
12.11.7 Introspection API 837
Chapter 13 Email 841
13.1 smtplib: Simple Mail Transfer Protocol Client 841
13.1.1 Sending an Email Message 841
13.1.2 Authentication and Encryption 843
13.1.3 Verifying an Email Address 846
13.2 smtpd: Sample Mail Servers 847
13.2.1 Mail Server Base Class 847
13.2.2 Debugging Server 850
13.2.3 Proxy Server 851
13.3 mailbox: Manipulate Email Archives 852
13.3.1 mbox 852
13.3.2 Maildir 855
13.3.3 Message Flags 862
13.3.4 Other Formats 864
13.4 imaplib: IMAP4 Client Library 864
13.4.1 Variations 864
13.4.2 Connecting to a Server 864
13.4.3 Example Configuration 866
13.4.4 Listing Mailboxes 866
13.4.5 Mailbox Status 869
13.4.6 Selecting a Mailbox 871
13.4.7 Searching for Messages 872
13.4.8 Search Criteria 872
13.4.9 Fetching Messages 874
13.4.10 Whole Messages 880
13.4.11 Uploading Messages 881
13.4.12 Moving and Copying Messages 883
13.4.13 Deleting Messages 884
Chapter 14 Application Building Blocks 887
14.1 argparse: Command-Line Option and Argument Parsing 888
14.1.1 Setting Up a Parser 888
14.1.2 Defining Arguments 888
14.1.3 Parsing a Command Line 889
14.1.4 Simple Examples 889
14.1.5 Help Output 897
14.1.6 Parser Organization 901
14.1.7 Advanced Argument Processing 908
14.2 getopt: Command-Line Option Parsing 916
14.2.1 Function Arguments 916
14.2.2 Short-Form Options 917
14.2.3 Long-Form Options 917
14.2.4 A Complete Example 918
14.2.5 Abbreviating Long-Form Options 920
14.2.6 GNU-Style Option Parsing 920
14.2.7 Ending Argument Processing 922
14.3 readline: The GNU readline Library 922
14.3.1 Configuring readline 923
14.3.2 Completing Text 924
14.3.3 Accessing the Completion Buffer 927
14.3.4 Input History 931
14.3.5 Hooks 934
14.4 getpass: Secure Password Prompt 935
14.4.1 Example 935
14.4.2 Using getpass Without a Terminal 937
14.5 cmd: Line-Oriented Command Processors 938
14.5.1 Processing Commands 938
14.5.2 Command Arguments 940
14.5.3 Live Help 941
14.5.4 Auto-Completion 942
14.5.5 Overriding Base Class Methods 944
14.5.6 Configuring Cmd Through Attributes 946
14.5.7 Running Shell Commands 947
14.5.8 Alternative Inputs 948
14.5.9 Commands from sys.argv 950
14.6 shlex: Parse Shell-Style Syntaxes 951
14.6.1 Parsing Quoted Strings 951
14.6.2 Making Safe Strings for Shells 953
14.6.3 Embedded Comments 954
14.6.4 Splitting Strings into Tokens 954
14.6.5 Including Other Sources of Tokens 955
14.6.6 Controlling the Parser 956
14.6.7 Error Handling 957
14.6.8 POSIX Versus Non-POSIX Parsing 959
14.7 configparser: Work with Configuration Files 960
14.7.1 Configuration File Format 961
14.7.2 Reading Configuration Files 961
14.7.3 Accessing Configuration Settings 963
14.7.4 Modifying Settings 970
14.7.5 Saving Configuration Files 972
14.7.6 Option Search Path 972
14.7.7 Combining Values with Interpolation 975
14.8 logging: Report Status, Error, and Informational Messages 980
14.8.1 Logging Components 980
14.8.2 Logging in Applications Versus Libraries 980
14.8.3 Logging to a File 981
14.8.4 Rotating Log Files 981
14.8.5 Verbosity Levels 982
14.8.6 Naming Logger Instances 984
14.8.7 The Logging Tree 984
14.8.8 Integration with the warnings Module 985
14.9 fileinput: Command-Line Filter Framework 986
14.9.1 Converting M3U Files to RSS 987
14.9.2 Progress Metadata 989
14.9.3 In-Place Filtering 990
14.10 atexit: Program Shutdown Callbacks 993
14.10.1 Registering Exit Callbacks 993
14.10.2 Decorator Syntax 994
14.10.3 Canceling Callbacks 994
14.10.4 When Are atexit Callbacks Not Called? 995
14.10.5 Handling Exceptions 997
14.11 sched: Timed Event Scheduler 998
14.11.1 Running Events with a Delay 999
14.11.2 Overlapping Events 1000
14.11.3 Event Priorities 1001
14.11.4 Canceling Events 1001
Chapter 15 Internationalization and Localization 1003
15.1 gettext: Message Catalogs 1003
15.1.1 Translation Workflow Overview 1003
15.1.2 Creating Message Catalogs from Source Code 1004
15.1.3 Finding Message Catalogs at Runtime 1007
15.1.4 Plural Values 1008
15.1.5 Application Versus Module Localization 1011
15.1.6 Switching Translations 1012
15.2 locale: Cultural Localization API 1012
15.2.1 Probing the Current Locale 1013
15.2.2 Currency 1018
15.2.3 Formatting Numbers 1019
15.2.4 Parsing Numbers 1021
15.2.5 Dates and Times 1022
Chapter 16 Developer Tools 1023
16.1 pydoc: Online Help for Modules 1024
16.1.1 Plain Text Help 1024
16.1.2 HTML Help 1025
16.1.3 Interactive Help 1026
16.2 doctest: Testing Through Documentation 1026
16.2.1 Getting Started 1026
16.2.2 Handling Unpredictable Output 1028
16.2.3 Tracebacks 1032
16.2.4 Working Around Whitespace 1034
16.2.5 Test Locations 1039
16.2.6 External Documentation 1042
16.2.7 Running Tests 1044
16.2.8 Test Context 1048
16.3 unittest: Automated Testing Framework 1051
16.3.1 Basic Test Structure 1051
16.3.2 Running Tests 1051
16.3.3 Test Outcomes 1052
16.3.4 Asserting Truth 1054
16.3.5 Testing Equality 1054
16.3.6 Almost Equal? 1055
16.3.7 Containers 1056
16.3.8 Testing for Exceptions 1061
16.3.9 Test Fixtures 1062
16.3.10 Repeating Tests with Different Inputs 1065
16.3.11 Skipping Tests 1066
16.3.12 Ignoring Failing Tests 1068
16.4 trace: Follow Program Flow 1069
16.4.1 Example Program 1069
16.4.2 Tracing Execution 1069
16.4.3 Code Coverage 1070
16.4.4 Calling Relationships 1073
16.4.5 Programming Interface 1074
16.4.6 Saving Result Data 1076
16.4.7 Options 1077
16.5 traceback: Exceptions and Stack Traces 1078
16.5.1 Supporting Functions 1079
16.5.2 Examining the Stack 1079
16.5.3 TracebackException 1081
16.5.4 Low-Level Exception APIs 1082
16.5.5 Low-Level Stack APIs 1086
16.6 cgitb: Detailed Traceback Reports 1089
16.6.1 Standard Traceback Dumps 1089
16.6.2 Enabling Detailed Tracebacks 1090
16.6.3 Local Variables in Tracebacks 1093
16.6.4 Exception Properties 1096
16.6.5 HTML Output 1098
16.6.6 Logging Tracebacks 1098
16.7 pdb: Interactive Debugger 1101
16.7.1 Starting the Debugger 1101
16.7.2 Controlling the Debugger 1104
16.7.3 Breakpoints 1117
16.7.4 Changing Execution Flow 1129
16.7.5 Customizing the Debugger with Aliases 1136
16.7.6 Saving Configuration Settings 1137
16.8 profile and pstats: Performance Analysis 1140
16.8.1 Running the Profiler 1140
16.8.2 Running in a Context 1143
16.8.3 pstats: Saving and Working with Statistics 1144
16.8.4 Limiting Report Contents 1145
16.8.5 Caller/Callee Graphs 1146
16.9 timeit: Time the Execution of Small Bits of Python Code 1148
16.9.1 Module Contents 1148
16.9.2 Basic Example 1148
16.9.3 Storing Values in a Dictionary 1149
16.9.4 From the Command Line 1152
16.10 tabnanny: Indentation Validator 1153
16.10.1 Running from the Command Line 1153
16.11 compileall: Byte-Compile Source Files 1155
16.11.1 Compiling One Directory 1155
16.11.2 Ignoring Files 1156
16.11.3 Compiling sys.path 1157
16.11.4 Compiling Individual Files 1157
16.11.5 From the Command Line 1158
16.12 pyclbr: Class Browser 1160
16.12.1 Scanning for Classes 1161
16.12.2 Scanning for Functions 1162
16.13 venv: Create Virtual Environments 1163
16.13.1 Creating Environments 1163
16.13.2 Contents of a Virtual Environment 1164
16.13.3 Using Virtual Environments 1165
16.14 ensurepip: Install the Python Package Installer 1167
16.14.1 Installing pip 1167
Chapter 17 Runtime Features 1169
17.1 site: Site-wide Configuration 1169
17.1.1 Import Path 1169
17.1.2 User Directories 1171
17.1.3 Path Configuration Files 1172
17.1.4 Customizing Site Configuration 1175
17.1.5 Customizing User Configuration 1176
17.1.6 Disabling the site Module 1177
17.2 sys: System-Specific Configuration 1178
17.2.1 Interpreter Settings 1178
17.2.2 Runtime Environment 1185
17.2.3 Memory Management and Limits 1187
17.2.4 Exception Handling 1194
17.2.5 Low-Level Thread Support 1197
17.2.6 Modules and Imports 1200
17.2.7 Tracing a Program As It Runs 1221
17.3 os: Portable Access to Operating System–Specific Features 1227
17.3.1 Examining the File System Contents 1228
17.3.2 Managing File System Permissions 1230
17.3.3 Creating and Deleting Directories 1233
17.3.4 Working with Symbolic Links 1234
17.3.5 Safely Replacing an Existing File 1234
17.3.6 Detecting and Changing the Process Owner 1235
17.3.7 Managing the Process Environment 1237
17.3.8 Managing the Process Working Directory 1238
17.3.9 Running External Commands 1239
17.3.10 Creating Processes with os.fork() 1240
17.3.11 Waiting for Child Processes 1242
17.3.12 Spawning New Processes 1244
17.3.13 Operating System Error Codes 1245
17.4 platform: System Version Information 1246
17.4.1 Interpreter 1246
17.4.2 Platform 1247
17.4.3 Operating System and Hardware Information 1248
17.4.4 Executable Architecture 1250
17.5 resource: System Resource Management 1251
17.5.1 Current Usage 1251
17.5.2 Resource Limits 1252
17.6 gc: Garbage Collector 1254
17.6.1 Tracing References 1255
17.6.2 Forcing Garbage Collection 1258
17.6.3 Finding References to Objects That Cannot Be Collected 1259
17.6.4 Collection Thresholds and Generations 1261
17.6.5 Debugging 1265
17.7 sysconfig: Interpreter Compile-Time Configuration 1270
17.7.1 Configuration Variables 1270
17.7.2 Installation Paths 1272
17.7.3 Python Version and Platform 1276
Chapter 18 Language Tools 1279
18.1 warnings: Non-fatal Alerts 1279
18.1.1 Categories and Filtering 1280
18.1.2 Generating Warnings 1280
18.1.3 Filtering with Patterns 1281
18.1.4 Repeated Warnings 1283
18.1.5 Alternative Message Delivery Functions 1284
18.1.6 Formatting 1285
18.1.7 Stack Level in Warnings 1286
18.2 abc: Abstract Base Classes 1287
18.2.1 How ABCs Work 1287
18.2.2 Registering a Concrete Class 1287
18.2.3 Implementation Through Subclassing 1288
18.2.4 Helper Base Class 1289
18.2.5 Incomplete Implementations 1290
18.2.6 Concrete Methods in ABCs 1291
18.2.7 Abstract Properties 1292
18.2.8 Abstract Class and Static Methods 1295
18.3 dis: Python Byte-Code Disassembler 1296
18.3.1 Basic Disassembly 1297
18.3.2 Disassembling Functions 1297
18.3.3 Classes 1300
18.3.4 Source Code 1301
18.3.5 Using Disassembly to Debug 1302
18.3.6 Performance Analysis of Loops 1303
18.3.7 Compiler Optimizations 1309
18.4 inspect: Inspect Live Objects 1311
18.4.1 Example Module 1311
18.4.2 Inspecting Modules 1312
18.4.3 Inspecting Classes 1314
18.4.4 Inspecting Instances 1316
18.4.5 Documentation Strings 1316
18.4.6 Retrieving Source 1318
18.4.7 Method and Function Signatures 1319
18.4.8 Class Hierarchies 1322
18.4.9 Method Resolution Order 1323
18.4.10 The Stack and Frames 1324
18.4.11 Command-Line Interface 1327
Chapter 19 Modules and Packages 1329
19.1 importlib: Python’s Import Mechanism 1329
19.1.1 Example Package 1329
19.1.2 Module Types 1330
19.1.3 Importing Modules 1331
19.1.4 Loaders 1332
19.2 pkgutil: Package Utilities 1334
19.2.1 Package Import Paths 1334
19.2.2 Development Versions of Packages 1336
19.2.3 Managing Paths with PKG Files 1338
19.2.4 Nested Packages 1340
19.2.5 Package Data 1341
19.3 zipimport: Load Python Code from ZIP Archives 1344
19.3.1 Example 1344
19.3.2 Finding a Module 1345
19.3.3 Accessing Code 1345
19.3.4 Source 1346
19.3.5 Packages 1348
19.3.6 Data 1348
Appendix A Porting Notes 1351
A.1 References 1351
A.2 New Modules 1352
A.3 Renamed Modules 1352
A.4 Removed Modules 1354
A.4.1 bsddb 1354
A.4.2 commands 1354
A.4.3 compiler 1354
A.4.4 dircache 1354
A.4.5 EasyDialogs 1354
A.4.6 exceptions 1354
A.4.7 htmllib 1354
A.4.8 md5 1354
A.4.9 mimetools, MimeWriter, mimify, multifile, and rfc822 1354
A.4.10 popen2 1354
A.4.11 posixfile 1355
A.4.12 sets 1355
A.4.13 sha 1355
A.4.14 sre 1355
A.4.15 statvfs 1355
A.4.16 thread 1355
A.4.17 user 1355
A.5 Deprecated Modules 1355
A.5.1 asyncore and asynchat 1355
A.5.2 formatter 1355
A.5.3 imp 1356
A.5.4 optparse 1356
A.6 Summary of Changes to Modules 1356
A.6.1 abc 1356
A.6.2 anydbm 1356
A.6.3 argparse 1356
A.6.4 array 1357
A.6.5 atexit 1357
A.6.6 base64 1357
A.6.7 bz2 1357
A.6.8 collections 1357
A.6.9 comands 1357
A.6.10 configparser 1358
A.6.11 contextlib 1358
A.6.12 csv 1358
A.6.13 datetime 1358
A.6.14 decimal 1358
A.6.15 fractions 1358
A.6.16 gc 1358
A.6.17 gettext 1359
A.6.18 glob 1359
A.6.19 http.cookies 1359
A.6.20 imaplib 1359
A.6.21 inspect 1359
A.6.22 itertools 1359
A.6.23 json 1359
A.6.24 locale 1359
A.6.25 logging 1360
A.6.26 mailbox 1360
A.6.27 mmap 1360
A.6.28 operator 1360
A.6.29 os 1360
A.6.30 os.path 1361
A.6.31 pdb 1361
A.6.32 pickle 1361
A.6.33 pipes 1362
A.6.34 platform 1362
A.6.35 random 1362
A.6.36 re 1362
A.6.37 shelve 1362
A.6.38 signal 1362
A.6.39 socket 1362
A.6.40 socketserver 1363
A.6.41 string 1363
A.6.42 struct 1363
A.6.43 subprocess 1363
A.6.44 sys 1363
A.6.45 threading 1364
A.6.46 time 1364
A.6.47 unittest 1364
A.6.48 UserDict, UserList, and UserString 1365
A.6.49 uuid 1365
A.6.50 whichdb 1365
A.6.51 xml.etree.ElementTree 1365
A.6.52 zipimport 1365
Appendix B Outside of the Standard Library 1367
B.1 Text 1367
B.2 Algorithms 1367
B.3 Dates and Times 1368
B.4 Mathematics 1368
B.5 Data Persistence and Exchange 1368
B.6 Cryptography 1369
B.7 Concurrency with Processes, Threads, and Coroutines 1369
B.8 The Internet 1369
B.9 Email 1370
B.10 Application Building Blocks 1370
B.11 Developer Tools 1371
Index of Python Modules 1373
Index 1375



Bookscreen
e-books shop

How This Book Is Organized
This book supplements the comprehensive reference guide (available at http://docs.python.org), providing fully functional example programs to demonstrate the features
described there. The modules are grouped into chapters to make it easy to find an individual
module for reference and browse by subject for more leisurely exploration. In the
unlikely event that you want to read it through from cover to cover, it is organized to
minimize “forward references” to modules not yet covered, although it was not possible to eliminate them entirely.
Loading...
DMCA.com Protection Status