Python in Practice
Author's Note (Jan 2018): Much of the material in this
section formed the basis for the book
Python Projects
which I co-authored with Laura Cassell and published by Wrox press.
The book contains expanded explanations and more detailed examples
and projects.
You can buy the book here:
Or search your favourite online store.
In this section of the tutorial we are going to focus on some
of the practical applications to which Python can be put, and the
library modules that help us do that. This will involve learning
about some background technologies such as databases, computer
networks and the world wide web, as well as the basic features of
the operating system that drives your computer. Because this is
supposed to be a programming tutorial I will only cover the bare
bones of these technologies and provide links to other sites
where the curious can find out more.
The topics I have chosen reflect the areas that seem to crop
up most often on the Python tutor mailing list, and therefore
should be closest to the needs of new programmers. If your
particular area of interest is not covered then the final topic
may provide links to suitable sites where you will find what you
need.
Finally, the topics in this section are all based on Python
exclusively. There may be similar capabilities available in
JavaScript and VBScript but the differences are far greater than
the similarities at this level of detail. For example the easiest
way to access the Windows operating system from JavaScript or
VBScript is via the Windows Script Host discussed earlier but
that is completely different in approach to Python's os module.
Comparisons would be meaningless.
The topics
The specific topics I will be covering, along with some idea of
the depth of coverage, are listed below:
- Working with Data
- The need to store and retrieve complex sets of data is one
that most programmers come across at some stage. While Python
provides several methods of storing simple data easily the most
powerful storage mechanism is a full relational database. This
topic will cover the principles behind relational databases and
the Structured Query Language (SQL) language used to
manipulate them. It will conclude with a very simple example of
using such a database from within Python as we extend the address
book introduced away back in the data topic.
- Using the Operating System
- The operating system is the basis of everything we do with
a computer and it is very common that in the middle of a
program we want to do the sort of things we do as a user
everyday. For example, we might want to copy or move files, or
create a folder, or start another program, or print a document.
Fortunately the operating system exposes a programmable
interface as well as a user interface and in this topic we will
look at some of the features available to us, especially in the
areas of traversing file structures and working with the
environment.
- Inter-process communications
- Most beginner programs consist of a single computer process
running in isolation, however as the systems we build get more
powerful it is often better to split the program into separate
parts each running in its own process, often using a technique
known as client-server design. Or sometimes we simply
want to access another program's output. In this module we will
look at the basic principles and then illustrate an example of
each of the two types described including creating a local
client-server version of our address book.
- Network programming
- The internet has connected computers all over the globe,
but how can one computer communicate with another from within a
program? It turns out that there are lots of ways to do this
but here we will consider the most basic mechanism available in
Python, the socket. We conclude with a networked version
of the address book that allows you to run the server process
on a remote computer.
- Writing web clients
- Having mastered basic network programming we now come onto
the most common form of networking today: The World Wide Web.
It turns out that python provides modules that make web
programming easy. In this first topic on the subject we look at
automating basic web tasks such as fetching information from a
web site on a regular basis without resorting to a browser.
- Writing Web Applications
- In this topic we turn from the web user perspective to the
web site creators view. We consider how to write a basic web
application using the most basic and fundamental web application
protocol - CGI.
- Using Web Frameworks
- It turns out that Python is particularly well suited to
building application development frameworks for the web. There
are over a dozen to choose from, ranging from large scale
industrial packages capable of handling large numbers of
transactions to very simple frameworks that are easy to use
but lack the bells and whistles of their siblings. This topic
focuses on Flask which is towards the lower end but
still capable of producing a commercial web site for a
small business. In this topic, after looking at Flask basics,
we build a web front end to the address book database
that we have used throughout this section.
- Concurrent processing
- As programs get more complex it is often the case that the
simple sequential model of execution that we have discussed up
until now is not sufficient. We want to do several things at
once. Python offers several techniques to help in this area
and we consider two of them. We will look at how to use
Python's multiprocessing and threading
modules to build some very simple parallel processing into
a program.