Synopses & Reviews
Already the industry standard for Python users, Programming Python from O'Reilly just got even better. This third edition has been updated to reflect current best practices and the abundance of changes introduced by the latest version of the language, Python 2.5.
Whether you're a novice or an advanced practitioner, you'll find this refreshed book more than lives up to its reputation. Programming Python, 3rd Edition, teaches you the right way to code. It explains Python language syntax and programming techniques in a clear and concise manner, with numerous examples that illustrate both correct usage and common idioms. By reading this comprehensive guide, you'll learn how to apply Python in real-world problem domains such as:
- GUI programming
- Internet scripting
- Parallel processing
- Database management
- Networked applications
Programming Python, Third Edition covers each of these target domains gradually, beginning with in-depth discussions of core concepts and then progressing toward complete programs. Large examples do appear, but only after you've learned enough to understand their techniques and code.
Along the way, you'll also learn how to use the Python language in realistically scaled programs--concepts such as Object-Oriented Programming (OOP) and code reuse are recurring side themes throughout this text. If you're interested in Python programming, then this O'Reilly classic needs to be within arm's reach. The wealth of practical advice, snippets of code, and patterns of program design can all be put into use on a daily basis--making your life easier and more productive.
Reviews of the second edition:
"...about as comprehensive as any book can be."
--Dr. Dobb's Journal
"If the language had manuals, they would undoubtedly be the texts from O'Reilly...'Learning Python' and 'Programming Python' are definitive treatments."
Learn how to develop your own applications to monitor or control instrumentation hardware. Whether you need to acquire data from a device or automate its functions, this practical book shows you how to use Python's rapid development capabilities to build interfaces that include everything from software to wiring. You get step-by-step instructions, clear examples, and hands-on tips for interfacing a PC to a variety of devices.
Use the book's hardware survey to identify the interface type for your particular device, and then follow detailed examples to develop an interface with Python and C. Organized by interface type, data processing activities, and user interface implementations, this book is for anyone who works with instrumentation, robotics, data acquisition, or process control.
- Understand how to define the scope of an application and determine the algorithms necessary, and why it's important
- Learn how to use industry-standard interfaces such as RS-232, RS-485, and GPIB
- Create low-level extension modules in C to interface Python with a variety of hardware and test instruments
- Explore the console, curses, TkInter, and wxPython for graphical and text-based user interfaces
- Use open source software tools and libraries to reduce costs and avoid implementing functionality from scratch
As a language optimized for developer productivity, Python solves the software development crunch. As the industry standard book for mastering this expedient language, this volume explains Python language syntax and programming techniques in a clear and concise manner, with numerous examples that illustrate both correct usage and common idioms.
About the Author
John M. Hughes is an embedded systems engineer with over 30 years of experience in electronics, embedded systems and software, aerospace systems, and scientific applications programming. He was responsible for the surface imaging software on the Phoenix Mars Lander, and has worked on digital engine control systems for commercial and military aircraft, automated test systems, radio telescope data acquisition, and realtime adaptive optics controls for astronomy. Hughes has been using Python for many years in a variety of applications, including the software for a multiwavelength laser interferometer system for verifying the alignment of telescope mirror segments on the James Webb Space Telescope. He is currently using Python for imaging systems simulation and analysis at the University of Arizona.
Table of Contents
Foreword; How Time Flies!; Coming Attractions; Concluding Remarks; Foreword to the Second Edition (2001); Foreword from the First Edition (1996); Preface; "And Now for Something Completely Different . . . Again"; About This Book; About This Edition; This Book's Motivation; This Book's Structure; This Edition's Design; Using the Book's Examples; Conventions Used in This Book; Safari® Enabled; Where to Look for Updates; Contacting O'Reilly; Using Code Examples; Acknowledgments; Part I: The Beginning; Chapter 1: Introducing Python; 1.1 "And Now for Something Completely Different"; 1.2 Python Philosophy 101; 1.3 The Life of Python; 1.4 Signs of the Python Times; 1.5 The Compulsory Features List; 1.6 What's Python Good For?; 1.7 What's Python Not Good For?; 1.8 Truth in Advertising; Chapter 2: A Sneak Preview; 2.1 "Programming Python: The Short Story"; 2.2 The Task; 2.3 Step 1: Representing Records; 2.4 Step 2: Storing Records Persistently; 2.5 Step 3: Stepping Up to OOP; 2.6 Step 4: Adding Console Interaction; 2.7 Step 5: Adding a GUI; 2.8 Step 6: Adding a Web Interface; 2.9 The End of the Demo; Part II: System Programming; Chapter 3: System Tools; 3.1 "The os.path to Knowledge"; 3.2 System Scripting Overview; 3.3 Introducing the sys Module; 3.4 Introducing the os Module; 3.5 Script Execution Context; 3.6 Current Working Directory; 3.7 Command-Line Arguments; 3.8 Shell Environment Variables; 3.9 Standard Streams; Chapter 4: File and Directory Tools; 4.1 "Erase Your Hard Drive in Five Easy Steps!"; 4.2 File Tools; 4.3 Directory Tools; Chapter 5: Parallel System Tools; 5.1 "Telling the Monkeys What to Do"; 5.2 Forking Processes; 5.3 Threads; 5.4 Program Exits; 5.5 Interprocess Communication; 5.6 Pipes; 5.7 Signals; 5.8 Other Ways to Start Programs; 5.9 A Portable Program-Launch Framework; 5.10 Other System Tools; Chapter 6: System Examples: Utilities; 6.1 "Splits and Joins and Alien Invasions"; 6.2 Splitting and Joining Files; 6.3 Generating Forward-Link Web Pages; 6.4 A Regression Test Script; 6.5 Packing and Unpacking Files; 6.6 Automated Program Launchers; Chapter 7: System Examples: Directories; 7.1 "The Greps of Wrath"; 7.2 Fixing DOS Line Ends; 7.3 Fixing DOS Filenames; 7.4 Searching Directory Trees; 7.5 Visitor: Walking Trees Generically; 7.6 Copying Directory Trees; 7.7 Deleting Directory Trees; 7.8 Comparing Directory Trees; Part III: GUI Programming; Chapter 8: Graphical User Interfaces; 8.1 "Here's Looking at You, Kid"; 8.2 Python GUI Development Options; 8.3 Tkinter Overview; 8.4 Climbing the GUI Learning Curve; 8.5 Tkinter Coding Basics; 8.6 Tkinter Coding Alternatives; 8.7 Adding Buttons and Callbacks; 8.8 Adding User-Defined Callback Handlers; 8.9 Adding Multiple Widgets; 8.10 Customizing Widgets with Classes; 8.11 Reusable GUI Components with Classes; 8.12 The End of the Tutorial; 8.13 Python/Tkinter for Tcl/Tk Converts; Chapter 9: A Tkinter Tour, Part 1; 9.1 "Widgets and Gadgets and GUIs, Oh My!"; 9.2 Configuring Widget Appearance; 9.3 Top-Level Windows; 9.4 Dialogs; 9.5 Binding Events; 9.6 Message and Entry; 9.7 Checkbutton, Radiobutton, and Scale; 9.8 Running GUI Code Three Ways; 9.9 Images; 9.10 Viewing and Processing Images with PIL; Chapter 10: A Tkinter Tour, Part 2; 10.1 "On Today's Menu: Spam, Spam, and Spam"; 10.2 Menus; 10.3 Listboxes and Scrollbars; 10.4 Text; 10.5 Canvas; 10.6 Grids; 10.7 Time Tools, Threads, and Animation; 10.8 The End of the Tour; 10.9 The PyDemos and PyGadgets Launchers; Chapter 11: GUI Coding Techniques; 11.1 "Building a Better Mouse Trap"; 11.2 GuiMixin: Common Tool Mixin Classes; 11.3 GuiMaker: Automating Menus and Toolbars; 11.4 ShellGui: GUIs for Command-Line Tools; 11.5 GuiStreams: Redirecting Streams to Widgets; 11.6 Reloading Callback Handlers Dynamically; 11.7 Wrapping Up Top-Level Window Interfaces; 11.8 GUIs, Threads, and Queues; 11.9 More Ways to Add GUIs to Non-GUI Code; Chapter 12: Complete GUI Programs; 12.1 "Python, Open Source, and Camaros"; 12.2 PyEdit: A Text Editor Program/Object; 12.3 PyPhoto: An Image Viewer and Resizer; 12.4 PyView: An Image and Notes Slideshow; 12.5 PyDraw: Painting and Moving Graphics; 12.6 PyClock: An Analog/Digital Clock Widget; 12.7 PyToe: A Tic-Tac-Toe Game Widget; 12.8 Where to Go from Here; Part IV: Internet Programming; Chapter 13: Network Scripting; 13.1 "Tune In, Log On, and Drop Out"; 13.2 Plumbing the Internet; 13.3 Socket Programming; 13.4 Handling Multiple Clients; 13.5 A Simple Python File Server; Chapter 14: Client-Side Scriptttttting; 14.1 "Socket to Me!"; 14.2 FTP: Transferring Files over the Net; 14.3 Processing Internet Email; 14.4 POP: Fetching Email; 14.5 SMTP: Sending Email; 14.6 email: Parsing and Composing Mails; 14.7 pymail: A Console-Based Email Client; 14.8 The mailtools Utility Package; 14.9 NNTP: Accessing Newsgroups; 14.10 HTTP: Accessing Web Sites; 14.11 Module urllib Revisited; 14.12 Other Client-Side Scripting Options; Chapter 15: The PyMailGUI Client; 15.1 "Use the Source, Luke"; 15.2 A PyMailGUI Demo; 15.3 PyMailGUI Implementation; Chapter 16: Server-Side Scripting; 16.1 "Oh What a Tangled Web We Weave"; 16.2 What's a Server-Side CGI Script?; 16.3 Running Server-Side Examples; 16.4 Climbing the CGI Learning Curve; 16.5 Saving State Information in CGI Scripts; 16.6 The Hello World Selector; 16.7 Refactoring Code for Maintainability; 16.8 More on HTML and URL Escapes; 16.9 Transferring Files to Clients and Servers; Chapter 17: The PyMailCGI Server; 17.1 "Things to Do When Visiting Chicago"; 17.2 The PyMailCGI Web Site; 17.3 The Root Page; 17.4 Sending Mail by SMTP; 17.5 Reading POP Email; 17.6 Processing Fetched Mail; 17.7 Utility Modules; 17.8 CGI Script Trade-Offs; Chapter 18: Advanced Internet Topics; 18.1 "Surfing on the Shoulders of Giants"; 18.2 Zope: A Web Application Framework; 18.3 HTMLgen: Web Pages from Objects; 18.4 Jython: Python for Java; 18.5 Grail: A Python-Based Web Browser; 18.6 XML Processing Tools; 18.7 Windows Web Scripting Extensions; 18.8 Python Server Pages; 18.9 Rolling Your Own Servers in Python; 18.10 And Other Cool Stuff; Part V: Tools and Techniques; Chapter 19: Databases and Persistence; 19.1 "Give Me an Order of Persistence, but Hold the Pickles"; 19.2 Persistence Options in Python; 19.3 DBM Files; 19.4 Pickled Objects; 19.5 Shelve Files; 19.6 The ZODB Object-Oriented Database; 19.7 SQL Database Interfaces; 19.8 PyForm: A Persistent Object Viewer; Chapter 20: Data Structures; 20.1 "Roses Are Red, Violets Are Blue; Lists Are Mutable, and So Is Set Foo"; 20.2 Implementing Stacks; 20.3 Implementing Sets; 20.4 Subclassing Built-In Types; 20.5 Binary Search Trees; 20.6 Graph Searching; 20.7 Reversing Sequences; 20.8 Permuting Sequences; 20.9 Sorting Sequences; 20.10 Data Structures Versus Python Built-Ins; 20.11 PyTree: A Generic Tree Object Viewer; Chapter 21: Text and Language; 21.1 "See Jack Hack. Hack, Jack, Hack"; 21.2 Strategies for Parsing Text in Python; 21.3 String Method Utilities; 21.4 Regular Expression Pattern Matching; 21.5 Advanced Language Tools; 21.6 Handcoded Parsers; 21.7 PyCalc: A Calculator Program/Object; Part VI: Integration; Chapter 22: Extending Python; 22.1 "I Am Lost at C"; 22.2 Integration Modes; 22.3 C Extensions Overview; 22.4 A Simple C Extension Module; 22.5 Extension Module Details; 22.6 The SWIG Integration Code Generator; 22.7 Wrapping C Environment Calls; 22.8 A C Extension Module String Stack; 22.9 A C Extension Type String Stack; 22.10 Wrapping C++ Classes with SWIG; 22.11 Other Extending Tools; Chapter 23: Embedding Python; 23.1 "Add Python. Mix Well. Repeat."; 23.2 C Embedding API Overview; 23.3 Basic Embedding Techniques; 23.4 Registering Callback Handler Objects; 23.5 Using Python Classes in C; 23.6 A High-Level Embedding API: ppembed; 23.7 Other Integration Topics; Part VIII: The End; Chapter 24: Conclusion: Python and the Development Cycle; 24.1 "That's the End of the Book, Now Here's the Meaning of Life"; 24.2 "Something's Wrong with the Way We Program Computers"; 24.3 The "Gilligan Factor"; 24.4 Doing the Right Thing; 24.5 Enter Python; 24.6 But What About That Bottleneck?; 24.7 On Sinking the Titanic; 24.8 So What's "Python: The Sequel"?; 24.9 In the Final Analysis . . .; 24.10 Postscript to the Second Edition (2000); 24.11 Postscript to the Third Edition (2006); Colophon;