Home Page Icon
Home Page
Table of Contents for
Python
Close
Python
by Steve Holden, Anna Ravenscroft, Alex Martelli
Python in a Nutshell, 3rd Edition
Preface
How This Book Is Organized
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, v2/v3 Migration
Conventions Used in This Book
Reference Conventions
Version Conventions
Typographic Conventions
How to Contact Us
O’Reilly Safari
Acknowledgments
1. Introduction to Python
The Python Language
The Python Standard Library and Extension Modules
Python Implementations
CPython
Jython
IronPython
PyPy
Choosing Between CPython, Jython, IronPython, and PyPy
Other Developments, Implementations, and Distributions
Licensing and Price Issues
Python Development and Versions
Python Resources
Documentation
Community
Installation
Installing Python from Binaries
macOS
Installing Python from Source Code
Microsoft Windows
Unix-Like Platforms
Installing Jython
Installing IronPython
Installing PyPy
2. The Python Interpreter
The python Program
Environment Variables
Command-Line Syntax and Options
Interactive Sessions
Python Development Environments
IDLE
Other Python IDEs
Free Text Editors with Python Support
Tools for Checking Python Programs
Running Python Programs
The jython Interpreter
The IronPython Interpreter
The PyPy Interpreter
3. The Python Language
Lexical Structure
Lines and Indentation
Character Sets
Tokens
Statements
Data Types
Numbers
Sequences
Sets
Dictionaries
None
Callables
Boolean Values
Variables and Other References
Variables
Assignment Statements
del Statements
Expressions and Operators
Comparison Chaining
Short-Circuiting Operators
Numeric Operations
Numeric Conversions
Arithmetic Operations
Comparisons
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 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
The def Statement
Parameters
“Keyword-only” Parameters (v3 Only)
Attributes of Function Objects
Function Annotations and Type Hints (v3 Only)
The return Statement
Calling Functions
Namespaces
lambda Expressions
Generators
Recursion
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
How Python v2 Determines a Class’s Metaclass
How Python v3 Determines a Class’s Metaclass
How a Metaclass Creates a Class
5. Exceptions
The try Statement
try/except
try/finally
The try/except/finally Statement
The with Statement and Context Managers
Generators and Exceptions
Exception Propagation
The raise Statement
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
Error-Checking Strategies
LBYL Versus EAFP
Handling Errors in Large Programs
Logging Errors
The assert Statement
The __debug__ Built-in Variable
6. Modules
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
sys.modules Entries
Custom Importers
Packages
Special Attributes of Package Objects
Namespace Packages (v3 Only)
Absolute Versus Relative Imports
Distribution Utilities (distutils) and setuptools
Python Wheels (and Eggs)
Python Environments
Enter the Virtual Environment
What Is a Virtual Environment?
Creating and Deleting Virtual Environments
Working with Virtual Environments
Managing Dependency Requirements
Best Practices with virtualenvs
7. Core Built-ins and Standard Library Modules
Built-in Types
Built-in Functions
The sys Module
The copy Module
The collections Module
ChainMap (v3 Only)
Counter
OrderedDict
defaultdict
deque
namedtuple
The functools Module
The heapq Module
The Decorate-Sort-Undecorate Idiom
The argparse Module
The itertools Module
8. Strings and Things
Methods of String and Bytes Objects
The string Module
String Formatting
Value Selection
Value Conversion
Value Formatting
New in 3.6: Formatted String Literals
Legacy String Formatting with %
Format Specifier Syntax
Text Wrapping and Filling
The pprint Module
The reprlib Module
Unicode
The codecs Module
The unicodedata Module
9. Regular Expressions
Regular Expressions and the re Module
re and bytes Versus Unicode Strings
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
10. File and Text Operations
Other Chapters That Also Deal with Files
Organization of This Chapter
The io Module
Creating a “file” Object with io.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 linecache Module
The struct Module
In-Memory “Files”: io.StringIO and io.BytesIO
Compressed Files
The gzip Module
The bz2 Module
The tarfile Module
The zipfile Module
The zlib Module
The os Module
OSError Exceptions
The errno Module
Filesystem Operations
Path-String Attributes of the os Module
Permissions
File and Directory Functions of the os Module
The os.path Module
The stat Module
The filecmp Module
The fnmatch Module
The glob Module
The shutil Module
File Descriptor Operations
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
Interactive Command Sessions
Initializing a Cmd Instance
Methods of Cmd Instances
Attributes of Cmd Instances
A Cmd Example
Internationalization
The locale Module
The gettext Module
More Internationalization Resources
11. Persistence and Databases
Serialization
The json Module
The pickle and cPickle Modules
The shelve Module
DBM Modules
The v3 dbm Package
v2’s dbm modules
Examples of DBM-Like File Use
Berkeley DB Interfacing
The Python Database API (DBAPI) 2.0
Exception Classes
Thread Safety
Parameter Style
Factory Functions
Type Description Attributes
The connect Function
Connection Objects
Cursor Objects
DBAPI-Compliant Modules
SQLite
class sqlite3.Connection
class sqlite3.Row
12. Time Operations
The time Module
The datetime Module
The date Class
The time Class
The datetime Class
The timedelta Class
The pytz Module
The dateutil Module
The sched Module
The calendar Module
13. Controlling Execution
Site and User Customization
The site and sitecustomize Modules
User 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
14. Threads and Processes
Threads in Python
The threading Module
Thread Objects
Thread Synchronization Objects
Thread Local Storage
The queue Module
Methods of Queue Instances
The multiprocessing Module
Differences Between Multiprocessing and Threading
Sharing State: Classes Value, Array, and Manager
Multiprocessing Pool
The concurrent.futures Module
Threaded Program Architecture
Process Environment
Running Other Programs
Running Other Programs with the os Module
The Subprocess Module
The mmap Module
Methods of mmap Objects
Using mmap Objects for IPC
15. Numeric Processing
The math and cmath Modules
The operator Module
Random and Pseudorandom Numbers
Physically Random and Cryptographically Strong Random Numbers
The random Module
The fractions Module
The decimal Module
The gmpy2 Module
Array Processing
The array Module
Extensions for Numeric Array Computation
NumPy
Creating a NumPy Array
Shape, Indexing, and Slicing
Matrix Operations in NumPy
SciPy
16. Testing, Debugging, and Optimizing
Testing
Unit Testing and System Testing
The doctest Module
The unittest Module
Debugging
Before You Debug
The inspect Module
The traceback Module
The pdb Module
The warnings Module
Classes
Objects
Filters
Functions
Optimization
Developing a Fast-Enough Python Application
Benchmarking
Large-Scale Optimization
Profiling
Small-Scale Optimization
17. Networking Basics
Networking Principles
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 (TLS, AKA SSL)
SSLContext
18. Asynchronous Alternatives
Coroutine-Based Async Architectures
The asyncio Module (v3 Only)
Coroutines in asyncio
asyncio’s Event Loop
Synchronization and Queues
The selectors Module
selectors Events
The SelectorKey Class
The BaseSelector Class
When to Use selectors
19. Client-Side Network Protocol Modules
Email Protocols
The poplib Module
The smtplib Module
HTTP and URL Clients
URL Access
The Third-Party requests Package
The urllib Package (v3)
The urllib Module (v2)
The urllib2 Module (v2)
Other Network Protocols
20. Serving HTTP
WSGI
WSGI Servers
Python Web Frameworks
Full-Stack Versus Lightweight Frameworks
A Few Popular Full-Stack Frameworks
Some Popular Lightweight Frameworks
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
rfc822 and mimetools Modules (v2)
Encoding Binary Data as ASCII Text
The base64 Module
The quopri Module
The uu Module
22. Structured Text: HTML
The html.entities (v2: htmlentitydefs) Module
The BeautifulSoup Third-Party Package
The BeautifulSoup Class
The Navigable Classes of bs4
bs4 find... Methods (“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
The jinja2.Environment Class
The jinja2.Template Class
23. Structured Text: XML
ElementTree
The Element Class
The ElementTree Class
Functions in the ElementTree Module
Parsing XML with ElementTree.parse
Building an ElementTree from Scratch
Parsing XML Iteratively
24. Extending and Embedding Classic Python
Extending Python with Python’s C API
Building and Installing C-Coded Python Extensions
Overview of C-Coded Python Extension Modules
Return Values of Python’s C API Functions
The Initialization Module
Reference Counting
Accessing Arguments
Creating Python Values
Exceptions
Abstract Layer Functions
Concrete Layer Functions
A Simple Extension Example
Defining New Types
Extending Python Without Python’s C API
ctypes
Cython
The cdef and cpdef Statements and Function Parameters
The ctypedef Statement
The for…from Statement
Cython Expressions
A Cython Example: Greatest Common Divisor
Embedding Python
Installing Resident Extension Modules
Setting Arguments
Python Initialization and Finalization
Running Python Code
25. Distributing Extensions and Programs
setuptools
The Distribution and Its Root
The setup.py Script
The requirements.txt File
The MANIFEST.in File
The setup.cfg File
Distributing Your Package
Create the Distribution
Registering and Uploading to a Repository
26. v2/v3 Migration and Coexistence
Preparing for Python 3
Minimizing Syntax Differences
Avoid “Old-Style” Classes
print as a Function
String Literals
Numeric Constants
Text and Binary Data
Never Sort Using cmp
except Clauses
Division
Incompatible Syntax to Avoid for Compatibility
Choosing Your Support Strategy
Steady As She Goes—v2-only Support
v2/v3 Support with Conversion
2to3 Conversion: A Case Study
v2/v3 Support with a Single Source Tree
v3-only Support
Index
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Next
Next Chapter
Python in a Nutshell
Python
in a Nutshell
Third Edition
Alex Martelli, Anna Ravenscroft,
and Steve Holden
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset