Dive Into Python 3

Book description

Mark Pilgrim's Dive Into Python 3 is a hands-on guide to Python 3 and its differences from Python 2. As in the original book, Dive Into Python, each chapter starts with a real, complete code sample, proceeds to pick it apart and explain the pieces, and then puts it all back together in a summary at the end.

This book includes:

  • Example programs completely rewritten to illustrate powerful new concepts now available in Python 3: sets, iterators, generators, closures, comprehensions, and much more
  • A detailed case study of porting a major library from Python 2 to Python 3
  • A comprehensive appendix of all the syntactic and semantic changes in Python 3

This is the perfect resource for you if you need to port applications to Python 3, or if you like to jump into languages fast and get going right away.

Table of contents

  1. Title page
  2. Copyright Page
  3. Contents at a Glance
  4. Table of Contents (1/2)
  5. Table of Contents (2/2)
  6. Foreword
  7. About the Author
  8. About the Technical Reviewer
  9. Acknowledgments
  10. Installing Python
    1. Which Python Is Right for You?
    2. Installing on Microsoft Windows (1/2)
    3. Installing on Microsoft Windows (2/2)
    4. Installing on Mac OS X (1/3)
    5. Installing on Mac OS X (2/3)
    6. Installing on Mac OS X (3/3)
    7. Installing on Ubuntu Linux (1/2)
    8. Installing on Ubuntu Linux (2/2)
    9. Installing on Other Platforms
    10. Using the Python Shell
    11. Python Editors and IDEs
    12. CHAPTER 1 Your First Python Program
      1. Declaring Functions
      2. Optional and Named Arguments
      3. Writing Readable Code
      4. Documentation Strings
      5. The import Search Path
      6. Everything Is an Object
      7. What’s an Object?
      8. Indenting Code
      9. Exceptions
      10. Catching Import Errors
      11. Unbound Variables
      12. Running Scripts
      13. Further Reading Online
    13. CHAPTER 2 Native Datatypes
      1. Booleans
      2. Numbers
      3. Coercing Integers to Floats and Vice Versa
        1. Common Numerical Operations
      4. Fractions
      5. Trigonometry
      6. Numbers in a Boolean Context
      7. Lists (1/2)
      8. Lists (2/2)
        1. Creating a List
        2. Slicing a List
        3. Adding Items to a List
        4. Searching For Values in a List
        5. Removing Items from a List
        6. Removing Items from a List: Bonus Round
        7. Lists in a Boolean Context
      9. Tuples
        1. Tuples in a Boolean Context
        2. Assigning Multiple Values at Once
      10. Sets (1/2)
      11. Sets (2/2)
        1. Modifying a Set
        2. Removing Items from a Set
        3. Common Set Operations
        4. Sets in a Boolean Context
      12. Dictionaries
        1. Creating a Dictionary
        2. Modifying a Dictionary
        3. Mixed-Value Dictionaries
        4. Dictionaries in a Boolean Context
      13. None
        1. None in a Boolean Context
      14. Further Reading Online
    14. CHAPTER 3 Comprehensions
      1. Working With Files and Directories
        1. The Current Working Directory
        2. Working with Filenames and Directory Names
        3. Listing Directories
        4. Getting File Metadata
        5. Constructing Absolute Pathnames
      2. List Comprehensions
      3. Dictionary Comprehensions
        1. Fun with Dictionary Comprehensions
      4. Set Comprehensions
      5. Further Reading Online
    15. CHAPTER 4 Strings
      1. Unicode
      2. Diving In
      3. Formatting Strings
      4. Compound Field Names
      5. Format Specifiers
      6. Other Common String Methods
      7. Slicing a String
      8. Strings versus Bytes
      9. Character Encoding of Python Source Code
      10. Further Reading Online
    16. CHAPTER 5 Regular Expressions
      1. Case Study: Street Addresses
      2. Case Study: Roman Numerals
      3. Checking for Thousands
      4. Checking for Hundreds
      5. Using the {n,m} Syntax
      6. Checking for Tens and Ones
      7. Verbose Regular Expressions
      8. Case Study: Parsing Phone Numbers
      9. Further Reading Online
    17. CHAPTER 6 Closures and Generators
      1. I Know, Let’s Use Regular Expressions!
      2. A List of Functions
      3. A List of Patterns
      4. A File of Patterns
      5. Generators
      6. A Fibonacci Generator
      7. A Plural Rule Generator
      8. Further Reading Online
    18. CHAPTER 7 Classes and Iterators
      1. Defining Classes
      2. The __init__() Method
      3. Instantiating Classes
      4. Instance Variables
      5. A Fibonacci Iterator
      6. A Plural Rule Iterator
      7. Further Reading Online
    19. CHAPTER 8 Advanced Iterators
      1. Finding All Occurrences of a Pattern
      2. Finding the Unique Items in a Sequence
      3. Making Assertions
      4. Generator Expressions
      5. Calculating Permutations … the Lazy Way
      6. Other Fun Stuff in the itertools Module
      7. A New Kind of String Manipulation
      8. Evaluating Arbitrary Strings as Python Expressions
      9. Putting It All Together
      10. Further Reading Online
    20. CHAPTER 9 Unit Testing
      1. A Single Question (1/2)
      2. A Single Question (2/2)
      3. Halt and Catch Fire
      4. More Halting, More Fire
      5. And One More Thing …
      6. A Pleasing Symmetry
      7. More Bad Input
    21. CHAPTER 10 Refactoring
      1. Handling Changing Requirements
      2. Refactoring
      3. Further Reading Online
    22. CHAPTER 11 Files
      1. Reading from Text Files
        1. Character Encoding Rears Its Ugly Head
        2. Stream Objects
        3. Reading Data from a Text File
        4. Closing Files
        5. Closing Files Automatically
        6. Reading Data One Line at a Time
      2. Writing to Text Files
        1. Character Encoding Again
      3. Binary Files
      4. Streams Objects from Nonfile Sources
        1. Handling Compressed Files
      5. Standard Input, Output, and Error
        1. Redirecting Standard Output
      6. Further Reading Online
    23. CHAPTER 12 XML
      1. A 5-Minute Crash Course in XML
      2. The Structure of an Atom Feed
      3. Parsing XML
      4. Elements Are Lists
      5. Attributes Are Dictionaries
      6. Searching for Nodes Within an XML Document
      7. Going Further with lxml
      8. Generating XML
      9. Parsing Broken XML
      10. Further Reading Online
    24. CHAPTER 13 Serializing Python Objects
      1. A Quick Note About the Examples in this Chapter
      2. Saving Data to a Pickle File
      3. Loading Data from a Pickle File
      4. Pickling Without a File
      5. Bytes and Strings Rear Their Ugly Heads Again
      6. Debugging Pickle Files
      7. Serializing Python Objects to be Read by Other Languages
      8. Saving Data to a JSON File
      9. Mapping Python Datatypes to JSON
      10. Serializing Datatypes Unsupported by JSON
      11. Loading Data from a JSON File
      12. Further Reading Online
    25. CHAPTER 14 HTTP Web Services
      1. Features of HTTP
        1. Caching
        2. Last-Modified Checking
        3. ETags
        4. Compression
        5. Redirects
      2. How Not to Fetch Data Over HTTP
      3. What’s On the Wire?
      4. Introducing httplib2 (1/3)
      5. Introducing httplib2 (2/3)
      6. Introducing httplib2 (3/3)
        1. Caching with httplib2
        2. Handling Last-Modified and ETag Headers with httplib2
        3. Handling Compression with httplib2
        4. Handling Redirects with httplib2
      7. Beyond HTTP GET
      8. Beyond HTTP POST
      9. Further Reading Online
    26. CHAPTER 15 Case Study: Porting chardet to Python 3
      1. What Is Character Encoding Auto-Detection?
        1. Why Auto-Detection Is Difficult
        2. Auto-Encoding Algorithms
      2. Introducing the chardet Module
        1. UTF-n with a BOM
        2. Escaped Encodings
        3. Multibyte Encodings
        4. Single-Byte Encodings
        5. windows-1252
      3. Running 2to3
      4. A Short Digression Into Multi-File Modules
      5. Fixing What 2to3 Can’t (1/4)
      6. Fixing What 2to3 Can’t (2/4)
      7. Fixing What 2to3 Can’t (3/4)
      8. Fixing What 2to3 Can’t (4/4)
        1. False Is Invalid Syntax
        2. No Module Named Constants
        3. Name 'file' Is Not Defined
        4. Can’t Use a String Pattern on a Bytes-Like Object
        5. Can’t Convert 'bytes' Object to str Implicitly
        6. Unsupported Operand type(s) for +: 'int' and 'bytes'
        7. ord() Expected String of Length 1, but int Found
        8. Unorderable Types: int() >= str()
        9. Global Name 'reduce' Is not Defined
      9. Lessons Learned
    27. CHAPTER 16 Packaging Python Libraries
      1. Things Distutils Can’t Do for You
      2. Directory Structure
        1. Writing Your Setup Script
      3. Classifying Your Package
        1. Examples of Good Package Classifiers
      4. Checking Your Setup Script for Errors
      5. Creating a Source Distribution
      6. Creating a Graphical Installer
        1. Building Installable Packages for Other Operating Systems
      7. Adding Your Software to the Python Package Index
      8. The Many Possible Futures of Python Packaging
      9. Further Reading Online
    28. APPENDIX A Porting Code to Python 3 with 2to3
      1. print Statement
      2. Unicode String Literals
      3. unicode() Global Function
      4. long Datatype
      5. <> Comparison
      6. has_key() Dictionary Method
      7. Dictionary Methods that Return Lists
      8. Renamed or Reorganized Modules
        1. http
        2. urllib
        3. dbm
        4. xmlrpc
        5. Other Modules
      9. Relative Imports Within a Package
      10. next() Iterator Method
      11. filter() Global Function
      12. map() Global Function
      13. reduce() Global Function
      14. apply() Global Function
      15. intern() Global Function
      16. exec Statement
      17. execfile Statement
      18. repr Literals (Backticks)
      19. try...except Statement
      20. raise Statement
      21. throw Method on Generators
      22. xrange() Global Function
      23. raw_input() and input() Global Functions
      24. func_* Function Attributes
      25. xreadlines() I/O Method
      26. lambda Functions that Take a Tuple Instead of Multiple Parameters
      27. Special Method Attributes
      28. __nonzero__ Special Method
      29. Octal Literals
      30. sys.maxint
      31. callable() Global Function
      32. zip() Global Function
      33. StandardError Exception
      34. types Module Constants
      35. isinstance() Global Function
      36. basestring Datatype
      37. itertools Module
      38. sys.exc_type, sys.exc_value, sys.exc_traceback
      39. List Comprehensions Over Tuples
      40. os.getcwdu() Function
      41. Metaclasses
      42. Matters of Style
        1. set() Literals (Explicit)
        2. buffer() Global Function (Explicit)
        3. Whitespace Around Commas (Explicit)
        4. Common Idioms (Explicit)
    29. APPENDIX B Special Method Names
      1. Basics
      2. Classes that Act Like Iterators
      3. Computed Attributes
      4. Classes that Act Like Functions
      5. Classes that Act Like Sequences
      6. Classes that Act Like Dictionaries
      7. Classes that Act Like Numbers
      8. Classes that Can Be Compared
      9. Classes that Can Be Serialized
      10. Classes that Can Be Used in a “with” Block
      11. Really Esoteric Stuff
      12. Further Reading Online
    30. APPENDIX C Where to Go From Here
    31. Index (1/3)
    32. Index (2/3)
    33. Index (3/3)

Product information

  • Title: Dive Into Python 3
  • Author(s): Mark Pilgrim
  • Release date: March 2010
  • Publisher(s): Apress
  • ISBN: None