Python in a Nutshell: A Desktop Quick Reference

This document was uploaded by one of our users. The uploader already confirmed that they had the permission to publish it. If you are author/publisher or own the copyright of this documents, please report to us by using this DMCA report form.

Simply click on the Download Book button.

Yes, Book downloads on Ebookily are 100% Free.

Sometimes the book is free on Amazon As well, so go ahead and hit "Search on Amazon"

Python was recently ranked as today's most popular programming language on the TIOBE index, thanks to its broad applicability to design and prototyping to testing, deployment, and maintenance. With this updated fourth edition, you'll learn how to get the most out of Python, whether you're a professional programmer or someone who needs this language to solve problems in a particular field. Carefully curated by recognized experts in Python, this new edition focuses on version 3.10, bringing this seminal work on the Python language fully up to date on five version releases, including preview coverage of upcoming 3.11 features. This handy guide will help you: • Learn how Python represents data and program as objects • Understand the value and uses of type annotations • Examine which language features appeared in which recent versions • Discover how to use modern Python idiomatically • Learn ways to structure Python projects appropriately • Understand how to debug Python code

Author(s): Alex Martelli, Anna Ravenscroft, Steve Holden, Paul McGuire
Edition: 4
Publisher: O'Reilly Media
Year: 2023

Language: English
Commentary: Publisher's PDF. Revision History for the Fourth Edition 2023-01-09: First Release
Pages: 735
City: Sebastopol, CA
Tags: Programming; Debugging; Multithreading; Python; Concurrency; HTTP; Object-Oriented Programming; Modules; Packages; Networking; Testing; XML; Standard Library; Regular Expressions; Metaprogramming; Persistence; Email; Code Optimization; Exception Handling; Elementary; HTML; Type Annotations; Database Access

Cover
Copyright
Table of Contents
Preface
How To Use This Book
Part I, Getting Started with Python
Part II, Core Python Language and Built-ins
Part III, Python Library and Extension Modules
Part IV, Network and Web Programming
Part V, Extending, Distributing, and Version Upgrade and Migration
Conventions Used in This Book
Reference Conventions
Version Conventions
Typographic Conventions
Using Code Examples
O’Reilly Online Learning
How to Contact Us
Acknowledgments
Chapter 1. Introduction to Python
The Python Language
The Python Standard Library and Extension Modules
Python Implementations
CPython
PyPy
Choosing Between CPython, PyPy, and Other Implementations
Other Developments, Implementations, and Distributions
Licensing and Price Issues
Python Development and Versions
Python Resources
Documentation
Community
Installation
Installing Python from Binaries
Installing Python from Source Code
Microsoft Windows
Unix-Like Platforms
Chapter 2. The Python Interpreter
The python Program
Environment Variables
Command-Line Syntax and Options
The Windows py Launcher
The PyPy Interpreter
Interactive Sessions
Python Development Environments
IDLE
Other Python IDEs
Free Text Editors with Python Support
Tools for Checking Python Programs
Running Python Programs
Running Python in the Browser
PyScript
Jupyter
Chapter 3. The Python Language
Lexical Structure
Lines and Indentation
Character Sets
Tokens
Statements
Data Types
Numbers
Sequences
Sets
Dictionaries
None
Ellipsis (...)
Callables
Boolean Values
Variables and Other References
Variables
Assignment Statements
del Statements
Expressions and Operators
Comparison Chaining
Short-Circuiting Operators
Assignment Expressions
Numeric Operations
Numeric Conversions
Arithmetic Operations
Bitwise Operations on Integers
Sequence Operations
Sequences in General
Strings
Tuples
Lists
Set Operations
Set Membership
Set Methods
Dictionary Operations
Dictionary Membership
Indexing a Dictionary
Dictionary Methods
Control Flow Statements
The if Statement
The match Statement
The while Statement
The for Statement
The break Statement
The continue Statement
The else Clause on Loop Statements
The pass Statement
The try and raise Statements
The with Statement
Functions
Defining Functions: The def Statement
Parameters
Attributes of Function Objects
Function Annotations
The return Statement
Calling Functions
Namespaces
lambda Expressions
Generators
Recursion
Chapter 4. Object-Oriented Python
Classes and Instances
Python Classes
The class Statement
The Class Body
Descriptors
Instances
Attribute Reference Basics
Bound and Unbound Methods
Inheritance
The Built-in object Type
Class-Level Methods
Properties
__slots__
__getattribute__
Per Instance Methods
Inheritance from Built-in Types
Special Methods
General-Purpose Special Methods
Special Methods for Containers
Abstract Base Classes
Special Methods for Numeric Objects
Decorators
Metaclasses
Alternatives to Custom Metaclasses for Simple Class Customization
How Python Determines a Class’s Metaclass
How a Metaclass Creates a Class
Data Classes
Enumerated Types (Enums)
Chapter 5. Type Annotations
History
Type-Checking Utilities
mypy
Other Type Checkers
Type Annotation Syntax
The typing Module
Types
Type Expression Parameters
Abstract Base Classes
Protocols
Utilities and Decorators
Defining Custom Types
Using Type Annotations at Runtime
How to Add Type Annotations to Your Code
Adding Type Annotations to New Code
Adding Type Annotations to Existing Code (Gradual Typing)
Using .pyi Stub Files
Summary
Chapter 6. Exceptions
The try Statement
try/except
try/finally
try/except/finally
The raise Statement
The with Statement and Context Managers
Generators and Exceptions
Exception Propagation
Exception Objects
The Hierarchy of Standard Exceptions
Standard Exception Classes
Custom Exception Classes
Custom Exceptions and Multiple Inheritance
Other Exceptions Used in the Standard Library
ExceptionGroup and except*
Error-Checking Strategies
LBYL Versus EAFP
Handling Errors in Large Programs
Logging Errors
The assert Statement
Chapter 7. Modules and Packages
Module Objects
The import Statement
The from Statement
Module Loading
Built-in Modules
Searching the Filesystem for a Module
The Main Program
Reloading Modules
Circular Imports
Custom Importers
Packages
Special Attributes of Package Objects
Absolute Versus Relative Imports
Distribution Utilities (distutils) and setuptools
Python Environments
Enter the Virtual Environment
What Is a Virtual Environment?
Creating and Deleting Virtual Environments
Working with Virtual Environments
Managing Dependency Requirements
Other Environment Management Solutions
Best Practices with Virtualenvs
Chapter 8. Core Built-ins and Standard Library Modules
Built-in Types
Built-in Functions
The sys Module
The copy Module
The collections Module
ChainMap
Counter
OrderedDict
defaultdict
deque
The functools Module
The heapq Module
The Decorate–Sort–Undecorate Idiom
The argparse Module
The itertools Module
Chapter 9. Strings and Things
Methods of String Objects
The string Module
String Formatting
Formatted String Literals (F-Strings)
Formatting Using format Calls
Value Conversion
Value Formatting: The Format Specifier
Nested Format Specifications
Formatting of User-Coded Classes
Legacy String Formatting with %
Format Specifier Syntax
Text Wrapping and Filling
The pprint Module
The reprlib Module
Unicode
The codecs Module
The unicodedata Module
Chapter 10. Regular Expressions
Regular Expressions and the re Module
REs and bytes Versus str
Pattern String Syntax
Common Regular Expression Idioms
Sets of Characters
Alternatives
Groups
Optional Flags
Match Versus Search
Anchoring at String Start and End
Regular Expression Objects
Match Objects
Functions of the re Module
REs and the := Operator
The Third-Party regex Module
Chapter 11. File and Text Operations
The io Module
Creating a File Object with open
Attributes and Methods of File Objects
Iteration on File Objects
File-Like Objects and Polymorphism
The tempfile Module
Auxiliary Modules for File I/O
The fileinput Module
The struct Module
In-Memory Files: io.StringIO and io.BytesIO
Archived and Compressed Files
The tarfile Module
The zipfile Module
The os Module
Filesystem Operations
The os.path Module
OSError Exceptions
The errno Module
The pathlib Module
The stat Module
The filecmp Module
The fnmatch Module
The glob Module
The shutil Module
Text Input and Output
Standard Output and Standard Error
The print Function
Standard Input
The getpass Module
Richer-Text I/O
The readline Module
Console I/O
Internationalization
The locale Module
The gettext Module
More Internationalization Resources
Chapter 12. Persistence and Databases
Serialization
The csv Module
The json Module
The pickle Module
The shelve Module
DBM Modules
The dbm Package
Examples of DBM-Like File Use
The Python Database API (DBAPI)
Exception Classes
Thread Safety
Parameter Style
Factory Functions
Type Description Attributes
The connect Function
Connection Objects
Cursor Objects
DBAPI-Compliant Modules
SQLite
Chapter 13. Time Operations
The time Module
The datetime Module
The date Class
The time Class
The datetime Class
The timedelta Class
The tzinfo Abstract Class
The timezone Class
The zoneinfo Module
The dateutil Module
The sched Module
The calendar Module
Chapter 14. Customizing Execution
Per-Site Customization
Termination Functions
Dynamic Execution and exec
Avoiding exec
Expressions
compile and Code Objects
Never exec or eval Untrusted Code
Internal Types
Type Objects
The Code Object Type
The Frame Type
Garbage Collection
The gc Module
The weakref Module
Chapter 15. Concurrency: Threads and Processes
Threads in Python
The threading Module
Thread Objects
Thread Synchronization Objects
Thread Local Storage
The queue Module
The multiprocessing Module
Differences Between multiprocessing and threading
Sharing State: Classes Value, Array, and Manager
Process Pools
The concurrent.futures Module
Threaded Program Architecture
Process Environment
Running Other Programs
Using the Subprocess Module
Running Other Programs with the os Module
The mmap Module
Methods of mmap Objects
Using mmap Objects for IPC
Chapter 16. Numeric Processing
Floating-Point Values
The math and cmath Modules
The statistics Module
The operator Module
Random and Pseudorandom Numbers
The random Module
Crypto-Quality Random Numbers: The secrets Module
The fractions Module
The decimal Module
Array Processing
The array Module
Extensions for Numeric Array Computation
Chapter 17. Testing, Debugging, and Optimizing
Testing
Unit Testing and System Testing
The doctest Module
The unittest Module
Testing with nose2
Testing with pytest
Debugging
Before You Debug
The inspect Module
The traceback Module
The pdb Module
Other Debugging Modules
The warnings Module
Classes
Objects
Filters
Functions
Optimization
Developing a Fast-Enough Python Application
Benchmarking
Large-Scale Optimization
Profiling
Small-Scale Optimization
Chapter 18. Networking Basics
The Berkeley Socket Interface
Socket Addresses
Client/Server Computing
The socket Module
Socket Objects
A Connectionless Socket Client
A Connectionless Socket Server
A Connection-Oriented Socket Client
A Connection-Oriented Socket Server
Transport Layer Security
SSLContext
Chapter 19. Client-Side Network Protocol Modules
Email Protocols
The poplib Module
The smtplib Module
HTTP and URL Clients
URL Access
The urllib Package
The Third-Party requests Package
Other Network Protocols
Chapter 20. Serving HTTP
http.server
WSGI
WSGI Servers
ASGI
Python Web Frameworks
“Full-Stack” Versus “Lightweight” Frameworks
A Few Popular Full-Stack Frameworks
Considerations When Using Lightweight Frameworks
A Few Popular Lightweight Frameworks
Chapter 21. Email, MIME, and Other Network Encodings
MIME and Email Format Handling
Functions in the email Package
The email.message Module
The email.Generator Module
Creating Messages
The email.encoders Module
The email.utils Module
Example Uses of the email Package
Encoding Binary Data as ASCII Text
The base64 Module
The quopri Module
The uu Module
Chapter 22. Structured Text: HTML
The html.entities Module
The BeautifulSoup Third-Party Package
The BeautifulSoup Class
The Navigable Classes of bs4
bs4 find… Methods (aka Search Methods)
bs4 CSS Selectors
An HTML Parsing Example with BeautifulSoup
Generating HTML
Editing and Creating HTML with bs4
Building HTML with bs4
Templating
The jinja2 Package
Chapter 23. Structured Text: XML
ElementTree
The Element Class
The ElementTree Class
Functions in the ElementTree Module
Parsing XML with ElementTree.parse
Selecting Elements from an ElementTree
Editing an ElementTree
Building an ElementTree from Scratch
Parsing XML Iteratively
Chapter 24. Packaging Programs and Extensions
What We Don’t Cover in This Chapter
A Brief History of Python Packaging
Online Material
Chapter 25. Extending and Embedding Classic Python
Online Material
Chapter 26. v3.7 to v3.n Migration
Significant Changes in Python Through 3.11
Planning a Python Version Upgrade
Choosing a Target Version
Scoping the Work
Applying the Code Changes
Upgrade Automation Using pyupgrade
Multiversion Testing
Use a Controlled Deployment Process
How Often Should You Upgrade?
Summary
Appendix A. New Features and Changes in Python 3.7 Through 3.11
Python 3.7
Python 3.8
Python 3.9
Python 3.10
Python 3.11
Index