Earlier in the semester we learned how to use the import
command in python to load in functions from outside python files while using python interactive shell.
Today we are going to learn more about using modules, import
, and layers of abstraction in python.
Let’s say that we are building an application which connects to and uses the (now familiar) movie database. As a reminder, this is the schema of this database:
CREATE TABLE director (
first_name TEXT,
last_name TEXT,
age INT,
director_id INT PRIMARY KEY);
CREATE TABLE movie (
title TEXT,
year INT,
rt_rating INT,
movie_id INT PRIMARY KEY,
director_id INT,
FOREIGN KEY (director_id) REFERENCES director(director_id));
In this application, we want the ability to fetch information about movies and directors. We also want to be able to add information, and ensure that it is saved in the database so it can be retrieved at a later time. Below is a link to an example of such a program:
Download the code and follow along.
Before running this code to try it out, we should insert some data into the movie
and director
tables:
INSERT INTO director VALUES ('Chris', 'Nolan', 46, 1);
INSERT INTO director VALUES ('Tom', 'Hooper', 44, 2);
INSERT INTO director VALUES ('Clint', 'Eastwood', 86, 3);
INSERT INTO director VALUES ('Peter', 'Jackson', 55, 4);
INSERT INTO director VALUES ('Steven', 'Spielberg', 70, 5);
INSERT INTO director VALUES ('Mel', 'Gibson', 61, 6);
INSERT INTO director VALUES ('Kevin', 'Costner', 62, 7);
INSERT INTO movie VALUES ('King Kong', 2005, 84, 1, 4);
INSERT INTO movie VALUES ('Flags of Our Fathers', 2006, 73, 2, 3);
INSERT INTO movie VALUES ('Man of Steel', 2013, 55, 3, 1);
INSERT INTO movie VALUES ('Super 8', 2011, 82, 4, 5);
INSERT INTO movie VALUES ('Open Range', 2003, 79, 5, 7);
INSERT INTO movie VALUES ('The Kings Speech', 2010, 95, 6, 2);
INSERT INTO movie VALUES ('Hacksaw Ridge', 2016, 87, 7, 6);
In class, we will take some time here to examine the source code of pymdb.py
and run it a few times.
If you are following along at home, try this out for yourself.
…
This program works, and seems to serve it’s purpose well However, after taking a closer look at the code, we can see that it is not particularly easy to understand, nor is it “well organized”.
If we think about the structure of this program, and how it interacts with the modules it depends on, we get a organization diagram that looks something like this:
Let’s see if we cant clean up this code a little bit.
When looking through the code of pymdb.py
we can see a lot of SQL-related code interspersed with the core logic of command-handling.
One way we could make this more organized it to extract all of the SQL-related code into a separate module.
Below, I’ve linked to a zip file containing some python files where I’ve done just that.
Take a close look at this code, with organization in mind. Is it easier to understand? Harder? About the same?
Again, representing the organization of the code with a diagram, we get:
Let’s make one more attempt at “organizing” this code even further. Check out the code in the below zip file.
The code follows this model:
One of the big motivations for keeping code organized and well-abstracted is understandability.
Another motivation for separating large programs into smaller components is re-usability.
If the code and functionality of your program is neatly organized into modules, you will find that you can re-use the same module is multiple programs.
This allows a person to avoid re-writing identical code over-and-over again across different programs.
The code linked below uses the same movieconn
module in a different way.
Because we were able to re-use code from an existing module, the program itself was extremely simple to implement. When writing it, we did not need to worry about opening a database connection and constructing/executing SQL queries.
In each phase of the transformation of pymdb.py
, we have been abstracting functionality into smaller components.
In computer science, Abstraction Is the process of encapsulating complex capabilities / functionality into a simpler, more user-friendly interface.
I’m not going to provide a lot of lecture notes on this topic. Instead, I’ll provide some links that I suggest you read:
In class, we will discuss what Abstraction is…