O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Python 3 Standard Library by Example, Second Edition

Book Description

Master the Powerful Python 3 Standard Library through Real Code Examples

"The genius of Doug's approach is that with 15 minutes per week, any motivated programmer can learn the Python Standard Library. Doug's guided tour will help you flip the switch to fully power-up Python's batteries."

-- Raymond Hettinger, Distinguished Python Core Developer

The Python 3 Standard Library contains hundreds of modules for interacting with the operating system, interpreter, and Internet-all extensively tested and ready to jump-start application development. Now, Python expert Doug Hellmann introduces every major area of the Python 3.x library through concise source code and output examples. Hellmann's examples fully demonstrate each feature and are designed for easy learning and reuse.

You'll find practical code for working with text, data structures, algorithms, dates/times, math, the file system, persistence, data exchange, compression, archiving, crypto, processes/threads, networking, Internet capabilities, email, developer and language tools, the runtime, packages, and more. Each section fully covers one module, with links to additional resources, making this book an ideal tutorial and reference.

The Python 3 Standard Library by Example introduces Python 3.x's new libraries, significant functionality changes, and new layout and naming conventions. Hellmann also provides expert porting guidance for moving code from 2.x Python standard library modules to their Python 3.x equivalents.

  • Manipulate text with string, textwrap, re (regular expressions), and difflib
  • Use data structures: enum, collections, array, heapq, queue, struct, copy, and more
  • Implement algorithms elegantly and concisely with functools, itertools, and contextlib
  • Handle dates/times and advanced mathematical tasks
  • Archive and data compression
  • Understand data exchange and persistence, including json, dbm, and sqlite
  • Sign and verify messages cryptographically
  • Manage concurrent operations with processes and threads
  • Test, debug, compile, profile, language, import, and package tools
  • Control interaction at runtime with interpreters or the environment

Table of Contents

  1. Cover Page
  2. About This E-Book
  3. Title Page
  4. Copyright Page
  5. Dedication Page
  6. Contents at a Glance
  7. Contents
  8. Introduction
    1. This Book’s Target Audience
    2. Python 3 Versus 2
    3. How This Book Is Organized
    4. Downloading the Example Code
  9. Acknowledgments
  10. About the Author
  11. Chapter 1. Text
    1. 1.1 string: Text Constants and Templates
    2. 1.2 textwrap: Formatting Text Paragraphs
    3. 1.3 re: Regular Expressions
    4. 1.4 difflib: Compare Sequences
  12. Chapter 2. Data Structures
    1. 2.1 enum: Enumeration Type
    2. 2.2 collections: Container Data Types
    3. 2.3 array: Sequence of Fixed-Type Data
    4. 2.4 heapq: Heap Sort Algorithm
    5. 2.5 bisect: Maintain Lists in Sorted Order
    6. 2.6 queue: Thread-Safe FIFO Implementation
    7. 2.7 struct: Binary Data Structures
    8. 2.8 weakref: Impermanent References to Objects
    9. 2.9 copy: Duplicate Objects
    10. 2.10 pprint: Pretty-Print Data Structures
  13. Chapter 3. Algorithms
    1. 3.1 functools: Tools for Manipulating Functions
    2. 3.2 itertools: Iterator Functions
    3. 3.3 operator: Functional Interface to Built-In Operators
    4. 3.4 contextlib: Context Manager Utilities
  14. Chapter 4. Dates and Times
    1. 4.1 time: Clock Time
    2. 4.2 datetime: Date and Time Value Manipulation
    3. 4.3 calendar: Work with Dates
  15. Chapter 5. Mathematics
    1. 5.1 decimal: Fixed- and Floating-Point Math
    2. 5.2 fractions: Rational Numbers
    3. 5.3 random: Pseudorandom Number Generators
    4. 5.4 math: Mathematical Functions
    5. 5.5 statistics: Statistical Calculations
  16. Chapter 6. The File System
    1. 6.1 os.path: Platform-Independent Manipulation of Filenames
    2. 6.2 pathlib: File System Paths as Objects
    3. 6.3 glob: Filename Pattern Matching
    4. 6.4 fnmatch: Unix-Style Glob Pattern Matching
    5. 6.5 linecache: Read Text Files Efficiently
    6. 6.6 tempfile: Temporary File System Objects
    7. 6.7 shutil: High-Level File Operations
    8. 6.8 filecmp: Compare Files
    9. 6.9 mmap: Memory-Map Files
    10. 6.10 codecs: String Encoding and Decoding
    11. 6.11 io: Text, Binary, and Raw Stream I/O Tools
  17. Chapter 7. Data Persistence and Exchange
    1. 7.1 pickle: Object Serialization
    2. 7.2 shelve: Persistent Storage of Objects
    3. 7.3 dbm: Unix Key-Value Databases
    4. 7.4 sqlite3: Embedded Relational Database
    5. 7.5 xml.etree.ElementTree: XML Manipulation API
    6. 7.6 csv: Comma-Separated Value Files
  18. Chapter 8. Data Compression and Archiving
    1. 8.1 zlib: GNU zlib Compression
    2. 8.2 gzip: Read and Write GNU zip Files
    3. 8.3 bz2: bzip2 Compression
    4. 8.4 tarfile: Tar Archive Access
    5. 8.5 zipfile: ZIP Archive Access
  19. Chapter 9. Cryptography
    1. 9.1 hashlib: Cryptographic Hashing
    2. 9.2 hmac: Cryptographic Message Signing and Verification
  20. Chapter 10. Concurrency with Processes, Threads, and Coroutines
    1. 10.1 subprocess: Spawning Additional Processes
    2. 10.2 signal: Asynchronous System Events
    3. 10.3 threading: Manage Concurrent Operations Within a Process
    4. 10.4 multiprocessing: Manage Processes Like Threads
    5. 10.5 asyncio: Asynchronous I/O, Event Loop, and Concurrency Tools
    6. 10.6 concurrent.futures: Manage Pools of Concurrent Tasks
  21. Chapter 11. Networking
    1. 11.1 ipaddress: Internet Addresses
    2. 11.2 socket: Network Communication
    3. 11.3 selectors: I/O Multiplexing Abstractions
    4. 11.4 select: Wait for I/O Efficiently
    5. 11.5 socketserver: Creating Network Servers
  22. Chapter 12. The Internet
    1. 12.1 urllib.parse: Split URLs into Components
    2. 12.2 urllib.request: Network Resource Access
    3. 12.3 urllib.robotparser: Internet Spider Access Control
    4. 12.4 base64: Encode Binary Data with ASCII
    5. 12.5 http.server: Base Classes for Implementing Web Servers
    6. 12.6 http.cookies: HTTP Cookies
    7. 12.7 webbrowser: Displays Web Pages
    8. 12.8 uuid: Universally Unique Identifiers
    9. 12.9 json: JavaScript Object Notation
    10. 12.10 xmlrpc.client: Client Library for XML-RPC
    11. 12.11 xmlrpc.server: An XML-RPC Server
  23. Chapter 13. Email
    1. 13.1 smtplib: Simple Mail Transfer Protocol Client
    2. 13.2 smtpd: Sample Mail Servers
    3. 13.3 mailbox: Manipulate Email Archives
    4. 13.4 imaplib: IMAP4 Client Library
  24. Chapter 14. Application Building Blocks
    1. 14.1 argparse: Command-Line Option and Argument Parsing
    2. 14.2 getopt: Command-Line Option Parsing
    3. 14.3 readline: The GNU readline Library
    4. 14.4 getpass: Secure Password Prompt
    5. 14.5 cmd: Line-Oriented Command Processors
    6. 14.6 shlex: Parse Shell-Style Syntaxes
    7. 14.7 configparser: Work with Configuration Files
    8. 14.8 logging: Report Status, Error, and Informational Messages
    9. 14.9 fileinput: Command-Line Filter Framework
    10. 14.10 atexit: Program Shutdown Callbacks
    11. 14.11 sched: Timed Event Scheduler
  25. Chapter 15. Internationalization and Localization
    1. 15.1 gettext: Message Catalogs
    2. 15.2 locale: Cultural Localization API
  26. Chapter 16. Developer Tools
    1. 16.1 pydoc: Online Help for Modules
    2. 16.2 doctest: Testing Through Documentation
    3. 16.3 unittest: Automated Testing Framework
    4. 16.4 trace: Follow Program Flow
    5. 16.5 traceback: Exceptions and Stack Traces
    6. 16.6 cgitb: Detailed Traceback Reports
    7. 16.7 pdb: Interactive Debugger
    8. 16.8 profile and pstats: Performance Analysis
    9. 16.9 timeit: Time the Execution of Small Bits of Python Code
    10. 16.10 tabnanny: Indentation Validator
    11. 16.11 compileall: Byte-Compile Source Files
    12. 16.12 pyclbr: Class Browser
    13. 16.13 venv: Create Virtual Environments
    14. 16.14 ensurepip: Install the Python Package Installer
  27. Chapter 17. Runtime Features
    1. 17.1 site: Site-Wide Configuration
    2. 17.2 sys: System-Specific Configuration
    3. 17.3 os: Portable Access to Operating System–Specific Features
    4. 17.4 platform: System Version Information
    5. 17.5 resource: System Resource Management
    6. 17.6 gc: Garbage Collector
    7. 17.7 sysconfig: Interpreter Compile-Time Configuration
  28. Chapter 18. Language Tools
    1. 18.1 warnings: Non-fatal Alerts
    2. 18.2 abc: Abstract Base Classes
    3. 18.3 dis: Python Byte-Code Disassembler
    4. 18.4 inspect: Inspect Live Objects
  29. Chapter 19. Modules and Packages
    1. 19.1 importlib: Python’s Import Mechanism
    2. 19.2 pkgutil: Package Utilities
    3. 19.3 zipimport: Load Python Code from ZIP Archives
  30. Appendix A. Porting Notes
    1. A.1 References
    2. A.2 New Modules
    3. A.3 Renamed Modules
    4. A.4 Removed Modules
    5. A.5 Deprecated Modules
    6. A.6 Summary of Changes to Modules
  31. Appendix B. Outside of the Standard Library
    1. B.1 Text
    2. B.2 Algorithms
    3. B.3 Dates and Times
    4. B.4 Mathematics
    5. B.5 Data Persistence and Exchange
    6. B.6 Cryptography
    7. B.7 Concurrency with Processes, Threads, and Coroutines
    8. B.8 The Internet
    9. B.9 Email
    10. B.10 Application Building Blocks
    11. B.11 Developer Tools
  32. Index of Python Modules
  33. Index
  34. Code Snippets