You are previewing Python in a Nutshell, 3rd Edition.
O'Reilly logo
Python in a Nutshell, 3rd Edition

Book Description

This Nutshell guide provides a one-stop shop for Python programmers who need help remembering or deciphering the syntax of this open source language and its many powerful but scantily documented modules. The third edition of this comprehensive reference guide makes it easy for you to look up the most frequently needed information—not just about the Python language itself, but also the most frequently used parts of the standard library and the most important third-party extensions. It's been revised and expanded to cover both versions 2.7 and 3.5, and highlights of version 3.6.

Table of Contents

  1. Preface
    1. How This Book Is Organized
      1. Part I, Getting Started with Python
      2. Part II, Core Python Language and Built-ins
      3. Part III, Python Library and Extension Modules
      4. Part IV, Network and Web Programming
      5. Part V, Extending and Distributing
    2. Conventions Used in This Book
      1. Reference Conventions
      2. Version Conventions
      3. Typographic Conventions
    3. Using Code Examples
    4. How to Contact Us
    5. Safari® Enabled
    6. Acknowledgments
  2. 1. Introduction to Python
    1. The Python Language
    2. The Python Standard Library and Extension Modules
    3. Python Implementations
      1. CPython
      2. Jython
      3. IronPython
      4. Choosing Between CPython, Jython, IronPython, and PyPy
      5. Other Developments, Implementations and Distributions
      6. Licensing and Price Issues
    4. Python Development and Versions
    5. Python Resources
      1. Documentation
      2. Community
    6. Installation
      1. Installing Python from Binaries
        1. Mac OS X
      2. Installing Python from source code
        1. Microsoft Windows
        2. Unix-Like Platforms
      3. Installing Jython
      4. Installing IronPython
      5. Installing PyPy
  3. 2. The Python Interpreter
    1. The python Program
      1. Environment Variables
      2. Command-Line Syntax and Options
      3. Interactive Sessions
    2. Python Development Environments
      1. IDLE
      2. Other Python IDEs
      3. Free Text Editors with Python Support
      4. Tools for Checking Python Programs
    3. Running Python Programs
    4. The jython Interpreter
    5. The IronPython Interpreter
    6. The PyPy Interpreter
  4. 3. The Python Language
    1. Lexical Structure
      1. Lines and Indentation
      2. Character Sets
      3. Tokens
      4. Statements
    2. Data Types
      1. Numbers
      2. Sequences
      3. Sets
      4. Dictionaries
      5. None
      6. Callables
      7. Boolean Values
    3. Variables and Other References
      1. Variables
      2. Assignment Statements
      3. del Statements
    4. Expressions and Operators
      1. Comparison Chaining
      2. Short-Circuiting Operators
    5. Numeric Operations
      1. Numeric Conversions
      2. Arithmetic Operations
      3. Comparisons
      4. Bitwise Operations on Integers
    6. Sequence Operations
      1. Sequences in General
      2. Strings
      3. Tuples
      4. Lists
    7. Set Operations
      1. Set Membership
      2. Set Methods
    8. Dictionary Operations
      1. Dictionary Membership
      2. Indexing a Dictionary
      3. Dictionary Methods
    9. Control Flow Statements
      1. The if Statement
      2. The while Statement
      3. The for Statement
      4. The break Statement
      5. The continue Statement
      6. The else Clause on Loop Statements
      7. The pass Statement
      8. The try and raise Statements
      9. The with Statement
    10. Functions
      1. The def Statement
      2. Parameters
      3. “Keyword-only” Parameters (v3 Only)
      4. Attributes of Function Objects
      5. Function Annotations and Type Hints (v3 Only)
      6. The return Statement
      7. Calling Functions
      8. Namespaces
      9. lambda Expressions
      10. Generators
      11. Recursion
  5. 4. Object-Oriented Python
    1. Classes and Instances
      1. Python Classes
      2. The class Statement
      3. The Class Body
      4. Descriptors
      5. Instances
      6. Attribute Reference Basics
      7. Bound and Unbound Methods
      8. Inheritance
      9. The Built-in object Type
      10. Class-Level Methods
      11. Properties
      12. __slots__
      13. __getattribute__
      14. Per-Instance Methods
      15. Inheritance from Built-in Types
    2. Special Methods
      1. General-Purpose Special Methods
      2. Special Methods for Containers
      3. Abstract Base Classes
      4. Special Methods for Numeric Objects
    3. Decorators
    4. Metaclasses
      1. How Python v2 Determines a Class’s Metaclass
      2. How Python v3 Determines a Class’s Metaclass
      3. How a Metaclass Creates a Class
  6. 5. Exceptions
    1. The try Statement
      1. try/except
      2. try/finally
      3. The try/except/finally statement
    2. The with statement and context managers
      1. Generators and exceptions
    3. Exception Propagation
    4. The raise Statement
    5. Exception Objects
      1. The Hierarchy of Standard Exceptions
      2. Standard Exception Classes
      3. Exceptions “wrapping” other exceptions or tracebacks
    6. Custom Exception Classes
      1. Custom Exceptions and Multiple Inheritance
      2. Other Exceptions Used in the Standard Library
    7. Error-Checking Strategies
      1. LBYL Versus EAFP
      2. Handling Errors in Large Programs
      3. Logging Errors
    8. The assert Statement
      1. The __debug__ built-in variable
  7. 6. Modules
    1. Module Objects
      1. The import Statement
      2. The from Statement
    2. Module Loading
      1. Built-in Modules
      2. Searching the Filesystem for a Module
      3. The Main Program
      4. Reloading Modules
      5. Circular Imports
      6. sys.modules Entries
      7. Custom Importers
    3. Packages
      1. Special attributes of package objects
      2. Absolute Versus Relative Imports
    4. Distribution Utilities (distutils) and setuptools
      1. Python Wheels (and Eggs)
      2. Python Environments
  8. 7. Core Built-ins and Standard Library Modules
    1. Built-in Types
    2. Built-in Functions
    3. The sys Module
    4. The copy Module
    5. The collections Module
      1. ChainMap
      2. Counter
      3. OrderedDict
      4. defaultdict
      5. deque
      6. namedtuple
    6. The functools Module
    7. The heapq Module
      1. The Decorate-Sort-Undecorate Idiom
    8. The argparse Module
    9. The itertools Module
  9. 8. Strings and Things
    1. Methods of String and Bytes Objects
    2. The string Module
    3. String Formatting
      1. Value Selection
      2. Value Conversion
      3. Value Formatting
      4. New in 3.6: Formatted String Literals
      5. Legacy string formatting with %
        1. Format Specifier Syntax
    4. Text Wrapping and Filling
    5. The pprint Module
    6. The reprlib Module
    7. Unicode
      1. The codecs Module
      2. The unicodedata Module
  10. 9. Regular Expressions
    1. Regular Expressions and the re Module
      1. re and bytes vs unicode strings
      2. Pattern-String Syntax
      3. Common Regular Expression Idioms
      4. Sets of Characters
      5. Alternatives
      6. Groups
      7. Optional Flags
      8. Match Versus Search
      9. Anchoring at String Start and End
      10. Regular Expression Objects
      11. Match Objects
      12. Functions of Module re
  11. 10. File and Text Operations
    1. Other Chapters That Also Deal with Files
    2. Organization of This Chapter
    3. The io module
      1. Creating a “file” Object with io.open
      2. Attributes and Methods of “file” Objects
      3. Iteration on File Objects
      4. File-Like Objects and Polymorphism
      5. The tempfile Module
    4. Auxiliary Modules for File I/O
      1. The fileinput Module
      2. The linecache Module
      3. The struct Module
    5. In-memory “files”: io.StringIO and io.BytesIO
    6. Compressed Files
      1. The gzip Module
      2. The bz2 Module
      3. The tarfile Module
      4. The zipfile Module
      5. The zlib Module
    7. The os Module
      1. OSError Exceptions
      2. The errno Module
    8. Filesystem Operations
      1. Path-String Attributes of the os Module
      2. Permissions
      3. File and Directory Functions of the os Module
      4. The os.path Module
      5. The stat Module
      6. The filecmp Module
      7. The fnmatch Module
      8. The glob Module
      9. The shutil Module
      10. File Descriptor Operations
    9. Text Input and Output
      1. Standard Output and Standard Error
      2. The print Function
      3. Standard Input
      4. The getpass Module
    10. Richer-Text I/O
      1. The readline Module
      2. Console I/O
    11. Interactive Command Sessions
      1. Initializing a Cmd Instance
      2. Methods of Cmd Instances
      3. Attributes of Cmd Instances
      4. A Cmd Example
    12. Internationalization
      1. The locale Module
      2. The gettext Module
      3. More Internationalization Resources
  12. 11. Persistence and Databases
    1. Serialization
      1. The json module
      2. The pickle and cPickle Modules
      3. The shelve Module
    2. DBM Modules
      1. The v3 dbm Package
      2. v2’s dbm modules
      3. Examples of DBM-Like File Use
    3. Berkeley DB Interfacing
    4. The Python Database API (DBAPI) 2.0
      1. Exception Classes
      2. Thread Safety
      3. Parameter Style
      4. Factory Functions
      5. Type Description Attributes
      6. The connect Function
      7. Connection Objects
      8. Cursor Objects
      9. DBAPI-Compliant Modules
      10. SQLite
  13. 12. Time Operations
    1. The time Module
    2. The datetime Module
      1. Class date
      2. Class time
      3. Class datetime
      4. Class timedelta
    3. The pytz Module
    4. The dateutil Module
    5. The sched Module
    6. The calendar Module
  14. 13. Controlling Execution
    1. Site and User Customization
      1. The site and sitecustomize Modules
      2. User Customization
    2. Termination Functions
    3. Dynamic Execution and exec
      1. Avoiding exec
      2. Expressions
      3. Compile and Code Objects
      4. Never exec nor eval Untrusted Code
    4. Internal Types
      1. Type Objects
      2. The Code Object Type
      3. The frame Type
    5. Garbage Collection
      1. The gc Module
      2. The weakref Module
  15. 14. Threads and Processes
    1. Threads in Python
    2. The threading Module
      1. Thread Objects
      2. Thread Synchronization Objects
      3. Thread Local Storage
    3. The queue Module
      1. Methods of Queue Instances
    4. The multiprocessing Module
      1. Differences between multiprocessing and threading
      2. Sharing state: classes Value, Array, and Manager
      3. Multiprocessing Pool
    5. The concurrent.futures Module
    6. Threaded Program Architecture
    7. Process Environment
    8. Running Other Programs
      1. Running Other Programs with the os Module
      2. The Subprocess Module
    9. The mmap Module
      1. Methods of mmap Objects
      2. Using mmap Objects for IPC
  16. 15. Asynchronous Alternatives
    1. Coroutine-based async architectures
    2. The asyncio module (v3 only)
      1. Coroutines in asyncio
      2. asyncio’s Event Loop
      3. Synchronization and Queues
    3. The selectors module
      1. selectors events
      2. The SelectorKey class
      3. The BaseSelector class
      4. When to use selectors
  17. 16. Numeric Processing
    1. The math and cmath Modules
    2. The operator Module
    3. Random and Pseudorandom Numbers
      1. Physically random and cryptographically strong random numbers
      2. The random module
    4. The fractions Module
    5. The decimal Module
    6. The gmpy2 Module
  18. 17. Testing, Debugging, and Optimizing
    1. Testing
      1. Unit Testing and System Testing
      2. The doctest Module
      3. The unittest Module
    2. Debugging
      1. Before You Debug
      2. The inspect Module
      3. The traceback Module
      4. The pdb Module
    3. The warnings Module
      1. Classes
      2. Objects
      3. Filters
      4. Functions
    4. Optimization
      1. Developing a Fast-Enough Python Application
      2. Benchmarking
      3. Large-Scale Optimization
      4. Profiling
      5. Small-Scale Optimization
  19. 18. Networking Basics
    1. Networking Principles
    2. The Berkeley Socket Interface
      1. Socket Addresses
      2. Client-Server Computing
      3. The socket Module
      4. Socket Objects
      5. A Connectionless Socket Client
      6. A Connectionless Socket Server
      7. A Connection-Oriented Socket Client
      8. A Connection-Oriented Socket Server
    3. Transport Layer Security (TLS, a.k.a. SSL)
      1. SSLContext
  20. 19. Client-Side Network Protocol Modules
    1. Email Protocols
      1. The poplib Module
      2. The smtplib Module
    2. HTTP and URL clients
      1. URL Access
      2. The third-party Requests package
      3. The urllib Package (v3)
      4. The urllib Module (v2)
      5. The urllib2 Module (v2)
    3. Other network protocols
  21. 20. Serving HTTP
    1. WSGI
      1. WSGI Servers
    2. Python Web Frameworks
  22. 21. Email, MIME and Other Network Encodings
    1. MIME and Email Format Handling
      1. Functions in the email Package
      2. The email.message Module
      3. The email.Generator Module
      4. Creating Messages
      5. The email.encoders Module
      6. The email.utils Module
      7. Example Uses of the email Package
      8. rfc822 and mimetools Modules (v2)
    2. Encoding Binary Data as ASCII Text
      1. The base64 Module
      2. The quopri Module
      3. The uu Module
  23. 22. Structured Text: HTML
    1. The html.entities (v2: htmlentitydefs) Module
  24. 23. Structured Text: XML
    1. ElementTree
    2. class Element
    3. class ElementTree
    4. Functions in module ElementTree
    5. Parsing XML with ElementTree.parse
    6. Building an ElementTree from scratch
      1. Parsing XML iteratively
  25. 24. Extending and Embedding Classic Python
    1. Extending Python with Python’s C API
      1. Building and Installing C-Coded Python Extensions
      2. Overview of C-Coded Python Extension Modules
      3. Return Values of Python’s C API Functions
      4. Module Initialization
      5. Reference Counting
      6. Accessing Arguments
      7. Creating Python Values
      8. Exceptions
      9. Abstract Layer Functions
      10. Concrete Layer Functions
      11. A Simple Extension Example
      12. Defining New Types
    2. Extending Python Without Python’s C API
      1. ctypes
    3. Cython
      1. The cdef and cpdef Statements and Function Parameters
      2. The ctypedef Statement
      3. The for…from Statement
      4. Cython Expressions
      5. A Cython Example: Greatest Common Divisor
    4. Embedding Python
      1. Installing Resident Extension Modules
      2. Setting Arguments
      3. Python Initialization and Finalization
      4. Running Python Code
  26. 25. Distributing Extensions and Programs
    1. setuptools
      1. The Distribution and Its Root
      2. The setup.py Script
      3. The requirements.txt File
      4. The MANIFEST.in File
      5. The setup.cfg File
    2. Distributing your package
      1. Create the Distribution
        1. Registering and Uploading to a Repository
  27. 26. v2/v3 Migration and Coexistence
    1. Preparing for Python 3
    2. Minimizing Syntax Differences
      1. Avoid “Old-Style” Classes
      2. print as a function
      3. String Literals
      4. Numeric Constants
      5. Text and Binary Data
      6. Never Sort Using cmp
      7. except Clauses
      8. Division
      9. Incompatible Syntax to Avoid for Compatibility
    3. Choosing Your Support Strategy
      1. Steady As She Goes—v2-only Support
      2. v2/v3 Support with Conversion
      3. 2to3 Conversion: A Case Study
      4. v2/v3 Support with a Single Source Tree
      5. v3-Only Support