Python in a Nutshell, 3rd Edition

Book description

Useful in many roles, from design and prototyping to testing, deployment, and maintenance, Python is consistently ranked among today’s most popular programming languages. The third edition of this practical book provides a quick reference to the language—including Python 3.5, 2.7, and highlights of 3.6—commonly used areas of its vast standard library, and some of the most useful third-party modules and packages.

Ideal for programmers with some Python experience, and those coming to Python from other programming languages, this book covers a wide range of application areas, including web and network programming, XML handling, database interactions, and high-speed numeric computing. Discover how Python provides a unique mix of elegance, simplicity, practicality, and sheer power.

This edition covers:

  • Python syntax, Object-Oriented Python, standard library modules, and third-party Python packages
  • Python’s support for file and text operations, persistence and databases, concurrent execution, and numeric computations
  • Networking basics, event-driven programming, and client-side network protocol modules
  • Python extension modules, and tools for packaging and distributing extensions, modules, and applications

Publisher resources

View/Submit Errata

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, Distributing, v2/v3 Migration
    2. Conventions Used in This Book
      1. Reference Conventions
      2. Version Conventions
      3. Typographic Conventions
    3. How to Contact Us
    4. O’Reilly Safari
    5. 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. PyPy
      5. Choosing Between CPython, Jython, IronPython, and PyPy
      6. Other Developments, Implementations, and Distributions
      7. Licensing and Price Issues
    4. Python Development and Versions
    5. Python Resources
      1. Documentation
      2. Community
    6. Installation
    7. Installing Python from Binaries
      1. macOS
    8. Installing Python from Source Code
      1. Microsoft Windows
      2. Unix-Like Platforms
    9. Installing Jython
    10. Installing IronPython
    11. 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
    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. Namespace Packages (v3 Only)
      3. Absolute Versus Relative Imports
    4. Distribution Utilities (distutils) and setuptools
      1. Python Wheels (and Eggs)
    5. Python Environments
      1. Enter the Virtual Environment
      2. What Is a Virtual Environment?
      3. Creating and Deleting Virtual Environments
      4. Working with Virtual Environments
      5. Managing Dependency Requirements
      6. Best Practices with virtualenvs
  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 (v3 Only)
      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 %
      6. 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 Versus 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 the re Module
  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
      11. class sqlite3.Connection
      12. class sqlite3.Row
  13. 12. Time Operations
    1. The time Module
    2. The datetime Module
      1. The date Class
      2. The time Class
      3. The datetime Class
      4. The timedelta Class
    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 or 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. 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
    7. Array Processing
    8. The array Module
    9. Extensions for Numeric Array Computation
      1. NumPy
      2. Creating a NumPy Array
      3. Shape, Indexing, and Slicing
      4. Matrix Operations in NumPy
      5. SciPy
  17. 16. 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
  18. 17. 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, AKA SSL)
      1. SSLContext
  19. 18. 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
  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
      1. Full-Stack Versus Lightweight Frameworks
      2. A Few Popular Full-Stack Frameworks
      3. Some Popular Lightweight 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
      1. The BeautifulSoup Third-Party Package
      2. The BeautifulSoup Class
      3. The Navigable Classes of bs4
      4. bs4 find... Methods (“Search Methods”)
      5. bs4 CSS Selectors
      6. An HTML Parsing Example with BeautifulSoup
    2. Generating HTML
      1. Editing and Creating HTML with bs4
      2. Building HTML with bs4
      3. Templating
      4. The jinja2 Package
      5. The jinja2.Environment Class
      6. The jinja2.Template Class
  24. 23. Structured Text: XML
    1. ElementTree
      1. The Element Class
      2. The ElementTree Class
      3. Functions in the ElementTree Module
      4. Parsing XML with ElementTree.parse
      5. Building an ElementTree from Scratch
      6. 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. The Initialization Module
      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
      2. 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
  28. Index

Product information

  • Title: Python in a Nutshell, 3rd Edition
  • Author(s): Alex Martelli, Anna Ravenscroft, Steve Holden
  • Release date: April 2017
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9781449392925