Programming Python, 4th Edition

Book description

If you've mastered Python's fundamentals, you're ready to start using it to get real work done. Programming Python will show you how, with in-depth tutorials on the language's primary application domains: system administration, GUIs, and the Web. You'll also explore how Python is used in databases, networking, front-end scripting layers, text processing, and more. This book focuses on commonly used tools and libraries to give you a comprehensive understanding of Python’s many roles in practical, real-world programming.

You'll learn language syntax and programming techniques in a clear and concise manner, with lots of examples that illustrate both correct usage and common idioms. Completely updated for version 3.x, Programming Python also delves into the language as a software development tool, with many code examples scaled specifically for that purpose.

Topics include:

  • Quick Python tour: Build a simple demo that includes data representation, object-oriented programming, object persistence, GUIs, and website basics
  • System programming: Explore system interface tools and techniques for command-line scripting, processing files and folders, running programs in parallel, and more
  • GUI programming: Learn to use Python’s tkinter widget library
  • Internet programming: Access client-side network protocols and email tools, use CGI scripts, and learn website implementation techniques
  • More ways to apply Python: Implement data structures, parse text-based information, interface with databases, and extend and embed Python

Publisher resources

View/Submit Errata

Table of contents

  1. A Note Regarding Supplemental Files
  2. Preface
    1. “And Now for Something Completely Different…”
    2. About This Book
      1. This Book’s Ecosystem
      2. What This Book Is Not
    3. About This Fourth Edition
      1. Specific Changes in This Edition
    4. What’s Left, Then?
    5. Python 3.X Impacts on This Book
      1. Specific 3.X Changes
      2. Language Versus Library: Unicode
      3. Python 3.1 Limitations: Email, CGI
    6. Using Book Examples
      1. Where to Look for Examples and Updates
      2. Example Portability
      3. Demo Launchers
      4. Code Reuse Policies
    7. Contacting O’Reilly
    8. Conventions Used in This Book
    9. Acknowledgments
  3. I. The Beginning
    1. 1. A Sneak Preview
      1. “Programming Python: The Short Story”
      2. The Task
      3. Step 1: Representing Records
        1. Using Lists
          1. Start-up pointers
          2. A database list
          3. Field labels
        2. Using Dictionaries
          1. Other ways to make dictionaries
          2. Lists of dictionaries
          3. Nested structures
          4. Dictionaries of dictionaries
      4. Step 2: Storing Records Persistently
        1. Using Formatted Files
          1. Test data script
          2. File name conventions
          3. Script start-up pointers
          4. Data format script
          5. Utility scripts
        2. Using Pickle Files
        3. Using Per-Record Pickle Files
        4. Using Shelves
      5. Step 3: Stepping Up to OOP
        1. Using Classes
        2. Adding Behavior
        3. Adding Inheritance
        4. Refactoring Code
          1. Augmenting methods
          2. Display format
          3. Constructor customization
          4. Alternative classes
        5. Adding Persistence
        6. Other Database Options
      6. Step 4: Adding Console Interaction
        1. A Console Shelve Interface
      7. Step 5: Adding a GUI
        1. GUI Basics
        2. Using OOP for GUIs
        3. Getting Input from a User
        4. A GUI Shelve Interface
          1. Coding the GUI
          2. Using the GUI
          3. Future directions
      8. Step 6: Adding a Web Interface
        1. CGI Basics
          1. GUIs versus the Web
        2. Running a Web Server
        3. Using Query Strings and urllib
        4. Formatting Reply Text
        5. A Web-Based Shelve Interface
          1. Coding the website
          2. Directories, string formatting, and security
          3. Using the website
          4. Future directions
      9. The End of the Demo
  4. II. System Programming
    1. 2. System Tools
      1. “The os.path to Knowledge”
        1. Why Python Here?
        2. The Next Five Chapters
      2. System Scripting Overview
        1. Python System Modules
        2. Module Documentation Sources
        3. Paging Documentation Strings
        4. A Custom Paging Script
        5. String Method Basics
        6. Other String Concepts in Python 3.X: Unicode and bytes
        7. File Operation Basics
        8. Using Programs in Two Ways
        9. Python Library Manuals
        10. Commercially Published References
      3. Introducing the sys Module
        1. Platforms and Versions
        2. The Module Search Path
        3. The Loaded Modules Table
        4. Exception Details
        5. Other sys Module Exports
      4. Introducing the os Module
        1. Tools in the os Module
        2. Administrative Tools
        3. Portability Constants
        4. Common os.path Tools
        5. Running Shell Commands from Scripts
          1. What’s a shell command?
          2. Running shell commands
          3. Communicating with shell commands
          4. The subprocess module alternative
          5. Shell command limitations
        6. Other os Module Exports
    2. 3. Script Execution Context
      1. “I’d Like to Have an Argument, Please”
      2. Current Working Directory
        1. CWD, Files, and Import Paths
        2. CWD and Command Lines
      3. Command-Line Arguments
        1. Parsing Command-Line Arguments
      4. Shell Environment Variables
        1. Fetching Shell Variables
        2. Changing Shell Variables
        3. Shell Variable Fine Points: Parents, putenv, and getenv
      5. Standard Streams
        1. Redirecting Streams to Files and Programs
          1. Chaining programs with pipes
          2. Coding alternatives for adders and sorters
        2. Redirected Streams and User Interaction
        3. Redirecting Streams to Python Objects
        4. The io.StringIO and io.BytesIO Utility Classes
        5. Capturing the stderr Stream
        6. Redirection Syntax in Print Calls
        7. Other Redirection Options: os.popen and subprocess Revisited
          1. Redirecting input or output with os.popen
          2. Redirecting input and output with subprocess
    3. 4. File and Directory Tools
      1. “Erase Your Hard Drive in Five Easy Steps!”
      2. File Tools
        1. The File Object Model in Python 3.X
        2. Using Built-in File Objects
          1. Output files
            1. Opening
            2. Writing
            3. Closing
          2. Ensuring file closure: Exception handlers and context managers
          3. Input files
          4. Reading lines with file iterators
          5. Other open options
        3. Binary and Text Files
          1. Unicode encodings for text files
          2. End-of-line translations for text files
          3. Parsing packed binary data with the struct module
          4. Random access files
        4. Lower-Level File Tools in the os Module
          1. Using os.open files
          2. os.open mode flags
          3. Wrapping descriptors in file objects
          4. Other os module file tools
        5. File Scanners
          1. File filters
      3. Directory Tools
        1. Walking One Directory
          1. Running shell listing commands with os.popen
          2. The glob module
          3. The os.listdir call
          4. Splitting and joining listing results
        2. Walking Directory Trees
          1. The os.walk visitor
          2. Recursive os.listdir traversals
        3. Handling Unicode Filenames in 3.X: listdir, walk, glob
          1. Unicode policies: File content versus file names
    4. 5. Parallel System Tools
      1. “Telling the Monkeys What to Do”
      2. Forking Processes
        1. The fork/exec Combination
          1. os.exec call formats
          2. Spawned child program
      3. Threads
        1. The _thread Module
          1. Basic usage
          2. Other ways to code threads with _thread
          3. Running multiple threads
          4. Synchronizing access to shared objects and names
          5. Waiting for spawned thread exits
          6. Coding alternatives: busy loops, arguments, and context managers
        2. The threading Module
          1. Other ways to code threads with threading
          2. Synchronizing access to shared objects and names revisited
        3. The queue Module
          1. Arguments versus globals
          2. Program exit with child threads
          3. Running the script
        4. Preview: GUIs and Threads
        5. More on the Global Interpreter Lock
          1. The thread switch interval
          2. Atomic operations
          3. C API thread considerations
          4. A process-based alternative: multiprocessing (ahead)
      4. Program Exits
        1. sys Module Exits
        2. os Module Exits
        3. Shell Command Exit Status Codes
          1. Exit status with os.system and os.popen
          2. Output stream buffering: A first look
          3. Exit status with subprocess
        4. Process Exit Status and Shared State
        5. Thread Exits and Shared State
      5. Interprocess Communication
        1. Anonymous Pipes
          1. Anonymous pipe basics
          2. Wrapping pipe descriptors in file objects
          3. Anonymous pipes and threads
          4. Bidirectional IPC with anonymous pipes
          5. Output stream buffering revisited: Deadlocks and flushes
        2. Named Pipes (Fifos)
          1. Named pipe basics
          2. Named pipe use cases
        3. Sockets: A First Look
          1. Socket basics
          2. Sockets and independent programs
          3. Socket use cases
        4. Signals
      6. The multiprocessing Module
        1. Why multiprocessing?
        2. The Basics: Processes and Locks
          1. Implementation and usage rules
        3. IPC Tools: Pipes, Shared Memory, and Queues
          1. multiprocessing pipes
          2. Shared memory and globals
          3. Queues and subclassing
        4. Starting Independent Programs
        5. And Much More
          1. And a little less…
        6. Why multiprocessing? The Conclusion
      7. Other Ways to Start Programs
        1. The os.spawn Calls
        2. The os.startfile call on Windows
          1. Using the DOS start command
          2. Using start in Python scripts
          3. The os.startfile call
      8. A Portable Program-Launch Framework
      9. Other System Tools Coverage
    5. 6. Complete System Programs
      1. “The Greps of Wrath”
      2. A Quick Game of “Find the Biggest Python File”
        1. Scanning the Standard Library Directory
        2. Scanning the Standard Library Tree
        3. Scanning the Module Search Path
        4. Scanning the Entire Machine
        5. Printing Unicode Filenames
      3. Splitting and Joining Files
        1. Splitting Files Portably
        2. Joining Files Portably
        3. Usage Variations
      4. Generating Redirection Web Pages
        1. Page Template File
        2. Page Generator Script
      5. A Regression Test Script
        1. Running the Test Driver
      6. Copying Directory Trees
      7. Comparing Directory Trees
        1. Finding Directory Differences
        2. Finding Tree Differences
        3. Running the Script
        4. Verifying Backups
        5. Reporting Differences and Other Ideas
      8. Searching Directory Trees
        1. Greps and Globs and Finds
        2. Rolling Your Own find Module
          1. The fnmatch module
        3. Cleaning Up Bytecode Files
        4. A Python Tree Searcher
      9. Visitor: Walking Directories “++”
        1. Editing Files in Directory Trees (Visitor)
        2. Global Replacements in Directory Trees (Visitor)
        3. Counting Source Code Lines (Visitor)
        4. Recoding Copies with Classes (Visitor)
        5. Other Visitor Examples (External)
      10. Playing Media Files
        1. The Python webbrowser Module
        2. The Python mimetypes Module
          1. Using mimetypes guesses for SearchVisitor
        3. Running the Script
      11. Automated Program Launchers (External)
  5. III. GUI Programming
    1. 7. Graphical User Interfaces
      1. “Here’s Looking at You, Kid”
        1. GUI Programming Topics
        2. Running the Examples
      2. Python GUI Development Options
      3. tkinter Overview
        1. tkinter Pragmatics
        2. tkinter Documentation
        3. tkinter Extensions
        4. tkinter Structure
          1. Implementation structure
          2. Programming structure
      4. Climbing the GUI Learning Curve
        1. “Hello World” in Four Lines (or Less)
        2. tkinter Coding Basics
        3. Making Widgets
        4. Geometry Managers
        5. Running GUI Programs
          1. Avoiding DOS consoles on Windows
      5. tkinter Coding Alternatives
        1. Widget Resizing Basics
        2. Configuring Widget Options and Window Titles
        3. One More for Old Times’ Sake
        4. Packing Widgets Without Saving Them
      6. Adding Buttons and Callbacks
        1. Widget Resizing Revisited: Expansion
      7. Adding User-Defined Callback Handlers
        1. Lambda Callback Handlers
        2. Deferring Calls with Lambdas and Object References
        3. Callback Scope Issues
          1. Arguments versus globals
          2. Passing in enclosing scope values with default arguments
          3. Passing in enclosing scope values with automatic references
          4. But you must still sometimes use defaults instead of enclosing scopes
        4. Bound Method Callback Handlers
        5. Callable Class Object Callback Handlers
        6. Other tkinter Callback Protocols
        7. Binding Events
      8. Adding Multiple Widgets
        1. Widget Resizing Revisited: Clipping
        2. Attaching Widgets to Frames
        3. Layout: Packing Order and Side Attachments
        4. The Packer’s Expand and Fill Revisited
        5. Using Anchor to Position Instead of Stretch
      9. Customizing Widgets with Classes
        1. Standardizing Behavior and Appearance
          1. Common behavior
          2. Common appearance
      10. Reusable GUI Components with Classes
        1. Attaching Class Components
        2. Extending Class Components
        3. Standalone Container Classes
      11. The End of the Tutorial
      12. Python/tkinter for Tcl/Tk Converts
    2. 8. A tkinter Tour, Part 1
      1. “Widgets and Gadgets and GUIs, Oh My!”
        1. This Chapter’s Topics
      2. Configuring Widget Appearance
      3. Top-Level Windows
        1. Toplevel and Tk Widgets
        2. Top-Level Window Protocols
      4. Dialogs
        1. Standard (Common) Dialogs
          1. A “smart” and reusable Quit button
          2. A dialog demo launcher bar
          3. Printing dialog results and passing callback data with lambdas
          4. Letting users select colors on the fly
          5. Other standard dialog calls
        2. The Old-Style Dialog Module
        3. Custom Dialogs
          1. Making custom dialogs modal
          2. Other ways to be modal
      5. Binding Events
        1. Other bind Events
          1. More on <Destroy> events and the quit and destroy methods
      6. Message and Entry
        1. Message
        2. Entry
          1. Programming Entry widgets
        3. Laying Out Input Forms
          1. Going modal again
        4. tkinter “Variables” and Form Layout Alternatives
      7. Checkbutton, Radiobutton, and Scale
        1. Checkbuttons
          1. Check buttons and variables
        2. Radio Buttons
          1. Radio buttons and variables
          2. Radio buttons without variables
          3. Hold onto your variables!
        3. Scales (Sliders)
          1. Scales and variables
      8. Running GUI Code Three Ways
        1. Attaching Frames
          1. Importing by name string
          2. Configuring at construction time
        2. Independent Windows
        3. Running Programs
          1. Launching GUIs as programs other ways: multiprocessing
          2. Cross-program communication
          3. Coding for reusability
      9. Images
        1. Fun with Buttons and Pictures
      10. Viewing and Processing Images with PIL
        1. PIL Basics
        2. Displaying Other Image Types with PIL
          1. Displaying all images in a directory
        3. Creating Image Thumbnails with PIL
          1. Performance: Saving thumbnail files
          2. Layout options: Gridding
          3. Layout options: Fixed-size buttons
          4. Scrolling and canvases (ahead)
    3. 9. A tkinter Tour, Part 2
      1. “On Today’s Menu: Spam, Spam, and Spam”
      2. Menus
        1. Top-Level Window Menus
        2. Frame- and Menubutton-Based Menus
          1. Using Menubuttons and Optionmenus
        3. Windows with Both Menus and Toolbars
          1. Using images in toolbars, too
          2. Automating menu construction
      3. Listboxes and Scrollbars
        1. Programming Listboxes
        2. Programming Scroll Bars
        3. Packing Scroll Bars
      4. Text
        1. Programming the Text Widget
          1. Text is a Python string
          2. String positions
            1. Text indexes
            2. Text marks
            3. Text tags
        2. Adding Text-Editing Operations
          1. Using the clipboard
          2. Composition versus inheritance
          3. It’s called “Simple” for a reason: PyEdit (ahead)
        3. Unicode and the Text Widget
          1. String types in the Text widget
          2. Unicode text in strings
          3. Unicode text in files
          4. Unicode and the Text widget
          5. The problem with treating text as bytes
          6. Other binary mode considerations
          7. Supporting Unicode in PyEdit (ahead)
        4. Advanced Text and Tag Operations
      5. Canvas
        1. Basic Canvas Operations
        2. Programming the Canvas Widget
          1. Coordinates
          2. Object construction
          3. Object identifiers and operations
          4. Canvas object tags
        3. Scrolling Canvases
        4. Scrollable Canvases and Image Thumbnails
          1. Scrolling images too: PyPhoto (ahead)
        5. Using Canvas Events
          1. Binding events on specific items
      6. Grids
        1. Why Grids?
        2. Grid Basics: Input Forms Revisited
        3. Comparing grid and pack
        4. Combining grid and pack
        5. Making Gridded Widgets Expandable
          1. Resizing in grids
          2. Spanning columns and rows
        6. Laying Out Larger Tables with grid
      7. Time Tools, Threads, and Animation
        1. Using Threads with tkinter GUIs
        2. Using the after Method
          1. Hiding and redrawing widgets and windows
        3. Simple Animation Techniques
          1. Using time.sleep loops
          2. Using widget.after events
          3. Using multiple time.sleep loop threads
        4. Other Animation Topics
          1. Other animation effects
          2. Threads and animation
          3. Graphics and gaming toolkits
      8. The End of the Tour
        1. Other Widgets and Options
    4. 10. GUI Coding Techniques
      1. “Building a Better Mousetrap”
      2. GuiMixin: Common Tool Mixin Classes
        1. Widget Builder Functions
        2. Mixin Utility Classes
      3. GuiMaker: Automating Menus and Toolbars
        1. Subclass Protocols
        2. GuiMaker Classes
        3. GuiMaker Self-Test
        4. BigGui: A Client Demo Program
      4. ShellGui: GUIs for Command-Line Tools
        1. A Generic Shell-Tools Display
        2. Application-Specific Tool Set Classes
        3. Adding GUI Frontends to Command Lines
          1. Non-GUI scripts
          2. GUI input dialogs
          3. Room for improvement
      5. GuiStreams: Redirecting Streams to Widgets
        1. Using Redirection for the Packing Scripts
      6. Reloading Callback Handlers Dynamically
      7. Wrapping Up Top-Level Window Interfaces
      8. GUIs, Threads, and Queues
        1. Placing Data on Queues
          1. Recoding with classes and bound methods
          2. Thread exits in GUIs
        2. Placing Callbacks on Queues
          1. Passing bound method callbacks on queues
      9. More Ways to Add GUIs to Non-GUI Code
        1. Popping Up GUI Windows on Demand
        2. Adding a GUI As a Separate Program: Sockets (A Second Look)
        3. Adding a GUI As a Separate Program: Command Pipes
          1. The specter of blocking input calls
          2. Updating GUIs within threads…and other nonsolutions
          3. Avoiding blocking input calls with non-GUI threads
          4. Sockets and pipes: Compare and contrast
          5. Other uses for threaded pipe GUIs
      10. The PyDemos and PyGadgets Launchers
        1. PyDemos Launcher Bar (Mostly External)
        2. PyGadgets Launcher Bar
    5. 11. Complete GUI Programs
      1. “Python, Open Source, and Camaros”
        1. Examples in Other Chapters
        2. This Chapter’s Strategy
      2. PyEdit: A Text Editor Program/Object
        1. Running PyEdit
          1. Menus and toolbars
          2. Dialogs
          3. Running program code
          4. Multiple windows
          5. Other PyEdit examples and screenshots in this book
        2. PyEdit Changes in Version 2.0 (Third Edition)
          1. Font dialog
          2. Undo, redo, and modified tests
          3. Configuration module
        3. PyEdit Changes in Version 2.1 (Fourth Edition)
          1. Modal dialog state fix
          2. Cross-process change tests on Quit
          3. New Grep dialog: Threaded and Unicode-aware file tree search
            1. Grep threading model
            2. Grep Unicode model
          4. Update for initial positioning
          5. Improvements for running code
          6. Unicode (Internationalized) text support
            1. Unicode file and display model
            2. Unicode options and choices
          7. More on Quit checks: The <Destroy> event revisited
        4. PyEdit Source Code
          1. User configurations file
          2. Windows (and other) launch files
          3. Main implementation file
      3. PyPhoto: An Image Viewer and Resizer
        1. Running PyPhoto
        2. PyPhoto Source Code
      4. PyView: An Image and Notes Slideshow
        1. Running PyView
          1. Embedding PyEdit in PyView
        2. PyView Source Code
      5. PyDraw: Painting and Moving Graphics
        1. Running PyDraw
        2. PyDraw Source Code
      6. PyClock: An Analog/Digital Clock Widget
        1. A Quick Geometry Lesson
        2. Running PyClock
        3. PyClock Source Code
      7. PyToe: A Tic-Tac-Toe Game Widget
        1. Running PyToe
        2. PyToe Source Code (External)
      8. Where to Go from Here
  6. IV. Internet Programming
    1. 12. Network Scripting
      1. “Tune In, Log On, and Drop Out”
        1. Internet Scripting Topics
          1. What we will cover
          2. What we won’t cover
          3. Other themes in this part of the book
        2. Running Examples in This Part of the Book
      2. Python Internet Development Options
      3. Plumbing the Internet
        1. The Socket Layer
          1. Machine identifiers
        2. The Protocol Layer
          1. Port number rules
          2. Clients and servers
          3. Protocol structures
        3. Python’s Internet Library Modules
      4. Socket Programming
        1. Socket Basics
          1. Server socket calls
          2. Transferring byte strings and objects
          3. Client socket calls
        2. Running Socket Programs Locally
        3. Running Socket Programs Remotely
          1. Socket pragmatics
        4. Spawning Clients in Parallel
          1. Preview: Denied client connections
        5. Talking to Reserved Ports
          1. Binding reserved port servers
      5. Handling Multiple Clients
        1. Forking Servers
          1. Running the forking server
          2. Other run modes: Local servers with Cygwin and remote clients
          3. Forked processes and sockets
          4. Exiting from children
          5. Killing the zombies: Don’t fear the reaper!
          6. Preventing zombies with signal handlers on Linux
          7. Why multiprocessing doesn’t help with socket server portability
        2. Threading Servers
        3. Standard Library Server Classes
        4. Multiplexing Servers with select
          1. A select-based echo server
          2. Running the select server
        5. Summary: Choosing a Server Scheme
      6. Making Sockets Look Like Files and Streams
        1. A Stream Redirection Utility
          1. Text-mode files and buffered output streams
          2. Stream requirements
          3. Line buffering
          4. Solutions
          5. Buffering in other contexts: Command pipes revisited
          6. Sockets versus command pipes
      7. A Simple Python File Server
        1. Running the File Server and Clients
        2. Adding a User-Interface Frontend
          1. Using row frames and command lines
          2. Using grids and function calls
          3. Using a reusable form-layout class
    2. 13. Client-Side Scripting
      1. “Socket to Me!”
      2. FTP: Transferring Files over the Net
      3. Transferring Files with ftplib
        1. Using urllib to Download Files
        2. FTP get and put Utilities
          1. Download utility
          2. Upload utility
          3. Playing the Monty Python theme song
        3. Adding a User Interface
      4. Transferring Directories with ftplib
        1. Downloading Site Directories
        2. Uploading Site Directories
        3. Refactoring Uploads and Downloads for Reuse
          1. Refactoring with functions
          2. Refactoring with classes
      5. Transferring Directory Trees with ftplib
        1. Uploading Local Trees
        2. Deleting Remote Trees
        3. Downloading Remote Trees
      6. Processing Internet Email
        1. Unicode in Python 3.X and Email Tools
      7. POP: Fetching Email
        1. Mail Configuration Module
        2. POP Mail Reader Script
        3. Fetching Messages
        4. Fetching Email at the Interactive Prompt
      8. SMTP: Sending Email
        1. SMTP Mail Sender Script
        2. Sending Messages
          1. Verifying receipt
          2. Manipulating both From and To
        3. Sending Email at the Interactive Prompt
      9. email: Parsing and Composing Mail Content
        1. Message Objects
        2. Basic email Package Interfaces in Action
          1. Handling multipart messages
        3. Unicode, Internationalization, and the Python 3.1 email Package
          1. Parser decoding requirement
          2. Text payload encodings: Handling mixed type results
          3. Text payload encodings: Using header information to decode
          4. Message header encodings: email package support
          5. Message address header encodings and parsing, and header creation
          6. Workaround: Message text generation for binary attachment payloads is broken
          7. Workaround: Message composition for non-ASCII text parts is broken
          8. Summary: Solutions and workarounds
      10. A Console-Based Email Client
        1. Running the pymail Console Client
      11. The mailtools Utility Package
        1. Initialization File
        2. MailTool Class
        3. MailSender Class
          1. Unicode issues for attachments, save files, and headers
        4. MailFetcher Class
          1. General usage
          2. Unicode decoding for full mail text on fetches
          3. Inbox synchronization tools
        5. MailParser Class
          1. Unicode decoding for text part payloads and message headers
        6. Self-Test Script
          1. Running the self-test
        7. Updating the pymail Console Client
          1. Running the pymail2 console client
      12. NNTP: Accessing Newsgroups
      13. HTTP: Accessing Websites
      14. The urllib Package Revisited
        1. Other urllib Interfaces
          1. Invoking programs and escaping text
      15. Other Client-Side Scripting Options
    3. 14. The PyMailGUI Client
      1. “Use the Source, Luke”
        1. Source Code Modules and Size
          1. Code size
          2. Code Structure
        2. Why PyMailGUI?
        3. Running PyMailGUI
        4. Presentation Strategy
      2. Major PyMailGUI Changes
        1. New in Version 2.1 and 2.0 (Third Edition)
        2. New in Version 3.0 (Fourth Edition)
          1. Version 3.0 Unicode support policies
      3. A PyMailGUI Demo
        1. Getting Started
        2. Loading Mail
        3. Threading Model
          1. Threading model implementation
        4. Load Server Interface
        5. Offline Processing with Save and Open
        6. Sending Email and Attachments
        7. Viewing Email and Attachments
        8. Email Replies and Forwards and Recipient Options
        9. Deleting Email
        10. POP Message Numbers and Synchronization
        11. Handling HTML Content in Email
        12. Mail Content Internationalization Support
        13. Alternative Configurations and Accounts
        14. Multiple Windows and Status Messages
      4. PyMailGUI Implementation
        1. PyMailGUI: The Main Module
        2. SharedNames: Program-Wide Globals
        3. ListWindows: Message List Windows
        4. ViewWindows: Message View Windows
        5. messagecache: Message Cache Manager
        6. popuputil: General-Purpose GUI Pop Ups
        7. wraplines: Line Split Tools
        8. html2text: Extracting Text from HTML (Prototype, Preview)
        9. mailconfig: User Configurations
        10. textConfig: Customizing Pop-Up PyEdit Windows
        11. PyMailGUIHelp: User Help Text and Display
        12. altconfigs: Configuring for Multiple Accounts
      5. Ideas for Improvement
    4. 15. Server-Side Scripting
      1. “Oh, What a Tangled Web We Weave”
      2. What’s a Server-Side CGI Script?
        1. The Script Behind the Curtain
        2. Writing CGI Scripts in Python
      3. Running Server-Side Examples
        1. Web Server Options
        2. Running a Local Web Server
        3. The Server-Side Examples Root Page
        4. Viewing Server-Side Examples and Output
      4. Climbing the CGI Learning Curve
        1. A First Web Page
          1. HTML basics
          2. Internet addresses (URLs)
          3. Using minimal URLs
          4. HTML file permission constraints
        2. A First CGI Script
          1. Installing CGI scripts
          2. Finding Python on remote servers
        3. Adding Pictures and Generating Tables
          1. Table tags
        4. Adding User Interaction
          1. Submission page
          2. More on form tags
          3. Response script
          4. Passing parameters in URLs
          5. Testing outside browsers with the module urllib.request
        5. Using Tables to Lay Out Forms
          1. Converting strings in CGI scripts
          2. Debugging CGI scripts
        6. Adding Common Input Devices
        7. Changing Input Layouts
          1. Keeping display and logic separate
        8. Passing Parameters in Hardcoded URLs
        9. Passing Parameters in Hidden Form Fields
      5. Saving State Information in CGI Scripts
        1. URL Query Parameters
        2. Hidden Form Input Fields
        3. HTTP “Cookies”
          1. Creating a cookie
          2. Receiving a cookie
          3. Using cookies in CGI scripts
          4. Handling cookies with the urllib.request module
        4. Server-Side Databases
        5. Extensions to the CGI Model
        6. Combining Techniques
      6. The Hello World Selector
        1. Checking for Missing and Invalid Inputs
      7. Refactoring Code for Maintainability
        1. Step 1: Sharing Objects Between Pages—A New Input Form
        2. Step 2: A Reusable Form Mock-Up Utility
        3. Step 3: Putting It All Together—A New Reply Script
      8. More on HTML and URL Escapes
        1. URL Escape Code Conventions
        2. Python HTML and URL Escape Tools
        3. Escaping HTML Code
        4. Escaping URLs
        5. Escaping URLs Embedded in HTML Code
          1. HTML and URL conflicts: &
          2. Avoiding conflicts
      9. Transferring Files to Clients and Servers
        1. Displaying Arbitrary Server Files on the Client
          1. Handling private files and errors
        2. Uploading Client Files to the Server
          1. Handling client path formats
        3. More Than One Way to Push Bits over the Net
    5. 16. The PyMailCGI Server
      1. “Things to Do When Visiting Chicago”
      2. The PyMailCGI Website
        1. Implementation Overview
        2. New in This Fourth Edition (Version 3.0)
        3. New in the Prior Edition (Version 2.0)
        4. Presentation Overview
        5. Running This Chapter’s Examples
      3. The Root Page
        1. Configuring PyMailCGI
      4. Sending Mail by SMTP
        1. The Message Composition Page
        2. The Send Mail Script
        3. Error Pages
        4. Common Look-and-Feel
        5. Using the Send Mail Script Outside a Browser
      5. Reading POP Email
        1. The POP Password Page
        2. The Mail Selection List Page
        3. Passing State Information in URL Link Parameters
        4. Security Protocols
          1. Reading mail with direct URLs
        5. The Message View Page
        6. Passing State Information in HTML Hidden Input Fields
        7. Escaping Mail Text and Passwords in HTML
      6. Processing Fetched Mail
        1. Reply and Forward
        2. Delete
        3. Deletions and POP Message Numbers
          1. Inbox synchronization error potential
          2. Alternative: Passing header text in hidden input fields (PyMailCGI_2.1)
          3. Alternative: Server-side files for headers
          4. Alternative: Delete on load
      7. Utility Modules
        1. External Components and Configuration
        2. POP Mail Interface
        3. POP Password Encryption
          1. Manual data encryption: rotor (defunct)
          2. Manual data encryption: PyCrypto
          3. HTTPS: Secure HTTP transmissions
          4. Secure cookies
          5. The secret.py module
          6. Rolling your own encryptor
        4. Common Utilities Module
      8. Web Scripting Trade-Offs
        1. PyMailCGI Versus PyMailGUI
        2. The Web Versus the Desktop
        3. Other Approaches
  7. V. Tools and Techniques
    1. 17. Databases and Persistence
      1. “Give Me an Order of Persistence, but Hold the Pickles”
      2. Persistence Options in Python
      3. DBM Files
        1. Using DBM Files
        2. DBM Details: Files, Portability, and Close
      4. Pickled Objects
        1. Using Object Pickling
        2. Pickling in Action
        3. Pickle Details: Protocols, Binary Modes, and _pickle
      5. Shelve Files
        1. Using Shelves
        2. Storing Built-in Object Types in Shelves
        3. Storing Class Instances in Shelves
        4. Changing Classes of Objects Stored in Shelves
        5. Shelve Constraints
          1. Keys must be strings (and str)
          2. Objects are unique only within a key
          3. Updates must treat shelves as fetch-modify-store mappings
          4. Concurrent updates are not directly supported
          5. Underlying DBM format portability
        6. Pickled Class Constraints
        7. Other Shelve Limitations
      6. The ZODB Object-Oriented Database
        1. The Mostly Missing ZODB Tutorial
      7. SQL Database Interfaces
        1. SQL Interface Overview
        2. An SQL Database API Tutorial with SQLite
          1. Getting started
          2. Making databases and tables
          3. Adding records
          4. Running queries
          5. Running updates
        3. Building Record Dictionaries
          1. Using table descriptions
          2. Record dictionaries construction
          3. Automating with scripts and modules
        4. Tying the Pieces Together
        5. Loading Database Tables from Files
          1. Loading with SQL and Python
          2. Python versus SQL
        6. SQL Utility Scripts
          1. Table load scripts
          2. Table display script
          3. Using the scripts
        7. SQL Resources
      8. ORMs: Object Relational Mappers
      9. PyForm: A Persistent Object Viewer (External)
    2. 18. Data Structures
      1. “Roses Are Red, Violets Are Blue; Lists Are Mutable, and So Is Set Foo”
      2. Implementing Stacks
        1. Built-in Options
        2. A Stack Module
        3. A Stack Class
        4. Customization: Performance Monitors
        5. Optimization: Tuple Tree Stacks
        6. Optimization: In-Place List Modifications
        7. Timing the Improvements
          1. Results under Python 3.1
          2. More on performance analysis
      3. Implementing Sets
        1. Built-in Options
        2. Set Functions
          1. Supporting multiple operands
        3. Set Classes
        4. Optimization: Moving Sets to Dictionaries
          1. Timing the results under Python 3.1
        5. Adding Relational Algebra to Sets (External)
      4. Subclassing Built-in Types
      5. Binary Search Trees
        1. Built-in Options
        2. Implementing Binary Trees
        3. Trees with Both Keys and Values
      6. Graph Searching
        1. Implementing Graph Search
        2. Moving Graphs to Classes
      7. Permuting Sequences
      8. Reversing and Sorting Sequences
        1. Implementing Reversals
        2. Implementing Sorts
          1. Adding comparison functions
        3. Data Structures Versus Built-ins: The Conclusion
      9. PyTree: A Generic Tree Object Viewer
    3. 19. Text and Language
      1. “See Jack Hack. Hack, Jack, Hack”
      2. Strategies for Processing Text in Python
      3. String Method Utilities
        1. Templating with Replacements and Formats
        2. Parsing with Splits and Joins
        3. Summing Columns in a File
          1. Summing with zips and comprehensions
          2. Summing with dictionaries
        4. Parsing and Unparsing Rule Strings
          1. More on the holmes expert system shell
      4. Regular Expression Pattern Matching
        1. The re Module
        2. First Examples
        3. String Operations Versus Patterns
        4. Using the re Module
          1. Module functions
          2. Compiled pattern objects
          3. Match objects
          4. Regular expression patterns
        5. More Pattern Examples
        6. Scanning C Header Files for Patterns
      5. XML and HTML Parsing
        1. XML Parsing in Action
          1. Regular expression parsing
          2. SAX parsing
          3. DOM parsing
          4. ElementTree parsing
          5. Other XML topics
        2. HTML Parsing in Action
          1. Handling HTML entity references (revisited)
          2. Extracting plain text from HTML (revisited)
      6. Advanced Language Tools
      7. Custom Language Parsers
        1. The Expression Grammar
        2. The Parser’s Code
        3. Adding a Parse Tree Interpreter
        4. Parse Tree Structure
        5. Exploring Parse Trees with the PyTree GUI
        6. Parsers Versus Python
      8. PyCalc: A Calculator Program/Object
        1. A Simple Calculator GUI
          1. Building the GUI
          2. Running code strings
          3. Extending and attaching
        2. PyCalc—A “Real” Calculator GUI
          1. Running PyCalc
          2. Evaluating expressions with stacks
          3. PyCalc source code
          4. Using PyCalc as a component
          5. Adding new buttons in new components
    4. 20. Python/C Integration
      1. “I Am Lost at C”
        1. Extending and Embedding
      2. Extending Python in C: Overview
      3. A Simple C Extension Module
      4. The SWIG Integration Code Generator
        1. A Simple SWIG Example
      5. Wrapping C Environment Calls
        1. Adding Wrapper Classes to Flat Libraries
        2. Wrapping C Environment Calls with SWIG
      6. Wrapping C++ Classes with SWIG
        1. A Simple C++ Extension Class
        2. Wrapping the C++ Class with SWIG
        3. Using the C++ Class in Python
          1. Using the low-level extension module
          2. Subclassing the C++ class in Python
          3. Exploring the wrappers interactively
      7. Other Extending Tools
      8. Embedding Python in C: Overview
        1. The C Embedding API
        2. What Is Embedded Code?
      9. Basic Embedding Techniques
        1. Running Simple Code Strings
          1. Compiling and running
        2. Running Code Strings with Results and Namespaces
        3. Calling Python Objects
        4. Running Strings in Dictionaries
        5. Precompiling Strings to Bytecode
      10. Registering Callback Handler Objects
        1. Registration Implementation
      11. Using Python Classes in C
      12. Other Integration Topics
  8. VI. The End
    1. 21. Conclusion: Python and the Development Cycle
      1. “That’s the End of the Book, Now Here’s the Meaning of Life”
      2. “Something’s Wrong with the Way We Program Computers”
      3. The “Gilligan Factor”
      4. Doing the Right Thing
        1. The Static Language Build Cycle
        2. Artificial Complexities
        3. One Language Does Not Fit All
      5. Enter Python
      6. But What About That Bottleneck?
        1. Python Provides Immediate Turnaround
        2. Python Is “Executable Pseudocode”
        3. Python Is OOP Done Right
        4. Python Fosters Hybrid Applications
      7. On Sinking the Titanic
      8. “So What’s Python?”: The Sequel
      9. In the Final Analysis…
  9. Index
  10. About the Author
  11. Colophon
  12. Copyright

Product information

  • Title: Programming Python, 4th Edition
  • Author(s): Mark Lutz
  • Release date: December 2010
  • Publisher(s): O'Reilly Media, Inc.
  • ISBN: 9780596158101