diveintopython.pdf

(1248 KB) Pobierz
Dive Into Python
Table of Contents
Dive Into Python. ..............................................................................................................................................................1
Chapter 1. Installing Python. ..........................................................................................................................................2
1.1. Which Python is right for you?. .....................................................................................................................2
1.2. Python on Windows. ......................................................................................................................................2
1.3. Python on Mac OS X. ....................................................................................................................................3
1.4. Python on Mac OS 9. .....................................................................................................................................5
1.5. Python on RedHat Linux. ..............................................................................................................................5
1.6. Python on Debian GNU/Linux. .....................................................................................................................6
1.7. Python Installation from Source. ...................................................................................................................6
1.8. The Interactive Shell. .....................................................................................................................................7
1.9. Summary. .......................................................................................................................................................8
Chapter 2. Your First Python Program. ........................................................................................................................9
2.1. Diving in. .......................................................................................................................................................9
2.2. Declaring Functions. ......................................................................................................................................9
2.3. Documenting Functions. ..............................................................................................................................10
2.4. Everything Is an Object ...............................................................................................................................11
2.5. Indenting Code. ............................................................................................................................................13
2.6. Testing Modules.. .........................................................................................................................................14
Chapter 3. Native Datatypes. ........................................................................................................................................15
3.1. Introducing Dictionaries. .............................................................................................................................15
3.2. Introducing Lists. .........................................................................................................................................17
3.3. Introducing Tuples. ......................................................................................................................................22
3.4. Declaring variables. .....................................................................................................................................23
3.5. Formatting Strings .......................................................................................................................................25
3.6. Mapping Lists. .............................................................................................................................................26
3.7. Joining Lists and Splitting Strings. ..............................................................................................................28
3.8. Summary. .....................................................................................................................................................29
Chapter 4. The Power Of Introspection.. .....................................................................................................................31
4.1. Diving In. .....................................................................................................................................................31
4.2. Using Optional and Named Arguments. ......................................................................................................32
4.3. Using type, str, dir, and Other Built−In Functions. .....................................................................................33
4.4. Getting Object References With getattr. ......................................................................................................36
4.5. Filtering Lists. ..............................................................................................................................................38
4.6. The Peculiar Nature of and and or. ..............................................................................................................39
4.7. Using lambda Functions. .............................................................................................................................41
4.8. Putting It All Together. ................................................................................................................................43
4.9. Summary. .....................................................................................................................................................45
Chapter 5. Objects and Object−Orientation. ..............................................................................................................47
5.1. Diving In. .....................................................................................................................................................47
5.2. Importing Modules Using from module impor.. .........................................................................................49
5.3. Defining Classes. .........................................................................................................................................50
5.4. Instantiating Classes. ....................................................................................................................................53
5.5. Exploring UserDict: A Wrapper Class ........................................................................................................54
5.6. Special Class Methods. ................................................................................................................................56
5.7. Advanced Special Class Methods. ...............................................................................................................59
Dive Into Python
i
Table of Contents
5.8. Introducing Class Attributes. .......................................................................................................................60
5.9. Private Functions. ........................................................................................................................................62
5.10. Summary. ...................................................................................................................................................63
Chapter 6. Exceptions and File Handling. ...................................................................................................................64
6.1. Handling Exceptions. ...................................................................................................................................64
6.2. Working with File Objects. ..........................................................................................................................66
6.3. Iterating with for Loops. ..............................................................................................................................70
6.4. Using sys.modules .......................................................................................................................................72
6.5. Working with Directories ............................................................................................................................74
6.6. Putting It All Together. ................................................................................................................................77
6.7. Summary. .....................................................................................................................................................78
Chapter 7. Regular Expressions. ..................................................................................................................................81
7.1. Diving In. .....................................................................................................................................................81
7.2. Case Study: Street Addresses.. .....................................................................................................................81
7.3. Case Study: Roman Numerals. ....................................................................................................................83
7.4. Using the {n,m} Syntax. ..............................................................................................................................85
7.5. Verbose Regular Expressions. .....................................................................................................................88
7.6. Case study: Parsing Phone Numbers ...........................................................................................................89
7.7. Summary. .....................................................................................................................................................93
Chapter 8. HTML Processing. ......................................................................................................................................94
8.1. Diving in. .....................................................................................................................................................94
8.2. Introducing sgmllib.py. ................................................................................................................................98
8.3. Extracting data from HTML documents. ...................................................................................................100
8.4. Introducing BaseHTMLProcessor.py. .......................................................................................................102
8.5. locals and globals. ......................................................................................................................................104
8.6. Dictionary−based string formatting. ..........................................................................................................107
8.7. Quoting attribute values. ............................................................................................................................108
8.8. Introducing dialect.py. ...............................................................................................................................109
8.9. Putting it all together.. ................................................................................................................................111
8.10. Summary. .................................................................................................................................................113
Chapter 9. XML Processing. .......................................................................................................................................115
9.1. Diving in. ...................................................................................................................................................115
9.2. Packages. ....................................................................................................................................................121
9.3. Parsing XML. .............................................................................................................................................123
9.4. Unicode. .....................................................................................................................................................125
9.5. Searching for elements. ..............................................................................................................................129
9.6. Accessing element attributes.. ....................................................................................................................131
9.7. Segue. .........................................................................................................................................................132
Chapter 10. Scripts and Streams. ...............................................................................................................................133
10.1. Abstracting input sources. ........................................................................................................................133
10.2. Standard input, output, and error. ............................................................................................................136
10.3. Caching node lookups. .............................................................................................................................140
10.4. Finding direct children of a node. ............................................................................................................141
10.5. Creating separate handlers by node type. ................................................................................................141
Dive Into Python
ii
Table of Contents
10.6. Handling command−line arguments. .......................................................................................................143
10.7. Putting it all together. ...............................................................................................................................146
10.8. Summary. .................................................................................................................................................148
Chapter 11. HTTP Web Services. ...............................................................................................................................149
11.1. Diving in. .................................................................................................................................................149
11.2. How not to fetch data over HTTP. ...........................................................................................................151
11.3. Features of HTTP. ....................................................................................................................................152
11.4. Debugging HTTP web services. ..............................................................................................................153
11.5. Setting the User−Agent. ...........................................................................................................................155
11.6. Handling Last−Modified and ETag. ........................................................................................................156
11.7. Handling redirects. ...................................................................................................................................159
11.8. Handling compressed data. ......................................................................................................................163
11.9. Putting it all together. ...............................................................................................................................165
11.10. Summary. ...............................................................................................................................................167
Chapter 12. SOAP Web Services. ...............................................................................................................................168
12.1. Diving In. .................................................................................................................................................168
12.2. Installing the SOAP Libraries. .................................................................................................................169
12.3. First Steps with SOAP. ............................................................................................................................171
12.4. Debugging SOAP Web Services .............................................................................................................172
12.5. Introducing WSDL. ..................................................................................................................................173
12.6. Introspecting SOAP Web Services with WSDL. .....................................................................................174
12.7. Searching Google. ....................................................................................................................................176
12.8. Troubleshooting SOAP Web Services. ....................................................................................................179
12.9. Summary. .................................................................................................................................................182
Chapter 13. Unit Testing. ............................................................................................................................................183
13.1. Introduction to Roman numerals .............................................................................................................183
13.2. Diving in. .................................................................................................................................................184
13.3. Introducing romantest.py. ........................................................................................................................184
13.4. Testing for success. ..................................................................................................................................187
13.5. Testing for failure. ...................................................................................................................................189
13.6. Testing for sanity .....................................................................................................................................190
Chapter 14. Test−First Programming. .......................................................................................................................193
14.1. roman.py, stage 1. ....................................................................................................................................193
14.2. roman.py, stage 2. ....................................................................................................................................196
14.3. roman.py, stage 3. ....................................................................................................................................199
14.4. roman.py, stage 4. ....................................................................................................................................202
14.5. roman.py, stage 5. ....................................................................................................................................205
Chapter 15. Refactoring. .............................................................................................................................................208
15.1. Handling bugs. .........................................................................................................................................208
15.2. Handling changing requirements. ............................................................................................................210
15.3. Refactoring. ..............................................................................................................................................216
15.4. Postscript. .................................................................................................................................................219
15.5. Summary. .................................................................................................................................................221
Dive Into Python
iii
Table of Contents
Chapter 16. Functional Programming. ......................................................................................................................223
16.1. Diving in. .................................................................................................................................................223
16.2. Finding the path. ......................................................................................................................................224
16.3. Filtering lists revisited. .............................................................................................................................226
16.4. Mapping lists revisited. ............................................................................................................................228
16.5. Data−centric programming. .....................................................................................................................229
16.6. Dynamically importing modules. .............................................................................................................230
16.7. Putting it all together. ...............................................................................................................................231
16.8. Summary. .................................................................................................................................................234
Chapter 17. Dynamic functions. .................................................................................................................................235
17.1. Diving in. .................................................................................................................................................235
17.2. plural.py, stage 1. .....................................................................................................................................235
17.3. plural.py, stage 2. .....................................................................................................................................237
17.4. plural.py, stage 3. .....................................................................................................................................239
17.5. plural.py, stage 4. .....................................................................................................................................240
17.6. plural.py, stage 5. .....................................................................................................................................242
17.7. plural.py, stage 6. .....................................................................................................................................243
17.8. Summary. .................................................................................................................................................246
Chapter 18. Performance Tuning. ..............................................................................................................................247
18.1. Diving in. .................................................................................................................................................247
18.2. Using the timeit Module. .........................................................................................................................249
18.3. Optimizing Regular Expressions. ............................................................................................................250
18.4. Optimizing Dictionary Lookups. .............................................................................................................253
18.5. Optimizing List Operations.. ....................................................................................................................256
18.6. Optimizing String Manipulation. .............................................................................................................258
18.7. Summary. .................................................................................................................................................260
Appendix A. Further reading. ....................................................................................................................................261
Appendix B. A 5−minute review. ................................................................................................................................268
Appendix C. Tips and tricks. ......................................................................................................................................282
Appendix D. List of examples. ....................................................................................................................................289
Appendix E. Revision history.. ....................................................................................................................................302
Appendix F. About the book. ......................................................................................................................................314
Appendix G. GNU Free Documentation License. .....................................................................................................315
G.0. Preamble. ...................................................................................................................................................315
G.1. Applicability and definitions. ....................................................................................................................315
G.2. Verbatim copying. .....................................................................................................................................316
G.3. Copying in quantity. ..................................................................................................................................316
G.4. Modifications. ...........................................................................................................................................317
G.5. Combining documents. .............................................................................................................................318
G.6. Collections of documents. .........................................................................................................................318
G.7. Aggregation with independent works. ......................................................................................................318
Dive Into Python
iv
Table of Contents
G.8. Translation. ...............................................................................................................................................318
G.9. Termination. ..............................................................................................................................................319
G.10. Future revisions of this license. ...............................................................................................................319
G.11. How to use this License for your documents. .........................................................................................319
Appendix H. Python license. .......................................................................................................................................320
H.A. History of the software. ............................................................................................................................320
H.B. Terms and conditions for accessing or otherwise using Python ..............................................................320
Dive Into Python
v
Zgłoś jeśli naruszono regulamin