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
How it all fits together
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
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:
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
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.
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.
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.
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_pythonApache 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.
Test Management/Reporting System
This project makes use of the
unittestmodule 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.
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.
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
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.
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
- About the Author
1. A Python Primer
- 1.1. Getting Started
- 1.2. Lexical Structure
- 1.3. Operators
- 1.4. Expressions and Statements
- 1.5. Iteration and Decision-Making
- 1.6. Functions
- 1.7. Modules
- 1.8. Classes
- 1.9. Summary
I. The Projects
2. Directory/File Snapshot Program
- 2.1. Using the Program
- 2.2. Design
2.3. Code and Code Explanation
- 2.3.1. Snapshot.py
- 2.3.2. snapshothelper.py
- 2.4. Testing
- 2.5. Modifying the Program
- 2.6. Summary
3. DVD Inventory System
3.1. Using the Program
- 3.1.1. Installing MySQL
- 3.1.2. Adding a DVD to the Database
- 3.1.3. Searching the DVD Inventory
- 3.1.4. Modifying a DVD Record
- 3.1.5. Deleting a DVD Record
- 3.1.6. Exporting the List of DVDs to a CSV File
- 3.2. Design
3.3. Code and Code Explanation
- 3.3.1. dvd.py
- 3.3.2. add_dvd.py
- 3.3.3. lookup_dvds.py
- 3.3.4. modify_dvd.py
- 3.3.5. delete_dvd.py
- 3.3.6. csvreport_dvd.py
- 3.4. Testing
- 3.5. Modifying the Program
- 3.6. Summary
- 3.1. Using the Program
4. Web Performance Tester
- 4.1. Using the Program
- 4.2. Design
- 4.3. Code and Code Explanation
- 4.4. Testing
- 4.5. Modifying the Program
- 4.6. Summary
5. Customer Follow-Up System
5.1. Using the Program
- 5.1.1. Preliminaries
- 5.1.2. Running the Program
- 5.2. Design
- 5.3. Code and Code Explanation
- 5.4. Testing
- 5.5. Modifying the Program
- 5.6. Summary
- 5.1. Using the Program
6. Test Management/Reporting System
6.1. Using the Program
- 6.1.1. Running Tests
- 6.1.2. Listing Test Runs
- 6.1.3. Showing Test Results
- 6.1.4. Generating an HTML Test Report
- 6.1.5. Examining the HTML File
- 6.1.6. Displaying Product Help
- 6.2. Design
6.3. Code and Code Explanation
- 6.3.1. test_manager.py
- 6.3.2. test_run.py
- 6.3.3. test_list.py
- 6.3.4. test_results.py
- 6.3.5. test_html.py
- 6.4. Testing
- 6.5. Modifying the Program
- 6.6. Summary
- 6.1. Using the Program
7. Version Management System
- 7.1. Using the Program
- 7.2. Design
- 7.3. Code and Code Explanation
- 7.4. Testing
- 7.5. Modifying the Program
- 7.6. Summary
8. Content Management System
- 8.1. Plone Overview
- 8.2. Installing and Configuring Plone
- 8.3. Design
- 8.4. Navigation
- 8.5. Content Management
- 8.6. User Permissions
- 8.7. Summary
- 2. Directory/File Snapshot Program
II. Advanced Topics
9. Interacting with the Operating System
9.1. Generic Operating System Services
- 9.1.1. The os Module — Generic OS Services
- 9.1.2. The time Module — Format and Manipulate System Time
- 9.1.3. The optparse Module — Parse Command-Line Options
- 9.1.4. The platform Module — Get Platform Information
- 9.1.5. The getpass Module — Generate and Check Passwords
- 9.1.6. Some Other Things You Can Do
9.2. Accessing Windows Services
- 9.2.1. The winreg Module — Manipulate the Windows Registry
- 9.2.2. The winsound Module
- 9.2.3. The win32serviceutil Module — Manage Windows Services
- 9.2.4. The win32net Module — Access Windows Networking Features
- 9.2.5. Some Other Things You Can Do
9.3. Accessing Unix/Linux Services
- 9.3.1. The termios Module — Access Unix-Style TTY Interface
- 9.3.2. The resource Module — Manage Unix System Resources
- 9.3.3. The syslog Module — Access the Unix syslog
- 9.3.4. The commands Module — Run Commands and Get Output
- 9.3.5. Some Other Things You Can Do
- 9.4. Summary
- 9.1. Generic Operating System Services
10. Debugging and Testing
- 10.1. The Python Debugger
10.2. Python Test Frameworks
- 10.2.1. Why We Test
- 10.2.2. Unit Testing
- 10.3. Summary
- 10.4. Final Remarks
- 9. Interacting with the Operating System
- A. Where to Go From Here — Resources That Can Help
- B. Installing Supplemental Programs
- Title: Python® Create-Modify-Reuse
- Release date: July 2008
- Publisher(s): Wrox
- ISBN: 9780470259320
You might also like
40 Algorithms Every Programmer Should Know
Learn algorithms for solving classic computer science problems with this concise guide covering everything from fundamental …
Tiny Python Projects
The projects are tiny, but the rewards are big: each chapter in Tiny Python Projects challenges …
Impractical Python Projects
Impractical Python Projects picks up where the complete beginner books leave off, expanding on existing concepts …
Building Machine Learning Systems with Python - Third Edition
Get more from your data by creating practical machine learning systems with Python Key Features Develop …