ebook img

Programming Python PDF

1598 Pages·2006·14.428 MB·English
by  LutzMark
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Programming Python

,Title.7160 Page i Friday, August 18, 2006 8:18 AM Programming Python ,Title.7160 Page ii Friday, August 18, 2006 8:18 AM Other resources from O’Reilly Related titles Learning Python Python Pocket Reference Python and XML Python Standard Library Python Cookbook™ Twisted Network Python in a Nutshell Programming Essentials oreilly.com oreilly.com is more than a complete catalog of O’Reilly books. You’llalsofindlinkstonews,events,articles,weblogs,sample chapters, and code examples. oreillynet.comistheessentialportalfordevelopersinterestedin openandemergingtechnologies,includingnewplatforms,pro- gramming languages, and operating systems. Conferences O’Reillybringsdiverseinnovatorstogethertonurturetheideas that spark revolutionary industries. We specialize in docu- menting the latest tools and systems, translating the innovator’s knowledge into useful skills for those in the trenches. Visit conferences.oreilly.com for our upcoming events. Safari Bookshelf (safari.oreilly.com) is the premier online refer- ence library for programmers and IT professionals. Conduct searchesacrossmorethan1,000books.Subscriberscanzeroin on answers to time-critical questions in a matter of seconds. Read the books on your Bookshelf from cover to cover or sim- ply flip to the page you need. Try it today for free. ,Title.7160 Page iii Friday, August 18, 2006 8:18 AM THIRD EDITION Programming Python Mark Lutz Beijing • Cambridge • Farnham • Köln • Paris • Sebastopol • Taipei • Tokyo ,Copyright.7029 Page iv Friday, August 18, 2006 8:17 AM Programming Python, Third Edition by Mark Lutz Copyright © 2006, 2001, 1996 O’Reilly Media, Inc. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (safari.oreilly.com). For more information, contact our corporate/institutional sales department: (800) 998-9938 [email protected]. Editor: Mary O’Brien Indexer: Johnna VanHoose Dinse Production Editor: Mary Brady Cover Designer: Edie Freedman Copyeditor: Audrey Doyle Interior Designer: David Futato Proofreaders: Lydia Onofrei, Colleen Gorman, Illustrators: RobertRomanoandJessamynRead and Mary Brady Printing History: October 1996: First Edition. March 2001: Second Edition. August 2006: Third Edition. Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of O’ReillyMedia,Inc.ProgrammingPython,theimageofanAfricanrockpython,andrelatedtradedress are trademarks of O’Reilly Media, Inc. Manyofthedesignationsusedbymanufacturersandsellerstodistinguishtheirproductsareclaimedas trademarks. Where those designations appear in this book, and O’Reilly Media, Inc. was aware of a trademark claim, the designations have been printed in caps or initial caps. Whileeveryprecautionhasbeentakeninthepreparationofthisbook,thepublisherandauthorassume no responsibility for errors or omissions, or for damages resulting from the use of the information contained herein. ISBN-10: 0-596-00925-9 ISBN-13: 978-0-596-00925-0 [M] Table of Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xiii Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xxi Part I. The Beginning 1. Introducing Python . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 “And Now for Something Completely Different” 3 Python Philosophy 101 3 The Life of Python 8 Signs of the Python Times 9 The Compulsory Features List 15 What’s Python Good For? 17 What’s Python Not Good For? 20 Truth in Advertising 22 2. A Sneak Preview. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 “Programming Python: The Short Story” 24 The Task 24 Step 1: Representing Records 25 Step 2: Storing Records Persistently 35 Step 3: Stepping Up to OOP 47 Step 4: Adding Console Interaction 57 Step 5: Adding a GUI 60 Step 6: Adding a Web Interface 70 The End of the Demo 86 v Part II. System Programming 3. System Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 “The os.path to Knowledge” 89 System Scripting Overview 90 Introducing the sys Module 100 Introducing the os Module 104 Script Execution Context 113 Current Working Directory 114 Command-Line Arguments 117 Shell Environment Variables 119 Standard Streams 123 4. File and Directory Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 “Erase Your Hard Drive in Five Easy Steps!” 142 File Tools 142 Directory Tools 159 5. Parallel System Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 “Telling the Monkeys What to Do” 175 Forking Processes 176 Threads 183 Program Exits 201 Interprocess Communication 208 Pipes 209 Signals 218 Other Ways to Start Programs 221 A Portable Program-Launch Framework 230 Other System Tools 235 6. System Examples: Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 236 “Splits and Joins and Alien Invasions” 236 Splitting and Joining Files 237 Generating Forward-Link Web Pages 247 A Regression Test Script 251 Packing and Unpacking Files 254 Automated Program Launchers 265 vi | Table of Contents 7. System Examples: Directories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294 “The Greps of Wrath” 294 Fixing DOS Line Ends 294 Fixing DOS Filenames 307 Searching Directory Trees 311 Visitor: Walking Trees Generically 317 Copying Directory Trees 339 Deleting Directory Trees 345 Comparing Directory Trees 349 Part III. GUI Programming 8. Graphical User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365 “Here’s Looking at You, Kid” 365 Python GUI Development Options 367 Tkinter Overview 371 Climbing the GUI Learning Curve 375 Tkinter Coding Basics 377 Tkinter Coding Alternatives 380 Adding Buttons and Callbacks 386 Adding User-Defined Callback Handlers 389 Adding Multiple Widgets 401 Customizing Widgets with Classes 406 Reusable GUI Components with Classes 408 The End of the Tutorial 414 Python/Tkinter for Tcl/Tk Converts 416 9. A Tkinter Tour, Part 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418 “Widgets and Gadgets and GUIs, Oh My!” 418 Configuring Widget Appearance 419 Top-Level Windows 422 Dialogs 427 Binding Events 443 Message and Entry 448 Checkbutton, Radiobutton, and Scale 456 Running GUI Code Three Ways 468 Images 478 Viewing and Processing Images with PIL 483 Table of Contents | vii 10. A Tkinter Tour, Part 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499 “On Today’s Menu: Spam, Spam, and Spam” 499 Menus 499 Listboxes and Scrollbars 511 Text 517 Canvas 529 Grids 543 Time Tools, Threads, and Animation 559 The End of the Tour 570 The PyDemos and PyGadgets Launchers 571 11. GUI Coding Techniques . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 583 “Building a Better Mouse Trap” 583 GuiMixin: Common Tool Mixin Classes 584 GuiMaker: Automating Menus and Toolbars 586 ShellGui: GUIs for Command-Line Tools 597 GuiStreams: Redirecting Streams to Widgets 605 Reloading Callback Handlers Dynamically 609 Wrapping Up Top-Level Window Interfaces 611 GUIs, Threads, and Queues 616 More Ways to Add GUIs to Non-GUI Code 624 12. Complete GUI Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 636 “Python, Open Source, and Camaros” 636 PyEdit: A Text Editor Program/Object 638 PyPhoto: An Image Viewer and Resizer 657 PyView: An Image and Notes Slideshow 668 PyDraw: Painting and Moving Graphics 676 PyClock: An Analog/Digital Clock Widget 685 PyToe: A Tic-Tac-Toe Game Widget 700 Where to Go from Here 704 Part IV. Internet Programming 13. Network Scripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 709 “Tune In, Log On, and Drop Out” 709 Plumbing the Internet 713 Socket Programming 720 viii | Table of Contents

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.