Programming In Python 3 - SMU

Transcription

Programming in Python 3A Complete Introduction to the Python LanguageSecond EditionMark SummerfieldUpper Saddle River, NJ · Boston · Indianapolis · San FranciscoNew York · Toronto · Montreal · London · Munich · Paris · MadridCapetown · Sydney · Tokyo · Singapore · Mexico City

Many of the designations used by manufacturers and sellers to distinguish their products are claimedas trademarks. Where those designations appear in this book, and the publisher was aware of atrademark claim, the designations have been printed with initial capital letters or in all capitals.The author and publisher have taken care in the preparation of this book, but make no expressed orimplied warranty of any kind and assume no responsibility for errors or omissions. No liability isassumed for incidental or consequential damages in connection with or arising out of the use of theinformation or programs contained herein.The publisher offers excellent discounts on this book when ordered in quantity for bulk purchasesor special sales, which may include electronic versions and/or custom covers and content particularto your business, training goals, marketing focus, and branding interests. For more information,please contact:U.S. Corporate and Government Sales(800) 382-3419corpsales@pearsontechgroup.comFor sales outside the United States, please contact:International Salesinternational@pearsoned.comVisit us on the Web: informit.com/awLibrary of Congress Cataloging-in-Publication DataSummerfield, Mark.Programming in Python 3 : a complete introduction to the Python language / MarkSummerfield.—2nd ed.p. cm.Includes bibliographical references and index.ISBN 978-0-321-68056-3 (pbk. : alk. paper)1. Python (Computer program language) 2. Object-oriented programming (Computer science)I. Title.QA76.73.P98S86 2010005.13’3—dc222009035430Copyright 2010 Pearson Education, Inc.All rights reserved. Printed in the United States of America. This publication is protected bycopyright, and permission must be obtained from the publisher prior to any prohibited reproduction,storage in a retrieval system, or transmission in any form or by any means, electronic, mechanical,photocopying, recording, or likewise. For information regarding permissions, write to:Pearson Education, Inc.Rights and Contracts Department501 Boylston Street, Suite 900Boston, MA 02116Fax: (617) 671-3447ISBN-13: 978-0-321-68056-3ISBN-10:0-321-68056-1Text printed in the United States on recycled paper at RR Donnelley in Crawfordsville, Indiana.First printing, November 2009

Contents at a GlanceList of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xvIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1Chapter 1. Rapid Introduction to Procedural Programming . . .9Chapter 2. Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51Chapter 3. Collection Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107Chapter 4. Control Structures and Functions . . . . . . . . . . . . . . . . . . . 159Chapter 5. Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195Chapter 6. Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . 233Chapter 7. File Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 287Chapter 8. Advanced Programming Techniques . . . . . . . . . . . . . . . . 339Chapter 9. Debugging, Testing, and Profiling . . . . . . . . . . . . . . . . . . . 413Chapter 10. Processes and Threading . . . . . . . . . . . . . . . . . . . . . . . . . . . 439Chapter 11. Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 457Chapter 12. Database Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 475Chapter 13. Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 489Chapter 14. Introduction to Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513Chapter 15. Introduction to GUI Programming . . . . . . . . . . . . . . . . . 569Epilogue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 595Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 597Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 599www.qtrac.eu/py3book.html

ContentsList of Tables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .xvIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1Chapter 1. Rapid Introduction to Procedural Programming . . .Creating and Running Python Programs . . . . . . . . . . . . . . . . . . . . . . . .Python’s “Beautiful Heart” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Piece #1: Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Piece #2: Object References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Piece #3: Collection Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Piece #4: Logical Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Piece #5: Control Flow Statements . . . . . . . . . . . . . . . . . . . . . . . . . .Piece #6: Arithmetic Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Piece #7: Input/Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Piece #8: Creating and Calling Functions . . . . . . . . . . . . . . . . . . . .Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .bigdigits.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .generate grid.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .991414161821263033363939424447Chapter 2. Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Identifiers and Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Integral Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Booleans . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Floating-Point Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Floating-Point Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Decimal Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Comparing Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Slicing and Striding Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .String Operators and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .51515454585859626365686971ix

String Formatting with the str.format() Method . . . . . . . . . . . . . .Character Encodings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .quadratic.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .csv2html.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7891949497102104Chapter 3. Collection Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Sequence Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Named Tuples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Lists . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Set Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Frozen Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Mapping Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Default Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Ordered Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Iterating and Copying Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Iterators and Iterable Operations and Functions . . . . . . . . . . . . .Copying Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .generate usernames.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .statistics.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49152156158Chapter 4. Control Structures and Functions . . . . . . . . . . . . . . . . . . .Control Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Conditional Branching . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Looping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Catching and Raising Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . .Custom Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Custom Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Names and Docstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Argument and Parameter Unpacking . . . . . . . . . . . . . . . . . . . . . . .159159159161163163168171176177x

Accessing Variables in the Global Scope . . . . . . . . . . . . . . . . . . . . .Lambda Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Example: make html skeleton.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .180182183185191192Chapter 5. Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Modules and Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Custom Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Overview of Python’s Standard Library . . . . . . . . . . . . . . . . . . . . . . . . . .String Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Command-Line Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Mathematics and Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Times and Dates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Algorithms and Collection Data Types . . . . . . . . . . . . . . . . . . . . . . .File Formats, Encodings, and Data Persistence . . . . . . . . . . . . . . .File, Directory, and Process Handling . . . . . . . . . . . . . . . . . . . . . . . .Networking and Internet Programming . . . . . . . . . . . . . . . . . . . . .XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Other Modules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31Chapter 6. Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . .The Object-Oriented Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Object-Oriented Concepts and Terminology . . . . . . . . . . . . . . . . . .Custom Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Attributes and Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Inheritance and Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using Properties to Control Attribute Access . . . . . . . . . . . . . . . .Creating Complete Fully Integrated Data Types . . . . . . . . . . . . .Custom Collection Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating Classes That Aggregate Collections . . . . . . . . . . . . . . . .Creating Collection Classes Using Aggregation . . . . . . . . . . . . . .Creating Collection Classes Using Inheritance . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .233234235238238243246248261261269276283285xi

Chapter 7. File Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Writing and Reading Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Pickles with Optional Compression . . . . . . . . . . . . . . . . . . . . . . . . . .Raw Binary Data with Optional Compression . . . . . . . . . . . . . . .Writing and Parsing Text Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Writing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Parsing Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Parsing Text Using Regular Expressions . . . . . . . . . . . . . . . . . . . .Writing and Parsing XML Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Element Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .DOM (Document Object Model) . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Manually Writing XML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Parsing XML with SAX (Simple API for XML) . . . . . . . . . . . . . . .Random Access Binary Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Generic BinaryRecordFile Class . . . . . . . . . . . . . . . . . . . . . . . . . .Example: The BikeStock Module’s Classes . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36337Chapter 8. Advanced Programming Techniques . . . . . . . . . . . . . . . .Further Procedural Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Branching Using Dictionaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Generator Expressions and Functions . . . . . . . . . . . . . . . . . . . . . . .Dynamic Code Execution and Dynamic Imports . . . . . . . . . . . . . .Local and Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Function and Method Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . .Function Annotations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Further Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . .Controlling Attribute Access . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Functors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Context Managers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Class Decorators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Abstract Base Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Multiple Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Metaclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Functional-Style Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Partial Function Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90395398xii

Coroutines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Example: Valid.py . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .399407410411Chapter 9. Debugging, Testing, and Profiling . . . . . . . . . . . . . . . . . . .Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Dealing with Syntax Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Dealing with Runtime Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Scientific Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Unit Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .413414414415420425432437Chapter 10. Processes and Threading . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the Multiprocessing Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the Threading Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Example: A Threaded Find Word Program . . . . . . . . . . . . . . . . . . .Example: A Threaded Find Duplicate Files Program . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .439440444446449454455Chapter 11. Networking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating a TCP Client . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating a TCP Server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .457458464471471Chapter 12. Database Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . .DBM Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .SQL Databases . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .475476480487488Chapter 13. Regular Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Python’s Regular Expression Language . . . . . . . . . . . . . . . . . . . . . . . . . .Characters and Character Classes . . . . . . . . . . . . . . . . . . . . . . . . . .Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Grouping and Capturing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Assertions and Flags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Regular Expression Module . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .489490490491494496499xiii

Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .509510Chapter 14. Introduction to Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . .BNF Syntax and Parsing Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . .Writing Handcrafted Parsers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Simple Key–Value Data Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . .Playlist Data Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Parsing the Blocks Domain-Specific Language . . . . . . . . . . . . . . .Pythonic Parsing with PyParsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .A Quick Introduction to PyParsing . . . . . . . . . . . . . . . . . . . . . . . . . .Simple Key–Value Data Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . .Playlist Data Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Parsing the Blocks Domain-Specific Language . . . . . . . . . . . . . . .Parsing First-Order Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Lex/Yacc-Style Parsing with PLY . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Simple Key–Value Data Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . .Playlist Data Parsing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Parsing the Blocks Domain-Specific Language . . . . . . . . . . . . . . .Parsing First-Order Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62566568Chapter 15. Introduction to GUI Programming . . . . . . . . . . . . . . . . .Dialog-Style Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Main-Window-Style Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating a Main Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating a Custom Dialog . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .569572578578590593593Epilogue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .595Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .597Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .599xiv

List of Tables2.1.2.2.Python’s Keywords . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52Numeric Operators and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 552.3.Integer Conversion Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2.4.Integer Bitwise Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57The Math Module’s Functions and Constants #1 . . . . . . . . . . . . . . 602.5.2.6.2.7.55The Math Module’s Functions and Constants #2 . . . . . . . . . . . . . . 61Python’s String Escapes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 662.8.String Methods #1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .732.9.String Methods #2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .742.10.String Methods #3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7.5.8.1.8.2.8.3.8.4.12.1.List Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115Set Methods and Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123Dictionary Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129Common Iterable Operators and Functions . . . . . . . . . . . . . . . . . . . 140Comparison Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242Fundamental Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 250Numeric and Bitwise Special Methods . . . . . . . . . . . . . . . . . . . . . . . 253Collection Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265Bytes and Bytearray Methods #1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299Bytes and Bytearray Methods #2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300Bytes and Bytearray Methods #3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 301File Object Attributes and Methods #1 . . . . . . . . . . . . . . . . . . . . . . . 325File Object Attributes and Methods #2 . . . . . . . . . . . . . . . . . . . . . . . 326Dynamic Programming and Introspection Functions . . . . . . . . . . 349Attribute Access Special Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . 365The Numbers Module’s Abstract Base Classes . . . . . . . . . . . . . . . . 381The Collections Module’s Main Abstract Base Classes . . . . . . . . . 383DB-API 2.0 Connection Object Methods . . . . . . . . . . . . . . . . . . . . . . 48112.2.DB-API 2.0 Cursor Object Attributes and Methods . . . . . . . . . . . 48213.1.Character Class Shorthands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492xv

13.2.13.3.13.4.13.5.13.6.13.7.Regular Expression Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 493Regular Expression Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 497The Regular Expression Module’s Functions . . . . . . . . . . . . . . . . . 502The Regular Expression Module’s Flags . . . . . . . . . . . . . . . . . . . . . . 502Regular Expression Object Methods . . . . . . . . . . . . . . . . . . . . . . . . . 503Match Object Attributes and Methods . . . . . . . . . . . . . . . . . . . . . . . . 507xvi

IntroductionPython is probably the easiest-to-learn and nicest-to-use programming language in widespread use. Python code is clear to read and write, and it is concise without being cryptic. Python is a very expressive language, which meansthat we can usually write far fewer lines of Python code than would be requiredfor an equivalent application written in, say, C or Java.Python is a cross-platform language: In general, the same Python program canbe run on Windows and Unix-like systems such as Linux, BSD, and Mac OS X,simply by copying the file or files that make up the program to the targetmachine, with no “building” or compiling necessary. It is possible to createPython programs that use platform-specific functionality, but this is rare

Programming in Python 3 A Complete Introduction to the Python Language Second Edition . library—this allows us to do such things as download a file from the Internet, unpack a compressed archive file,or create a web server,all with just one or a few lines of code. An