Search and Find
Service
Title page
2
Copyright Page
3
Contents at a Glance
5
Table of Contents
6
Foreword
14
About the Author
16
About the Technical Reviewer
17
Acknowledgments
18
Installing Python
19
Which Python Is Right for You?
19
Installing on Microsoft Windows
20
Installing on Mac OS X
29
Installing on Ubuntu Linux
41
Installing on Other Platforms
48
Using the Python Shell
48
Python Editors and IDEs
50
CHAPTER 1 Your First Python Program
52
Declaring Functions
53
Optional and Named Arguments
54
Writing Readable Code
56
Documentation Strings
56
The import Search Path
57
Everything Is an Object
58
What’s an Object?
59
Indenting Code
59
Exceptions
60
Catching Import Errors
62
Unbound Variables
63
Running Scripts
63
Further Reading Online
64
CHAPTER 2 Native Datatypes
65
Booleans
65
Numbers
66
Coercing Integers to Floats and Vice Versa
67
Common Numerical Operations
68
Fractions
69
Trigonometry
70
Numbers in a Boolean Context
70
Lists
71
Creating a List
71
Slicing a List
72
Adding Items to a List
73
Searching For Values in a List
75
Removing Items from a List
76
Removing Items from a List: Bonus Round
76
Lists in a Boolean Context
77
Tuples
78
Tuples in a Boolean Context
80
Assigning Multiple Values at Once
80
Sets
81
Modifying a Set
83
Removing Items from a Set
84
Common Set Operations
85
Sets in a Boolean Context
87
Dictionaries
88
Creating a Dictionary
88
Modifying a Dictionary
88
Mixed-Value Dictionaries
89
Dictionaries in a Boolean Context
90
None
91
None in a Boolean Context
91
Further Reading Online
92
CHAPTER 3 Comprehensions
93
Working With Files and Directories
93
The Current Working Directory
93
Working with Filenames and Directory Names
94
Listing Directories
96
Getting File Metadata
97
Constructing Absolute Pathnames
97
List Comprehensions
98
Dictionary Comprehensions
100
Fun with Dictionary Comprehensions
101
Set Comprehensions
101
Further Reading Online
102
CHAPTER 4 Strings
103
Unicode
104
Diving In
106
Formatting Strings
106
Compound Field Names
107
Format Specifiers
109
Other Common String Methods
110
Slicing a String
111
Strings versus Bytes
112
Character Encoding of Python Source Code
115
Further Reading Online
116
CHAPTER 5 Regular Expressions
118
Case Study: Street Addresses
118
Case Study: Roman Numerals
120
Checking for Thousands
121
Checking for Hundreds
122
Using the {n,m} Syntax
124
Checking for Tens and Ones
125
Verbose Regular Expressions
127
Case Study: Parsing Phone Numbers
129
Further Reading Online
134
CHAPTER 6 Closures and Generators
135
I Know, Let’s Use Regular Expressions!
136
A List of Functions
138
A List of Patterns
140
A File of Patterns
143
Generators
144
A Fibonacci Generator
146
A Plural Rule Generator
147
Further Reading Online
148
CHAPTER 7 Classes and Iterators
149
Defining Classes
150
The __init__() Method
150
Instantiating Classes
151
Instance Variables
152
A Fibonacci Iterator
153
A Plural Rule Iterator
155
Further Reading Online
160
CHAPTER 8 Advanced Iterators
161
Finding All Occurrences of a Pattern
163
Finding the Unique Items in a Sequence
163
Making Assertions
165
Generator Expressions
165
Calculating Permutations … the Lazy Way
167
Other Fun Stuff in the itertools Module
168
A New Kind of String Manipulation
172
Evaluating Arbitrary Strings as Python Expressions
174
Putting It All Together
178
Further Reading Online
178
CHAPTER 9 Unit Testing
179
A Single Question
180
Halt and Catch Fire
186
More Halting, More Fire
189
And One More Thing …
192
A Pleasing Symmetry
194
More Bad Input
198
CHAPTER 10 Refactoring
202
Handling Changing Requirements
205
Refactoring
209
Further Reading Online
213
CHAPTER 11 Files
214
Reading from Text Files
214
Character Encoding Rears Its Ugly Head
215
Stream Objects
216
Reading Data from a Text File
217
Closing Files
219
Closing Files Automatically
220
Reading Data One Line at a Time
220
Writing to Text Files
222
Character Encoding Again
223
Binary Files
224
Streams Objects from Nonfile Sources
225
Handling Compressed Files
226
Standard Input, Output, and Error
227
Redirecting Standard Output
228
Further Reading Online
231
CHAPTER 12 XML
232
A 5-Minute Crash Course in XML
233
The Structure of an Atom Feed
236
Parsing XML
238
Elements Are Lists
239
Attributes Are Dictionaries
240
Searching for Nodes Within an XML Document
241
Going Further with lxml
244
Generating XML
246
Parsing Broken XML
249
Further Reading Online
251
CHAPTER 13 Serializing Python Objects
252
A Quick Note About the Examples in this Chapter
252
Saving Data to a Pickle File
253
Loading Data from a Pickle File
255
Pickling Without a File
256
Bytes and Strings Rear Their Ugly Heads Again
257
Debugging Pickle Files
257
Serializing Python Objects to be Read by Other Languages
260
Saving Data to a JSON File
260
Mapping Python Datatypes to JSON
262
Serializing Datatypes Unsupported by JSON
262
Loading Data from a JSON File
267
Further Reading Online
269
CHAPTER 14 HTTP Web Services
271
Features of HTTP
272
Caching
272
Last-Modified Checking
273
ETags
274
Compression
275
Redirects
275
How Not to Fetch Data Over HTTP
276
What’s On the Wire?
277
Introducing httplib2
280
Caching with httplib2
283
Handling Last-Modified and ETag Headers with httplib2
286
Handling Compression with httplib2
288
Handling Redirects with httplib2
289
Beyond HTTP GET
292
Beyond HTTP POST
296
Further Reading Online
298
CHAPTER 15 Case Study: Porting chardet to Python 3
299
What Is Character Encoding Auto-Detection?
299
Why Auto-Detection Is Difficult
299
Auto-Encoding Algorithms
300
Introducing the chardet Module
300
UTF-n with a BOM
300
Escaped Encodings
300
Multibyte Encodings
301
Single-Byte Encodings
301
windows-1252
302
Running 2to3
302
A Short Digression Into Multi-File Modules
305
Fixing What 2to3 Can’t
307
False Is Invalid Syntax
307
No Module Named Constants
308
Name 'file' Is Not Defined
309
Can’t Use a String Pattern on a Bytes-Like Object
310
Can’t Convert 'bytes' Object to str Implicitly
312
Unsupported Operand type(s) for +: 'int' and 'bytes'
315
ord() Expected String of Length 1, but int Found
316
Unorderable Types: int() >= str()
319
Global Name 'reduce' Is not Defined
321
Lessons Learned
323
CHAPTER 16 Packaging Python Libraries
324
Things Distutils Can’t Do for You
325
Directory Structure
326
Writing Your Setup Script
327
Classifying Your Package
329
Examples of Good Package Classifiers
329
Checking Your Setup Script for Errors
332
Creating a Source Distribution
332
Creating a Graphical Installer
334
Building Installable Packages for Other Operating Systems
335
Adding Your Software to the Python Package Index
336
The Many Possible Futures of Python Packaging
337
Further Reading Online
338
APPENDIX A Porting Code to Python 3 with 2to3
339
print Statement
339
Unicode String Literals
340
unicode() Global Function
340
long Datatype
341
<> Comparison
341
has_key() Dictionary Method
342
Dictionary Methods that Return Lists
343
Renamed or Reorganized Modules
343
http
344
urllib
344
dbm
345
xmlrpc
346
Other Modules
346
Relative Imports Within a Package
348
next() Iterator Method
349
filter() Global Function
350
map() Global Function
350
reduce() Global Function
351
apply() Global Function
352
intern() Global Function
352
exec Statement
353
execfile Statement
353
repr Literals (Backticks)
354
try...except Statement
354
raise Statement
356
throw Method on Generators
356
xrange() Global Function
357
raw_input() and input() Global Functions
358
func_* Function Attributes
358
xreadlines() I/O Method
359
lambda Functions that Take a Tuple Instead of Multiple Parameters
360
Special Method Attributes
361
__nonzero__ Special Method
361
Octal Literals
362
sys.maxint
362
callable() Global Function
363
zip() Global Function
363
StandardError Exception
363
types Module Constants
364
isinstance() Global Function
365
basestring Datatype
365
itertools Module
366
sys.exc_type, sys.exc_value, sys.exc_traceback
366
List Comprehensions Over Tuples
367
os.getcwdu() Function
367
Metaclasses
367
Matters of Style
368
set() Literals (Explicit)
368
buffer() Global Function (Explicit)
368
Whitespace Around Commas (Explicit)
369
Common Idioms (Explicit)
369
APPENDIX B Special Method Names
371
Basics
371
Classes that Act Like Iterators
372
Computed Attributes
372
Classes that Act Like Functions
375
Classes that Act Like Sequences
376
Classes that Act Like Dictionaries
378
Classes that Act Like Numbers
379
Classes that Can Be Compared
383
Classes that Can Be Serialized
384
Classes that Can Be Used in a “with” Block
384
Really Esoteric Stuff
386
Further Reading Online
387
APPENDIX C Where to Go From Here
388
Index
390
All prices incl. VAT