Python® Create-Modify-Reuse

Book description

Python: Create-Modify-Reuse is designed for all levels of Python developers interested in a practical, hands-on way of learning Python development. This book is designed to show you how to use Python (in combination with the raw processing power of your computer) to accomplish real-world tasks in a more efficient way. Don't look for an exhaustive description of the Python language--you won't find it. The book's main purpose is not to thoroughly cover the Python language, but rather to show how you can use Python to create robust, real-world applications.

In this respect, the goal is similar to foreign-language books that identify themselves as "conversational," focusing on the vocabulary and concepts that people will need the most. Likewise, I focus specifically on the Python knowledge needed to accomplish practical, specific tasks. Along the way, you will learn to create useful, efficient scripts that are easy to maintain and enhance.

This book is for developers with some experience with Python who want to explore how to develop full-blown applications. It is also for developers with experience in other languages who want to learn Python by building robust applications. It is well-suited for developers who like to "learn by doing," rather than exploring a language feature by feature. To get the most out of the book, you should understand basic programming principles.

Because this book is project-based, you can approach it in numerous ways. You can, of course, read it from cover to cover. Chapters 2 through 8 each cover a different project, so the chapters are independent of each other. However, because each chapter project is covered individually, there may be some overlap of information. I also sometimes refer to explanations of particular topics covered in previous chapters. This will help to reinforce important concepts.

The end of the book contains two appendixes. The first one is a listing of Python resources you can check out for more information. The second one will help you with installing additional components used in some of the examples.

This book starts with a basic overview of the Python language, designed for those familiar with other languages but new to Python. It is followed by several chapters, each of which describes a complete project that can be used as-is or modified and extended to suit your particular purposes. You'll find applications that access databases, take advantage of web technologies, and facilitate network communications, to name a few. In addition, and more important than the technologies you will be introduced to, you will learn how to use Python to solve real challenges. Following these chapters are two chapters that cover accessing operating system resources and debugging and testing, respectively.

Each project chapter contains complete instructions describing how to install and use the application, so you can actually see the program run as you learn how to construct and use it, including how the project was designed and prototyped. This book is intended to be both a reference guide and a learning aid, teaching you how to build solutions with Python and providing reference information on a wide variety of Python programming concepts.

It is hoped that this book will help you have fun with Python and build useful applications, and—unlike my experience with building a deck—without sore thumbs.

This book is framed around the code itself. This is because developers are typically looking for how to do something; and, as with many activities, you learn how to do something by watching how others do and trying it yourself. If you want to know how a for loop works, you'll find for loops in my code, but that's not the thrust of the book. Instead, this book shows you how to do things: how to build a content management system, how to build a test management system, how to set up a system for tracking customer follow-up, and so on. Along the way, you'll learn how to communicate with a SQL database, how to act as a web server or communicate with one, how to access operating system services, and more.

There are three basic components to the book:

  • Chapter 1 is a brief overview of the Python language.

  • Chapters 2–8 cover seven different programming projects, which illustrate various technologies and techniques available to Python developers.

  • Chapters 9–10 cover additional, advanced topics, which will help you as you build Python projects.

The project chapters have a consistent structure:

  • Overview: What does the application do?

  • Using the program

  • Design

  • How it all fits together

  • Modules involved

  • Code and code explanation

  • Module/class 1 explanation

  • Module/class 2 explanation

  • Minor code file explanation

  • Testing, including suggested tests

  • Modifying the project, including some suggested adaptations

  • Summary

Each project is designed with classes that can be reused and accessed for multiple purposes. This is one of the main benefits of object-oriented programming, so designing for reusability is a main focus of the book. The book contains the following chapters:

  1. Python Basics

    This chapter is a basic primer on the Python language, and it functions as either a quick tutorial for experienced programmers new to Python or a refresher for programmers with Python experience.

    Part I: The Projects

  2. Directory/File Snapshot Program

    This project demonstrates how to interact with files, create and manipulate data structures, and provide user output. It also touches on code design issues to improve code maintainability. Often when installing or uninstalling software, or verifying changes to a file system, it can be valuable to take a "snapshot" of the files and directories, along with their size and last-modified time. The script introduced in this chapter does just that. This chapter also explores how to capture a directory listing into a Python list, and explains how to query this list for particular values.

  3. DVD Inventory System

    This project takes advantage of Python's capability to access and manipulate data in a SQL database. The application enables multiple users to log in to a website that provides access to a DVD inventory database. Permissions are set such that some users can add, modify, or delete entries, whereas other users have read-only access to the data.

  4. Web Performance Tester

    This project shows how to communicate with a Python web server and retrieve information regarding how long it takes to receive requested items from the web server. Although writing Python programs to work on a single computer can be useful, the real power of Python can be seen when it is used to script communication between computers on a network. Most networks contain several web servers. A nice feature of Python is that it can act as a lightweight server for various Internet protocols, such as HTTP (web) and ftp. This application enables you to monitor performance of HTTP traffic on your network.

  5. Customer Follow-Up System

    This project shows how to present a web form to the user and retrieve data from it, how to automatically format and send e-mail through an SMTP server, and how to generate an HTML-formatted report. The task for the second example is to automatically generate a customer comments e-mail message based on information the customer enters in a form. It uses the mod_python Apache module to take the information entered in the HTTP form and then utilizes a Python script on the web server to send that information to an SMTP server for mail delivery.

  6. Test Management/Reporting System

    This project makes use of the unittest module to run tests against an existing application, and creates a framework for reporting test results. Testing is a vital process for developing software. This application enables users to run tests for a given piece of software, to list the previous test runs by date, to show test run results for any previously run tests, and to output the results of any test run as HTML for viewing in a web browser.

  7. Version Management System

    This project connects to a list of servers via telnet, checks the application version of a pre-set application list, and displays its results both as output and to a log file. Often, a system administrator needs to patch systems or ensure that systems have the latest application versions installed. This script is an easy way to accomplish that task. It makes use of Python's capability to emulate a telnet client and log in to remote systems and perform functions on that remote system.

  8. Content Management System

    This project explores Plone, a popular content management system based on Python and Zope (a Python-based application server). Because Python is a very mature language, numerous applications have been built on top of it. A great thing about working with Python-based applications is that you get the benefit of a full-blown application but you can still use Python to configure and customize it.

    Part II: Advanced Topics

  9. Interacting with the Operating System

    When writing scripts "in the real world," often it is critical to be able to access services available through (and particular to) the operating system you happen to be on. For example, suppose you wanted to read or modify the Window Registry? Or you wanted to get the Linux process ID of a particular process that is running? Is such a thing even possible? Definitely—and this chapter shows you how.

  10. Debugging and Testing

    Because I am a software tester myself, testing is a subject that is certainly close to my heart. In...

Table of contents

  1. Copyright
  2. About the Author
  3. Credits
  4. Acknowledgments
  5. Introduction
    1. 0.1. Who This Book Is For
    2. 0.2. What This Book Covers
    3. 0.3. How This Book Is Structured
    4. 0.4. What You Need to Use This Book
    5. 0.5. Source Code
    6. 0.6. Errata
    7. 0.7.
  6. 1. A Python Primer
    1. 1.1. Getting Started
      1. 1.1.1. Obtaining Python and Installing It
      2. 1.1.2. The Python Interpreter
      3. 1.1.3. Your Editing/Execution Environment
    2. 1.2. Lexical Structure
      1. 1.2.1. Keywords
      2. 1.2.2. Lines and Indentation
      3. 1.2.3. Data Types and Identifiers
        1. Data Types
        2. Identifiers
    3. 1.3. Operators
    4. 1.4. Expressions and Statements
      1. 1.4.1. Expressions
        1. Operator Precedence in Expressions
      2. 1.4.2. Statements
        1. Multi-line Statements
    5. 1.5. Iteration and Decision-Making
      1. 1.5.1. Iteration
        1. For Loops
        2. While Loops
        3. Break and Continue
      2. 1.5.2. Decision-Making
        1. The if Statement
    6. 1.6. Functions
      1. 1.6.1.
        1. Defining a Function
        2. Calling a Function
    7. 1.7. Modules
      1. 1.7.1. Importing Modules
        1. Import
        2. Reload
      2. 1.7.2. How Python Finds Modules to Load
        1. The Module Search Path
        2. Exploring sys.path
    8. 1.8. Classes
    9. 1.9. Summary
  7. I. The Projects
    1. 2. Directory/File Snapshot Program
      1. 2.1. Using the Program
        1. 2.1.1. Creating a Snapshot
        2. 2.1.2. Listing Snapshot Files
        3. 2.1.3. Comparing Snapshots
        4. 2.1.4. Viewing Help
        5. 2.1.5. Exiting the Program
        6. 2.1.6. Putting It All Together
          1. Other Applications of the Program
      2. 2.2. Design
        1. 2.2.1. How It All Fits Together
        2. 2.2.2. Main Program
        3. 2.2.3. Modules
          1. snapshot Module
          2. snapshothelper Module
      3. 2.3. Code and Code Explanation
        1. 2.3.1.
          1. Program Header
          2. Import Statements
          3. The Main Program
          4. The menu() Function
        2. 2.3.2.
          1. createSnapshot(directory, filename)
          2. listSnapshot(extension)
          3. compareSnapshots(snapfile1, snapfile2)
          4. showHelp()
          5. invalidChoice()
          6. printList()
      4. 2.4. Testing
      5. 2.5. Modifying the Program
      6. 2.6. Summary
    2. 3. DVD Inventory System
      1. 3.1. Using the Program
        1. 3.1.1. Installing MySQL
        2. 3.1.2. Adding a DVD to the Database
          1. When Problems Occur
        3. 3.1.3. Searching the DVD Inventory
          1. Lookup by Movie Title
          2. Lookup by Star
          3. Lookup by Costar
          4. Lookup by Year Released
          5. Lookup by genre
          6. When Problems Occur
        4. 3.1.4. Modifying a DVD Record
          1. Modify Title
          2. Modify Star
          3. Modify Costar
          4. Modify Year
          5. Modify Genre
          6. When Problems Occur
        5. 3.1.5. Deleting a DVD Record
          1. When Problems Occur
        6. 3.1.6. Exporting the List of DVDs to a CSV File
      2. 3.2. Design
        1. 3.2.1. How It All Fits Together
        2. 3.2.2. Modules
      3. 3.3. Code and Code Explanation
        1. 3.3.1.
          1. The main() Function
          2. Menu()
        2. 3.3.2.
          1. AddDVD()
          2. SQLAddDVD(Title, Star, Costar, Year, Genre)
        3. 3.3.3.
          1. LookupDVD()
          2. SQLLookupDVD(searchby, searchtext)
        4. 3.3.4.
          1. ModifyDVD()
        5. 3.3.5.
          1. DeleteDVD()
          2. SQLDeleteDVD(dvdToDelete)
        6. 3.3.6.
          1. WriteCSV()
      4. 3.4. Testing
      5. 3.5. Modifying the Program
      6. 3.6. Summary
    3. 4. Web Performance Tester
      1. 4.1. Using the Program
        1. 4.1.1. Running the Python Web Server
          1. When Problems Occur
        2. 4.1.2. Running the Performance Profiler Client
          1. Testing Connection Performance to External Websites
          2. Testing Internal Web Server Performance
          3. Displaying the Log File
          4. When Problems Occur
      2. 4.2. Design
        1. 4.2.1. How It All Fits Together
        2. 4.2.2. Modules
      3. 4.3. Code and Code Explanation
        1. 4.3.1.
          1. Main()
          2. RunServer()
        2. 4.3.2.
          1. Main()
          2. Menu()
        3. 4.3.3.
          1. CheckExternalSites(sites)
          2. CheckInternalWebServers(serverlist, port)
          3. Other Support Files
      4. 4.4. Testing
      5. 4.5. Modifying the Program
      6. 4.6. Summary
    4. 5. Customer Follow-Up System
      1. 5.1. Using the Program
        1. 5.1.1. Preliminaries
          1. Installing Apache
          2. Installing mod_python
          3. Configuring Apache for mod_python
          4. Copying Program Files into Their Proper Directories
        2. 5.1.2. Running the Program
          1. Entering Comments at the Website
          2. When Problems Occur
          3. Viewing and Sorting the Log File
      2. 5.2. Design
        1. 5.2.1. How It All Fits Together
        2. 5.2.2. Modules
      3. 5.3. Code and Code Explanation
        1. 5.3.1. form.html
        2. 5.3.2.
          1. The email(req, name, phone, email, comment)
          2. writeCSVLog(name, phone, email, comment)
      4. 5.4. Testing
      5. 5.5. Modifying the Program
      6. 5.6. Summary
    5. 6. Test Management/Reporting System
      1. 6.1. Using the Program
        1. 6.1.1. Running Tests
          1. How Test Results Are Stored
          2. What If Some Tests Fail?
        2. 6.1.2. Listing Test Runs
        3. 6.1.3. Showing Test Results
          1. When Problems Occur
        4. 6.1.4. Generating an HTML Test Report
        5. 6.1.5. Examining the HTML File
          1. When Problems Occur
        6. 6.1.6. Displaying Product Help
      2. 6.2. Design
        1. 6.2.1. Modules
      3. 6.3. Code and Code Explanation
        1. 6.3.1.
          1. Main Program
          2. menu()
        2. 6.3.2.
          1. test_firstname(fname)
          2. test_lastname(lname)
          3. test_prime_number()
          4. run_tests()
          5. testcount()
        3. 6.3.3.
          1. list_tests()
        4. 6.3.4.
          1. show_test_results()
        5. 6.3.5.
          1. test_html_report ()
      4. 6.4. Testing
      5. 6.5. Modifying the Program
      6. 6.6. Summary
        1. 6.6.1. More About the Minidom Module
    6. 7. Version Management System
      1. 7.1. Using the Program
        1. 7.1.1. Setting Up Remote Computers
        2. 7.1.2. Running the Program — Command-Line Syntax
          1. Command-Line Examples
          2. Viewing the CSV Log File
          3. Running Against Several Different Machines in Batch Mode
      2. 7.2. Design
        1. 7.2.1. Modules
      3. 7.3. Code and Code Explanation
        1. 7.3.1.
          1. Main Program
          2. check_arguments()
          3. get_versions()
        2. 7.3.2.
          1. check_java()
          2. check_python()
          3. check_perl()
        3. 7.3.3.
          1. write_csv_log()
      4. 7.4. Testing
      5. 7.5. Modifying the Program
        1. 7.5.1. Security Considerations
      6. 7.6. Summary
    7. 8. Content Management System
      1. 8.1. Plone Overview
        1. 8.1.1. What Is Plone?
      2. 8.2. Installing and Configuring Plone
        1. 8.2.1. Downloading Plone
        2. 8.2.2. Extracting the Plone Install
        3. 8.2.3. Running the Plone Install
        4. 8.2.4. Starting Plone
        5. 8.2.5. Discovering the Admin User Password
        6. 8.2.6. Logging In as the Admin User
        7. 8.2.7. Setting Up the E-mail Server
        8. 8.2.8. Setting Up a User
        9. 8.2.9. Logging In as the Set-Up User
      3. 8.3. Design
      4. 8.4. Navigation
      5. 8.5. Content Management
        1. 8.5.1. Creating a Page
        2. 8.5.2. Creating a Collection
      6. 8.6. User Permissions
      7. 8.7. Summary
  8. II. Advanced Topics
    1. 9. Interacting with the Operating System
      1. 9.1. Generic Operating System Services
        1. 9.1.1. The os Module — Generic OS Services
          1. Examples
            1. Example 1
            2. Example 2
        2. 9.1.2. The time Module — Format and Manipulate System Time
          1. Examples
            1. Example 1
            2. Example 2
        3. 9.1.3. The optparse Module — Parse Command-Line Options
          1. Example
        4. 9.1.4. The platform Module — Get Platform Information
          1. Example
        5. 9.1.5. The getpass Module — Generate and Check Passwords
          1. Example
        6. 9.1.6. Some Other Things You Can Do
      2. 9.2. Accessing Windows Services
        1. 9.2.1. The winreg Module — Manipulate the Windows Registry
          1. Example
        2. 9.2.2. The winsound Module
          1. Example
        3. 9.2.3. The win32serviceutil Module — Manage Windows Services
          1. Example
        4. 9.2.4. The win32net Module — Access Windows Networking Features
          1. Example
        5. 9.2.5. Some Other Things You Can Do
      3. 9.3. Accessing Unix/Linux Services
        1. 9.3.1. The termios Module — Access Unix-Style TTY Interface
          1. Example
        2. 9.3.2. The resource Module — Manage Unix System Resources
          1. Example
        3. 9.3.3. The syslog Module — Access the Unix syslog
          1. Example
        4. 9.3.4. The commands Module — Run Commands and Get Output
          1. Example
        5. 9.3.5. Some Other Things You Can Do
      4. 9.4. Summary
    2. 10. Debugging and Testing
      1. 10.1. The Python Debugger
        1. 10.1.1. Running the Debugger
          1. Importing the pdb Module Directly
          2. Accessing the Python Debugger through IDLE
          3. The Debug Control window
          4. Example
      2. 10.2. Python Test Frameworks
        1. 10.2.1. Why We Test
        2. 10.2.2. Unit Testing
          1. Manual Unit Testing with the Python Interactive Interpreter
          2. unittest — Python's Default Unit Test Framework
            1. Example
            2. Running the Tests
          3. doctest — a Compelling Alternative
            1. Example 1
            2. Example 2
      3. 10.3. Summary
      4. 10.4. Final Remarks
  9. A. Where to Go From Here — Resources That Can Help
    1. A.1. Books
    2. A.2. Websites and Blogs
  10. B. Installing Supplemental Programs
    1. B.1. Installing MySQL
      1. B.1.1. Downloading the MySQL Community Server
      2. B.1.2. Running the Install
      3. B.1.3. Configuring the MySQL Server
    2. B.2. Installing Win32All
      1. B.2.1. Downloading the Win32All Package
      2. B.2.2. Running the Install

Product information

  • Title: Python® Create-Modify-Reuse
  • Author(s):
  • Release date: July 2008
  • Publisher(s): Wrox
  • ISBN: 9780470259320