|
Beginning Python |
5 |
|
|
About the Authors |
7 |
|
|
Credits |
9 |
|
|
Contents |
13 |
|
|
Acknowledgments |
31 |
|
|
Introduction |
33 |
|
|
What Is Python? |
33 |
|
|
The Benefits of Python |
33 |
|
|
Who This Book Is For |
34 |
|
|
What This Book Covers |
35 |
|
|
How This Book Is Structured |
35 |
|
|
What You Need to Use This Book |
37 |
|
|
Conventions |
38 |
|
|
On the Web Site |
39 |
|
|
Errata |
39 |
|
|
p2p.wrox.com |
40 |
|
|
Chapter 1: Programming Basics and Strings |
41 |
|
|
How Programming Is Different from Using a Computer |
41 |
|
|
The First Steps |
43 |
|
|
Beginning to Use Python — Strings |
45 |
|
|
Putting Two Strings Together |
48 |
|
|
Putting Strings Together in Different Ways |
49 |
|
|
Displaying Strings with Print |
50 |
|
|
Summary |
50 |
|
|
Exercises |
51 |
|
|
Chapter 2: Numbers and Operators |
53 |
|
|
Different Kinds of Numbers |
53 |
|
|
Program Files |
55 |
|
|
Using Numbers |
61 |
|
|
Summary |
64 |
|
|
Exercises |
65 |
|
|
Chapter 3: Variables — Names for Values |
67 |
|
|
Referring to Data — Using Names for Data |
67 |
|
|
Using More Built-in Types |
70 |
|
|
Other Common Sequence Properties |
78 |
|
|
Summary |
81 |
|
|
Exercises |
82 |
|
|
Chapter 4: Making Decisions |
83 |
|
|
Comparing Values — Are They the Same? |
83 |
|
|
Doing the Opposite — Not Equal |
85 |
|
|
Comparing Values — Which One Is More? |
85 |
|
|
Reversing True and False |
87 |
|
|
Looking for the Results of More Than One Comparison |
88 |
|
|
Repetition |
91 |
|
|
Handling Errors |
95 |
|
|
Summary |
97 |
|
|
Exercises |
98 |
|
|
Chapter 5: Functions |
99 |
|
|
Putting Your Program into Its Own File |
99 |
|
|
Functions: Grouping Code under a Name |
101 |
|
|
Layers of Functions |
114 |
|
|
Summary |
115 |
|
|
Exercises |
116 |
|
|
Chapter 6: Classes and Objects |
119 |
|
|
Thinking About Programming |
119 |
|
|
Defining a Class |
121 |
|
|
Summary |
132 |
|
|
Exercises |
133 |
|
|
Chapter 7: Organizing Programs |
135 |
|
|
Modules |
136 |
|
|
Packages |
141 |
|
|
Modules and Packages |
143 |
|
|
Basics of Testing Your Modules and Packages |
146 |
|
|
Summary |
146 |
|
|
Exercises |
147 |
|
|
Chapter 8: Files and Directories |
149 |
|
|
File Objects |
149 |
|
|
Paths and Directories |
153 |
|
|
Pickles |
163 |
|
|
Summary |
165 |
|
|
Exercises |
165 |
|
|
Chapter 9: Other Features of the Language |
167 |
|
|
Lambda and Filter: Short Anonymous Functions |
167 |
|
|
Reduce |
168 |
|
|
Map: Short-Circuiting Loops |
169 |
|
|
Decisions within Lists — List Comprehension |
170 |
|
|
Generating Lists for Loops |
171 |
|
|
Special String Substitution Using Dictionaries |
173 |
|
|
Featured Modules |
174 |
|
|
Summary |
181 |
|
|
Exercises |
182 |
|
|
Chapter 10: Building a Module |
183 |
|
|
Exploring Modules |
183 |
|
|
Creating Modules and Packages |
190 |
|
|
Working with Classes |
191 |
|
|
Finishing Your Modules |
194 |
|
|
Creating a Whole Module |
205 |
|
|
Installing Your Modules |
210 |
|
|
Summary |
214 |
|
|
Exercises |
214 |
|
|
Chapter 11: Text Processing |
215 |
|
|
Why Text Processing Is So Useful |
215 |
|
|
Navigating the File System with the os Module |
218 |
|
|
Working with Regular Expressions and the re Module |
224 |
|
|
Summary |
229 |
|
|
Exercises |
229 |
|
|
Chapter 12: Testing |
231 |
|
|
Assertions |
231 |
|
|
Test Cases and Test Suites |
233 |
|
|
Test Fixtures |
236 |
|
|
Putting It All Together with Extreme Programming |
239 |
|
|
Formal Testing in the Software Life Cycle |
247 |
|
|
Summary |
248 |
|
|
Chapter 13: Writing a GUI with Python |
249 |
|
|
GUI Programming Toolkits for Python |
249 |
|
|
PyGTK Introduction |
250 |
|
|
pyGTK Resources |
251 |
|
|
Creating GUI Widgets with pyGTK |
253 |
|
|
Using libGlade with Python |
265 |
|
|
A Glade Walkthrough |
265 |
|
|
Creating a Real Glade Application |
271 |
|
|
Advanced Widgets |
278 |
|
|
Further Enhancing PyRAP |
281 |
|
|
Summary |
288 |
|
|
Exercises |
288 |
|
|
Chapter 14: Accessing Databases |
289 |
|
|
Working with DBM Persistent Dictionaries |
290 |
|
|
Working with Relational Databases |
295 |
|
|
Using the Python Database APIs |
302 |
|
|
Summary |
313 |
|
|
Exercises |
314 |
|
|
Chapter 15: Using Python for XML |
315 |
|
|
What Is XML? |
315 |
|
|
What Is a Schema/DTD? |
318 |
|
|
Document Type Definitions |
318 |
|
|
Schemas |
320 |
|
|
XPath |
322 |
|
|
HTML as a Subset of XML |
322 |
|
|
XML Libraries Available for Python |
325 |
|
|
Validating XML Using Python |
325 |
|
|
What Is SAX? |
327 |
|
|
What Is DOM? |
328 |
|
|
Why Use SAX or DOM |
329 |
|
|
SAX and DOM Parsers Available for Python |
329 |
|
|
Intro to XSLT |
333 |
|
|
Using Python to Transform XML Using XSLT |
334 |
|
|
Putting It All Together: Working with RSS |
336 |
|
|
Summary |
343 |
|
|
Exercises |
343 |
|
|
Chapter 16: Network Programming |
345 |
|
|
Understanding Protocols |
347 |
|
|
Sending Internet E-mail |
351 |
|
|
Retrieving Internet E-mail |
363 |
|
|
Socket Programming |
371 |
|
|
Other Topics |
390 |
|
|
Summary |
394 |
|
|
Exercises |
394 |
|
|
Chapter 17: Extension Programming with C |
395 |
|
|
Extension Module Outline |
396 |
|
|
Building and Installing Extension Modules |
398 |
|
|
Passing Parameters from Python to C |
400 |
|
|
Returning Values from C to Python |
403 |
|
|
The LAME Project |
404 |
|
|
The LAME Extension Module |
408 |
|
|
Using Python Objects from C Code |
420 |
|
|
Summary |
423 |
|
|
Exercises |
423 |
|
|
Chapter 18: Writing Shareware and Commercial Programs |
425 |
|
|
A Case Study: Background |
425 |
|
|
How Much Python Should You Use? |
426 |
|
|
Pure Python Licensing |
427 |
|
|
Pricing Strategies |
429 |
|
|
Your Development Environment |
435 |
|
|
Finding Python Programmers |
436 |
|
|
Python Problems |
437 |
|
|
Portable Distribution |
440 |
|
|
Essential Libraries |
441 |
|
|
Summary |
443 |
|
|
Chapter 19: Numerical Programming |
445 |
|
|
Numbers in Python |
445 |
|
|
Mathematics |
452 |
|
|
Complex Numbers |
456 |
|
|
Arrays |
458 |
|
|
Summary |
464 |
|
|
Exercises |
465 |
|
|
Chapter 20: Python in the Enterprise |
467 |
|
|
Enterprise Applications |
468 |
|
|
Auditing, Sarbanes-Oxley, and What You Need to Know |
473 |
|
|
Working with Actual Enterprise Systems |
475 |
|
|
Summary |
498 |
|
|
Chapter 21: Web Applications and Web Services |
499 |
|
|
REST: The Architecture of the Web |
500 |
|
|
HTTP: Real-World REST |
503 |
|
|
CGI: Turning Scripts into Web Applications |
508 |
|
|
Building a Wiki |
519 |
|
|
Web Services |
533 |
|
|
REST Web Services |
534 |
|
|
XML-RPC |
548 |
|
|
SOAP |
560 |
|
|
Documenting Your Web Service API |
569 |
|
|
Choosing a Web Service Standard |
574 |
|
|
Web Service Etiquette |
575 |
|
|
A Sampling of Publicly Available Web Services |
576 |
|
|
Summary |
578 |
|
|
Exercises |
578 |
|
|
Chapter 22: Integrating Java with Python |
579 |
|
|
Scripting within Java Applications |
580 |
|
|
Comparing Python Implementations |
581 |
|
|
Installing Jython |
581 |
|
|
Running Jython |
582 |
|
|
Running Jython on Your Own |
586 |
|
|
Packaging Jython-Based Applications |
587 |
|
|
Integrating Java and Jython |
587 |
|
|
Testing from Jython |
605 |
|
|
Embedding the Jython Interpreter |
606 |
|
|
Compiling Python Code to Java |
608 |
|
|
Handling Differences between C Python and Jython |
609 |
|
|
Summary |
610 |
|
|
Exercises |
611 |
|
|
Appendix A: Answers to Exercises |
613 |
|
|
Appendix B: Online Resources |
645 |
|
|
For More Information |
647 |
|
|
Appendix C: What’s New in Python 2.4 |
649 |
|
|
Negative Numbers |
649 |
|
|
Automatic Promotion of Large Numbers |
649 |
|
|
A Different String Substitution |
650 |
|
|
Looking at Sequences in Reverse |
650 |
|
|
A More Pythonish Subprocess Creation |
650 |
|
|
Making Import a Bit Easier |
651 |
|
|
None Really Is None |
651 |
|
|
Glossary |
653 |
|
|
Index |
663 |
|