Skip to main content
Engineering LibreTexts

14: Object-oriented programming

  • Page ID
    89130
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    Managing larger programs

     

    At the beginning of this book, we came up with four basic programming patterns which we use to construct programs:

    • Sequential code
    • Conditional code (if statements)
    • Repetitive code (loops)
    • Store and reuse (functions)

    In later chapters, we explored simple variables as well as collection data structures like lists, tuples, and dictionaries.

    As we build programs, we design data structures and write code to manipulate those data structures. There are many ways to write programs and by now, you probably have written some programs that are "not so elegant" and other programs that are "more elegant". Even though your programs may be small, you are starting to see how there is a bit of art and aesthetic to writing code.

    As programs get to be millions of lines long, it becomes increasingly important to write code that is easy to understand. If you are working on a million-line program, you can never keep the entire program in your mind at the same time. We need ways to break large programs into multiple smaller pieces so that we have less to look at when solving a problem, fix a bug, or add a new feature.

    In a way, object oriented programming is a way to arrange your code so that you can zoom into 50 lines of the code and understand it while ignoring the other 999,950 lines of code for the moment.

    Getting started

    Like many aspects of programming, it is necessary to learn the concepts of object oriented programming before you can use them effectively. You should approach this chapter as a way to learn some terms and concepts and work through a few simple examples to lay a foundation for future learning.

    The key outcome of this chapter is to have a basic understanding of how objects are constructed and how they function and most importantly how we make use of the capabilities of objects that are provided to us by Python and Python libraries.

    Using objects

    As it turns out, we have been using objects all along in this book. Python provides us with many built-in objects. Here is some simple code where the first few lines should feel very simple and natural to you.

     

    stuff = list()
    stuff.append('python')
    stuff.append('chuck')
    stuff.sort()
    print (stuff[0])
    print (stuff.__getitem__(0))
    print (list.__getitem__(stuff,0))
    
    # Code: http://www.py4e.com/code3/party1.py

    Instead of focusing on what these lines accomplish, let's look at what is really happening from the point of view of object-oriented programming. Don't worry if the following paragraphs don't make any sense the first time you read them because we have not yet defined all of these terms.

    The first line constructs an object of type list, the second and third lines call the append() method, the fourth line calls the sort() method, and the fifth line retrieves the item at position 0.

    The sixth line calls the __getitem__() method in the stuff list with a parameter of zero.

    print (stuff.__getitem__(0))

    The seventh line is an even more verbose way of retrieving the 0th item in the list.

    print (list.__getitem__(stuff,0))

    In this code, we call the __getitem__ method in the list class and pass the list and the item we want retrieved from the list as parameters.

    The last three lines of the program are equivalent, but it is more convenient to simply use the square bracket syntax to look up an item at a particular position in a list.

    We can take a look at the capabilities of an object by looking at the output of the dir() function:

    >>> stuff = list()
    >>> dir(stuff)
    ['__add__', '__class__', '__contains__', '__delattr__',
    '__delitem__', '__dir__', '__doc__', '__eq__',
    '__format__', '__ge__', '__getattribute__', '__getitem__',
    '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
    '__iter__', '__le__', '__len__', '__lt__', '__mul__',
    '__ne__', '__new__', '__reduce__', '__reduce_ex__',
    '__repr__', '__reversed__', '__rmul__', '__setattr__',
    '__setitem__', '__sizeof__', '__str__', '__subclasshook__',
    'append', 'clear', 'copy', 'count', 'extend', 'index',
    'insert', 'pop', 'remove', 'reverse', 'sort']
    >>>

    The rest of this chapter will define all of the above terms so make sure to come back after you finish the chapter and re-read the above paragraphs to check your understanding.

    Starting with programs

    A program in its most basic form takes some input, does some processing, and produces some output. Our elevator conversion program demonstrates a very short but complete program showing all three of these steps.

    usf = input('Enter the US Floor Number: ')
    wf = int(usf) - 1
    print('Non-US Floor Number is',wf)
    
    # Code: http://www.py4e.com/code3/elev.py

    If we think a bit more about this program, there is the "outside world" and the program. The input and output aspects are where the program interacts with the outside world. Within the program we have code and data to accomplish the task the program is designed to solve.

    A Program
    A Program

    One way to think about object-oriented programming is that it separates our program into multiple "zones." Each zone contains some code and data (like a program) and has well defined interactions with the outside world and the other zones within the program.

    If we look back at the link extraction application where we used the BeautifulSoup library, we can see a program that is constructed by connecting different objects together to accomplish a task:

     

    # To run this, download the BeautifulSoup zip file
    # http://www.py4e.com/code3/bs4.zip
    # and unzip it in the same directory as this file
    
    import urllib.request, urllib.parse, urllib.error
    from bs4 import BeautifulSoup
    import ssl
    
    # Ignore SSL certificate errors
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    
    url = input('Enter - ')
    html = urllib.request.urlopen(url, context=ctx).read()
    soup = BeautifulSoup(html, 'html.parser')
    
    # Retrieve all of the anchor tags
    tags = soup('a')
    for tag in tags:
        print(tag.get('href', None))
    
    # Code: http://www.py4e.com/code3/urllinks.py

    We read the URL into a string and then pass that into urllib to retrieve the data from the web. The urllib library uses the socket library to make the actual network connection to retrieve the data. We take the string that urllib returns and hand it to BeautifulSoup for parsing. BeautifulSoup makes use of the object html.parser1 and returns an object. We call the tags() method on the returned object that returns a dictionary of tag objects. We loop through the tags and call the get() method for each tag to print out the href attribute.

    We can draw a picture of this program and how the objects work together.

    A Program as Network of Objects
    A Program as Network of Objects

    The key here is not to understand perfectly how this program works but to see how we build a network of interacting objects and orchestrate the movement of information between the objects to create a program. It is also important to note that when you looked at that program several chapters back, you could fully understand what was going on in the program without even realizing that the program was "orchestrating the movement of data between objects." It was just lines of code that got the job done.

    Subdividing a problem

    One of the advantages of the object-oriented approach is that it can hide complexity. For example, while we need to know how to use the urllib and BeautifulSoup code, we do not need to know how those libraries work internally. This allows us to focus on the part of the problem we need to solve and ignore the other parts of the program.

    Ignoring Detail When Using an Object
    Ignoring Detail When Using an Object

    This ability to focus exclusively on the part of a program that we care about and ignore the rest is also helpful to the developers of the objects that we use. For example, the programmers developing BeautifulSoup do not need to know or care about how we retrieve our HTML page, what parts we want to read, or what we plan to do with the data we extract from the web page.

    Ignoring Detail When Building an Object
    Ignoring Detail When Building an Object

    Our first Python object

    At a basic level, an object is simply some code plus data structures that are smaller than a whole program. Defining a function allows us to store a bit of code and give it a name and then later invoke that code using the name of the function.

    An object can contain a number of functions (which we call methods) as well as data that is used by those functions. We call data items that are part of the object attributes.

     

    We use the class keyword to define the data and code that will make up each of the objects. The class keyword includes the name of the class and begins an indented block of code where we include the attributes (data) and methods (code).

    class PartyAnimal:
       x = 0
    
       def party(self) :
         self.x = self.x + 1
         print("So far",self.x)
    
    an = PartyAnimal()
    an.party()
    an.party()
    an.party()
    PartyAnimal.party(an)
    
    # Code: http://www.py4e.com/code3/party2.py

    Each method looks like a function, starting with the def keyword and consisting of an indented block of code. This object has one attribute (x) and one method (party). The methods have a special first parameter that we name by convention self.

    Just as the def keyword does not cause function code to be executed, the class keyword does not create an object. Instead, the class keyword defines a template indicating what data and code will be contained in each object of type PartyAnimal. The class is like a cookie cutter and the objects created using the class are the cookies2. You don't put frosting on the cookie cutter; you put frosting on the cookies, and you can put different frosting on each cookie.

    A Class and Two Objects
    A Class and Two Objects

    If we continue through this sample program, we see the first executable line of code:

    an = PartyAnimal()

     

    This is where we instruct Python to construct (i.e., create) an object or instance of the class PartyAnimal. It looks like a function call to the class itself. Python constructs the object with the right data and methods and returns the object which is then assigned to the variable an. In a way this is quite similar to the following line which we have been using all along:

    counts = dict()

    Here we instruct Python to construct an object using the dict template (already present in Python), return the instance of dictionary, and assign it to the variable counts.

    When the PartyAnimal class is used to construct an object, the variable an is used to point to that object. We use an to access the code and data for that particular instance of the PartyAnimal class.

    Each Partyanimal object/instance contains within it a variable x and a method/function named party. We call the party method in this line:

    an.party()

    When the party method is called, the first parameter (which we call by convention self) points to the particular instance of the PartyAnimal object that party is called from. Within the party method, we see the line:

    self.x = self.x + 1

    This syntax using the dot operator is saying 'the x within self.' Each time party() is called, the internal x value is incremented by 1 and the value is printed out.

    The following line is another way to call the party method within the an object:

    PartyAnimal.party(an)

    In this variation, we access the code from within the class and explicitly pass the object pointer an as the first parameter (i.e., self within the method). You can think of an.party() as shorthand for the above line.

    When the program executes, it produces the following output:

    So far 1
    So far 2
    So far 3
    So far 4

    The object is constructed, and the party method is called four times, both incrementing and printing the value for x within the an object.

    Classes as types

     

    As we have seen, in Python all variables have a type. We can use the built-in dir function to examine the capabilities of a variable. We can also use type and dir with the classes that we create.

    class PartyAnimal:
       x = 0
    
       def party(self) :
         self.x = self.x + 1
         print("So far",self.x)
    
    an = PartyAnimal()
    print ("Type", type(an))
    print ("Dir ", dir(an))
    print ("Type", type(an.x))
    print ("Type", type(an.party))
    
    # Code: http://www.py4e.com/code3/party3.py

    When this program executes, it produces the following output:

    Type <class '__main__.PartyAnimal'>
    Dir  ['__class__', '__delattr__', ...
    '__sizeof__', '__str__', '__subclasshook__',
    '__weakref__', 'party', 'x']
    Type <class 'int'>
    Type <class 'method'>

    You can see that using the class keyword, we have created a new type. From the dir output, you can see both the x integer attribute and the party method are available in the object.

    Object lifecycle

     

    In the previous examples, we define a class (template), use that class to create an instance of that class (object), and then use the instance. When the program finishes, all of the variables are discarded. Usually, we don't think much about the creation and destruction of variables, but often as our objects become more complex, we need to take some action within the object to set things up as the object is constructed and possibly clean things up as the object is discarded.

    If we want our object to be aware of these moments of construction and destruction, we add specially named methods to our object:

    class PartyAnimal:
       x = 0
    
       def __init__(self):
         print('I am constructed')
    
       def party(self) :
         self.x = self.x + 1
         print('So far',self.x)
    
       def __del__(self):
         print('I am destructed', self.x)
    
    an = PartyAnimal()
    an.party()
    an.party()
    an = 42
    print('an contains',an)
    
    # Code: http://www.py4e.com/code3/party4.py

    When this program executes, it produces the following output:

    I am constructed
    So far 1
    So far 2
    I am destructed 2
    an contains 42

    As Python constructs our object, it calls our __init__ method to give us a chance to set up some default or initial values for the object. When Python encounters the line:

    an = 42

    It actually "throws our object away" so it can reuse the an variable to store the value 42. Just at the moment when our an object is being "destroyed" our destructor code (__del__) is called. We cannot stop our variable from being destroyed, but we can do any necessary cleanup right before our object no longer exists.

    When developing objects, it is quite common to add a constructor to an object to set up initial values for the object. It is relatively rare to need a destructor for an object.

    Multiple instances

    So far, we have defined a class, constructed a single object, used that object, and then thrown the object away. However, the real power in object-oriented programming happens when we construct multiple instances of our class.

    When we construct multiple objects from our class, we might want to set up different initial values for each of the objects. We can pass data to the constructors to give each object a different initial value:

    class PartyAnimal:
       x = 0
       name = ''
       def __init__(self, nam):
         self.name = nam
         print(self.name,'constructed')
    
       def party(self) :
         self.x = self.x + 1
         print(self.name,'party count',self.x)
    
    s = PartyAnimal('Sally')
    j = PartyAnimal('Jim')
    
    s.party()
    j.party()
    s.party()
    
    # Code: http://www.py4e.com/code3/party5.py

    The constructor has both a self parameter that points to the object instance and additional parameters that are passed into the constructor as the object is constructed:

    s = PartyAnimal('Sally')

    Within the constructor, the second line copies the parameter (nam) that is passed into the name attribute within the object instance.

    self.name = nam

    The output of the program shows that each of the objects (s and j) contain their own independent copies of x and nam:

    Sally constructed
    Jim constructed
    Sally party count 1
    Jim party count 1
    Sally party count 2

    Inheritance

    Another powerful feature of object-oriented programming is the ability to create a new class by extending an existing class. When extending a class, we call the original class the parent class and the new class the child class.

    For this example, we move our PartyAnimal class into its own file. Then, we can 'import' the PartyAnimal class in a new file and extend it, as follows:

    from party import PartyAnimal
    
    class CricketFan(PartyAnimal):
       points = 0
       def six(self):
          self.points = self.points + 6
          self.party()
          print(self.name,"points",self.points)
    
    s = PartyAnimal("Sally")
    s.party()
    j = CricketFan("Jim")
    j.party()
    j.six()
    print(dir(j))
    
    # Code: http://www.py4e.com/code3/party6.py

    When we define the CricketFan class, we indicate that we are extending the PartyAnimal class. This means that all of the variables (x) and methods (party) from the PartyAnimal class are inherited by the CricketFan class. For example, within the six method in the CricketFan class, we call the party method from the PartyAnimal class.

    As the program executes, we create s and j as independent instances of PartyAnimal and CricketFan. The j object has additional capabilities beyond the s object.

    Sally constructed
    Sally party count 1
    Jim constructed
    Jim party count 1
    Jim party count 2
    Jim points 6
    ['__class__', '__delattr__', ... '__weakref__',
    'name', 'party', 'points', 'six', 'x']

    In the dir output for the j object (instance of the CricketFan class), we see that it has the attributes and methods of the parent class, as well as the attributes and methods that were added when the class was extended to create the CricketFan class.

    Summary

    This is a very quick introduction to object-oriented programming that focuses mainly on terminology and the syntax of defining and using objects. Let's quickly review the code that we looked at in the beginning of the chapter. At this point you should fully understand what is going on.

    stuff = list()
    stuff.append('python')
    stuff.append('chuck')
    stuff.sort()
    print (stuff[0])
    print (stuff.__getitem__(0))
    print (list.__getitem__(stuff,0))
    
    # Code: http://www.py4e.com/code3/party1.py

    The first line constructs a list object. When Python creates the list object, it calls the constructor method (named __init__) to set up the internal data attributes that will be used to store the list data. We have not passed any parameters to the constructor. When the constructor returns, we use the variable stuff to point to the returned instance of the list class.

    The second and third lines call the append method with one parameter to add a new item at the end of the list by updating the attributes within stuff. Then in the fourth line, we call the sort method with no parameters to sort the data within the stuff object.

    We then print out the first item in the list using the square brackets which are a shortcut to calling the __getitem__ method within the stuff. This is equivalent to calling the __getitem__ method in the list class and passing the stuff object as the first parameter and the position we are looking for as the second parameter.

    At the end of the program, the stuff object is discarded but not before calling the destructor (named __del__) so that the object can clean up any loose ends as necessary.

    Those are the basics of object-oriented programming. There are many additional details as to how to best use object-oriented approaches when developing large applications and libraries that are beyond the scope of this chapter.3

    Glossary

    attribute
    A variable that is part of a class.
    class
    A template that can be used to construct an object. Defines the attributes and methods that will make up the object.
    child class
    A new class created when a parent class is extended. The child class inherits all of the attributes and methods of the parent class.
    constructor
    An optional specially named method (__init__) that is called at the moment when a class is being used to construct an object. Usually this is used to set up initial values for the object.
    destructor
    An optional specially named method (__del__) that is called at the moment just before an object is destroyed. Destructors are rarely used.
    inheritance
    When we create a new class (child) by extending an existing class (parent). The child class has all the attributes and methods of the parent class plus additional attributes and methods defined by the child class.
    method
    A function that is contained within a class and the objects that are constructed from the class. Some object-oriented patterns use 'message' instead of 'method' to describe this concept.
    object
    A constructed instance of a class. An object contains all of the attributes and methods that were defined by the class. Some object-oriented documentation uses the term 'instance' interchangeably with 'object'.
    parent class
    The class which is being extended to create a new child class. The parent class contributes all of its methods and attributes to the new child class. Using Databases and SQL =======================

    What is a database?

     

    A database is a file that is organized for storing data. Most databases are organized like a dictionary in the sense that they map from keys to values. The biggest difference is that the database is on disk (or other permanent storage), so it persists after the program ends. Because a database is stored on permanent storage, it can store far more data than a dictionary, which is limited to the size of the memory in the computer.

     

    Like a dictionary, database software is designed to keep the inserting and accessing of data very fast, even for large amounts of data. Database software maintains its performance by building indexes as data is added to the database to allow the computer to jump quickly to a particular entry.

    There are many different database systems which are used for a wide variety of purposes including: Oracle, MySQL, Microsoft SQL Server, PostgreSQL, and SQLite. We focus on SQLite in this book because it is a very common database and is already built into Python. SQLite is designed to be embedded into other applications to provide database support within the application. For example, the Firefox browser also uses the SQLite database internally as do many other products.

    http://sqlite.org/

    SQLite is well suited to some of the data manipulation problems that we see in Informatics such as the Twitter spidering application that we describe in this chapter.

    Database concepts

    When you first look at a database it looks like a spreadsheet with multiple sheets. The primary data structures in a database are: tables, rows, and columns.

    Relational Databases
    Relational Databases

    In technical descriptions of relational databases the concepts of table, row, and column are more formally referred to as relation, tuple, and attribute, respectively. We will use the less formal terms in this chapter.

    Database Browser for SQLite

    While this chapter will focus on using Python to work with data in SQLite database files, many operations can be done more conveniently using software called the Database Browser for SQLite which is freely available from:

    http://sqlitebrowser.org/

    Using the browser you can easily create tables, insert data, edit data, or run simple SQL queries on the data in the database.

    In a sense, the database browser is similar to a text editor when working with text files. When you want to do one or very few operations on a text file, you can just open it in a text editor and make the changes you want. When you have many changes that you need to do to a text file, often you will write a simple Python program. You will find the same pattern when working with databases. You will do simple operations in the database manager and more complex operations will be most conveniently done in Python.

    Creating a database table

    Databases require more defined structure than Python lists or dictionaries4.

    When we create a database table we must tell the database in advance the names of each of the columns in the table and the type of data which we are planning to store in each column. When the database software knows the type of data in each column, it can choose the most efficient way to store and look up the data based on the type of data.

    You can look at the various data types supported by SQLite at the following url:

    http://www.sqlite.org/datatypes.html

    Defining structure for your data up front may seem inconvenient at the beginning, but the payoff is fast access to your data even when the database contains a large amount of data.

    The code to create a database file and a table named Tracks with two columns in the database is as follows:

     

    import sqlite3
    
    conn = sqlite3.connect('music.sqlite')
    cur = conn.cursor()
    
    cur.execute('DROP TABLE IF EXISTS Tracks')
    cur.execute('CREATE TABLE Tracks (title TEXT, plays INTEGER)')
    
    conn.close()
    
    # Code: http://www.py4e.com/code3/db1.py

     

    The connect operation makes a "connection" to the database stored in the file music.sqlite in the current directory. If the file does not exist, it will be created. The reason this is called a "connection" is that sometimes the database is stored on a separate "database server" from the server on which we are running our application. In our simple examples the database will just be a local file in the same directory as the Python code we are running.

    A cursor is like a file handle that we can use to perform operations on the data stored in the database. Calling cursor() is very similar conceptually to calling open() when dealing with text files.

    A Database Cursor
    A Database Cursor

    Once we have the cursor, we can begin to execute commands on the contents of the database using the execute() method.

    Database commands are expressed in a special language that has been standardized across many different database vendors to allow us to learn a single database language. The database language is called Structured Query Language or SQL for short.

    http://en.wikipedia.org/wiki/SQL

    In our example, we are executing two SQL commands in our database. As a convention, we will show the SQL keywords in uppercase and the parts of the command that we are adding (such as the table and column names) will be shown in lowercase.

    The first SQL command removes the Tracks table from the database if it exists. This pattern is simply to allow us to run the same program to create the Tracks table over and over again without causing an error. Note that the DROP TABLE command deletes the table and all of its contents from the database (i.e., there is no "undo").

    cur.execute('DROP TABLE IF EXISTS Tracks ')

    The second command creates a table named Tracks with a text column named title and an integer column named plays.

    cur.execute('CREATE TABLE Tracks (title TEXT, plays INTEGER)')

    Now that we have created a table named Tracks, we can put some data into that table using the SQL INSERT operation. Again, we begin by making a connection to the database and obtaining the cursor. We can then execute SQL commands using the cursor.

    The SQL INSERT command indicates which table we are using and then defines a new row by listing the fields we want to include (title, plays) followed by the VALUES we want placed in the new row. We specify the values as question marks (?, ?) to indicate that the actual values are passed in as a tuple ( 'My Way', 15 ) as the second parameter to the execute() call.

    import sqlite3
    
    conn = sqlite3.connect('music.sqlite')
    cur = conn.cursor()
    
    cur.execute('INSERT INTO Tracks (title, plays) VALUES (?, ?)',
        ('Thunderstruck', 20))
    cur.execute('INSERT INTO Tracks (title, plays) VALUES (?, ?)',
        ('My Way', 15))
    conn.commit()
    
    print('Tracks:')
    cur.execute('SELECT title, plays FROM Tracks')
    for row in cur:
         print(row)
    
    cur.execute('DELETE FROM Tracks WHERE plays < 100')
    conn.commit()
    
    cur.close()
    
    # Code: http://www.py4e.com/code3/db2.py

    First we INSERT two rows into our table and use commit() to force the data to be written to the database file.

    Rows in a Table
    Rows in a Table

    Then we use the SELECT command to retrieve the rows we just inserted from the table. On the SELECT command, we indicate which columns we would like (title, plays) and indicate which table we want to retrieve the data from. After we execute the SELECT statement, the cursor is something we can loop through in a for statement. For efficiency, the cursor does not read all of the data from the database when we execute the SELECT statement. Instead, the data is read on demand as we loop through the rows in the for statement.

    The output of the program is as follows:

    Tracks:
    ('Thunderstruck', 20)
    ('My Way', 15)

     

    Our for loop finds two rows, and each row is a Python tuple with the first value as the title and the second value as the number of plays.

    Note: You may see strings starting with u' in other books or on the Internet. This was an indication in Python 2 that the strings are Unicode* strings that are capable of storing non-Latin character sets. In Python 3, all strings are unicode strings by default.*

    At the very end of the program, we execute an SQL command to DELETE the rows we have just created so we can run the program over and over. The DELETE command shows the use of a WHERE clause that allows us to express a selection criterion so that we can ask the database to apply the command to only the rows that match the criterion. In this example the criterion happens to apply to all the rows so we empty the table out so we can run the program repeatedly. After the DELETE is performed, we also call commit() to force the data to be removed from the database.

    Structured Query Language summary

    So far, we have been using the Structured Query Language in our Python examples and have covered many of the basics of the SQL commands. In this section, we look at the SQL language in particular and give an overview of SQL syntax.

    Since there are so many different database vendors, the Structured Query Language (SQL) was standardized so we could communicate in a portable manner to database systems from multiple vendors.

    A relational database is made up of tables, rows, and columns. The columns generally have a type such as text, numeric, or date data. When we create a table, we indicate the names and types of the columns:

    CREATE TABLE Tracks (title TEXT, plays INTEGER)

    To insert a row into a table, we use the SQL INSERT command:

    INSERT INTO Tracks (title, plays) VALUES ('My Way', 15)

    The INSERT statement specifies the table name, then a list of the fields/columns that you would like to set in the new row, and then the keyword VALUES and a list of corresponding values for each of the fields.

    The SQL SELECT command is used to retrieve rows and columns from a database. The SELECT statement lets you specify which columns you would like to retrieve as well as a WHERE clause to select which rows you would like to see. It also allows an optional ORDER BY clause to control the sorting of the returned rows.

    SELECT * FROM Tracks WHERE title = 'My Way'

    Using * indicates that you want the database to return all of the columns for each row that matches the WHERE clause.

    Note, unlike in Python, in a SQL WHERE clause we use a single equal sign to indicate a test for equality rather than a double equal sign. Other logical operations allowed in a WHERE clause include <, >, <=, >=, !=, as well as AND and OR and parentheses to build your logical expressions.

    You can request that the returned rows be sorted by one of the fields as follows:

    SELECT title,plays FROM Tracks ORDER BY title

    To remove a row, you need a WHERE clause on an SQL DELETE statement. The WHERE clause determines which rows are to be deleted:

    DELETE FROM Tracks WHERE title = 'My Way'

    It is possible to UPDATE a column or columns within one or more rows in a table using the SQL UPDATE statement as follows:

    UPDATE Tracks SET plays = 16 WHERE title = 'My Way'

    The UPDATE statement specifies a table and then a list of fields and values to change after the SET keyword and then an optional WHERE clause to select the rows that are to be updated. A single UPDATE statement will change all of the rows that match the WHERE clause. If a WHERE clause is not specified, it performs the UPDATE on all of the rows in the table.

    These four basic SQL commands (INSERT, SELECT, UPDATE, and DELETE) allow the four basic operations needed to create and maintain data.

    Spidering Twitter using a database

    In this section, we will create a simple spidering program that will go through Twitter accounts and build a database of them. Note: Be very careful when running this program. You do not want to pull too much data or run the program for too long and end up having your Twitter access shut off.

    One of the problems of any kind of spidering program is that it needs to be able to be stopped and restarted many times and you do not want to lose the data that you have retrieved so far. You don't want to always restart your data retrieval at the very beginning so we want to store data as we retrieve it so our program can start back up and pick up where it left off.

    We will start by retrieving one person's Twitter friends and their statuses, looping through the list of friends, and adding each of the friends to a database to be retrieved in the future. After we process one person's Twitter friends, we check in our database and retrieve one of the friends of the friend. We do this over and over, picking an "unvisited" person, retrieving their friend list, and adding friends we have not seen to our list for a future visit.

    We also track how many times we have seen a particular friend in the database to get some sense of their "popularity".

    By storing our list of known accounts and whether we have retrieved the account or not, and how popular the account is in a database on the disk of the computer, we can stop and restart our program as many times as we like.

    This program is a bit complex. It is based on the code from the exercise earlier in the book that uses the Twitter API.

    Here is the source code for our Twitter spidering application:

    from urllib.request import urlopen
    import urllib.error
    import twurl
    import json
    import sqlite3
    import ssl
    
    TWITTER_URL = 'https://api.twitter.com/1.1/friends/list.json'
    
    conn = sqlite3.connect('spider.sqlite')
    cur = conn.cursor()
    
    cur.execute('''
                CREATE TABLE IF NOT EXISTS Twitter
                (name TEXT, retrieved INTEGER, friends INTEGER)''')
    
    # Ignore SSL certificate errors
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    
    while True:
        acct = input('Enter a Twitter account, or quit: ')
        if (acct == 'quit'): break
        if (len(acct) < 1):
            cur.execute('SELECT name FROM Twitter WHERE retrieved = 0 LIMIT 1')
            try:
                acct = cur.fetchone()[0]
            except:
                print('No unretrieved Twitter accounts found')
                continue
    
        url = twurl.augment(TWITTER_URL, {'screen_name': acct, 'count': '20'})
        print('Retrieving', url)
        connection = urlopen(url, context=ctx)
        data = connection.read().decode()
        headers = dict(connection.getheaders())
    
        print('Remaining', headers['x-rate-limit-remaining'])
        js = json.loads(data)
        # Debugging
        # print json.dumps(js, indent=4)
    
        cur.execute('UPDATE Twitter SET retrieved=1 WHERE name = ?', (acct, ))
    
        countnew = 0
        countold = 0
        for u in js['users']:
            friend = u['screen_name']
            print(friend)
            cur.execute('SELECT friends FROM Twitter WHERE name = ? LIMIT 1',
                        (friend, ))
            try:
                count = cur.fetchone()[0]
                cur.execute('UPDATE Twitter SET friends = ? WHERE name = ?',
                            (count+1, friend))
                countold = countold + 1
            except:
                cur.execute('''INSERT INTO Twitter (name, retrieved, friends)
                            VALUES (?, 0, 1)''', (friend, ))
                countnew = countnew + 1
        print('New accounts=', countnew, ' revisited=', countold)
        conn.commit()
    
    cur.close()
    
    # Code: http://www.py4e.com/code3/twspider.py

    Our database is stored in the file spider.sqlite and it has one table named Twitter. Each row in the Twitter table has a column for the account name, whether we have retrieved the friends of this account, and how many times this account has been "friended".

    In the main loop of the program, we prompt the user for a Twitter account name or "quit" to exit the program. If the user enters a Twitter account, we retrieve the list of friends and statuses for that user and add each friend to the database if not already in the database. If the friend is already in the list, we add 1 to the friends field in the row in the database.

    If the user presses enter, we look in the database for the next Twitter account that we have not yet retrieved, retrieve the friends and statuses for that account, add them to the database or update them, and increase their friends count.

    Once we retrieve the list of friends and statuses, we loop through all of the user items in the returned JSON and retrieve the screen_name for each user. Then we use the SELECT statement to see if we already have stored this particular screen_name in the database and retrieve the friend count (friends) if the record exists.

    countnew = 0
    countold = 0
    for u in js['users'] :
        friend = u['screen_name']
        print(friend)
        cur.execute('SELECT friends FROM Twitter WHERE name = ? LIMIT 1',
            (friend, ) )
        try:
            count = cur.fetchone()[0]
            cur.execute('UPDATE Twitter SET friends = ? WHERE name = ?',
                (count+1, friend) )
            countold = countold + 1
        except:
            cur.execute('''INSERT INTO Twitter (name, retrieved, friends)
                VALUES ( ?, 0, 1 )''', ( friend, ) )
            countnew = countnew + 1
    print('New accounts=',countnew,' revisited=',countold)
    conn.commit()

    Once the cursor executes the SELECT statement, we must retrieve the rows. We could do this with a for statement, but since we are only retrieving one row (LIMIT 1), we can use the fetchone() method to fetch the first (and only) row that is the result of the SELECT operation. Since fetchone() returns the row as a tuple (even though there is only one field), we use the first value from the tuple to get the current friend count into the variable count.

    If this retrieval is successful, we use the SQL UPDATE statement with a WHERE clause to add 1 to the friends column for the row that matches the friend's account. Notice that there are two placeholders (i.e., question marks) in the SQL, and the second parameter to the execute() is a two-element tuple that holds the values to be substituted into the SQL in place of the question marks.

    If the code in the try block fails, it is probably because no record matched the WHERE name = ? clause on the SELECT statement. So in the except block, we use the SQL INSERT statement to add the friend's screen_name to the table with an indication that we have not yet retrieved the screen_name and set the friends count to one.

    So the first time the program runs and we enter a Twitter account, the program runs as follows:

    Enter a Twitter account, or quit: drchuck
    Retrieving http://api.twitter.com/1.1/friends ...
    New accounts= 20  revisited= 0
    Enter a Twitter account, or quit: quit

    Since this is the first time we have run the program, the database does not exist, so we create the database in the file spider.sqlite and add a table named Twitter to the database. Then we retrieve some friends and add them all to the database since the database is empty.

    At this point, we might want to write a simple database dumper to take a look at what is in our spider.sqlite file:

    import sqlite3
    
    conn = sqlite3.connect('spider.sqlite')
    cur = conn.cursor()
    cur.execute('SELECT * FROM Twitter')
    count = 0
    for row in cur:
        print(row)
        count = count + 1
    print(count, 'rows.')
    cur.close()
    
    # Code: http://www.py4e.com/code3/twdump.py

    This program simply opens the database and selects all of the columns of all of the rows in the table Twitter, then loops through the rows and prints out each row.

    If we run this program after the first execution of our Twitter spider above, its output will be as follows:

    ('opencontent', 0, 1)
    ('lhawthorn', 0, 1)
    ('steve_coppin', 0, 1)
    ('davidkocher', 0, 1)
    ('hrheingold', 0, 1)
    ...
    20 rows.

    We see one row for each screen_name, that we have not retrieved the data for that screen_name, and everyone in the database has one friend.

    Now our database reflects the retrieval of the friends of our first Twitter account (drchuck). We can run the program again and tell it to retrieve the friends of the next "unprocessed" account by simply pressing enter instead of a Twitter account as follows:

    Enter a Twitter account, or quit:
    Retrieving http://api.twitter.com/1.1/friends ...
    New accounts= 18  revisited= 2
    Enter a Twitter account, or quit:
    Retrieving http://api.twitter.com/1.1/friends ...
    New accounts= 17  revisited= 3
    Enter a Twitter account, or quit: quit

    Since we pressed enter (i.e., we did not specify a Twitter account), the following code is executed:

    if ( len(acct) < 1 ) :
        cur.execute('SELECT name FROM Twitter WHERE retrieved = 0 LIMIT 1')
        try:
            acct = cur.fetchone()[0]
        except:
            print('No unretrieved twitter accounts found')
            continue

    We use the SQL SELECT statement to retrieve the name of the first (LIMIT 1) user who still has their "have we retrieved this user" value set to zero. We also use the fetchone()[0] pattern within a try/except block to either extract a screen_name from the retrieved data or put out an error message and loop back up.

    If we successfully retrieved an unprocessed screen_name, we retrieve their data as follows:

    url=twurl.augment(TWITTER_URL,{'screen_name': acct,'count': '20'})
    print('Retrieving', url)
    connection = urllib.urlopen(url)
    data = connection.read()
    js = json.loads(data)
    
    cur.execute('UPDATE Twitter SET retrieved=1 WHERE name = ?',(acct, ))

    Once we retrieve the data successfully, we use the UPDATE statement to set the retrieved column to 1 to indicate that we have completed the retrieval of the friends of this account. This keeps us from retrieving the same data over and over and keeps us progressing forward through the network of Twitter friends.

    If we run the friend program and press enter twice to retrieve the next unvisited friend's friends, then run the dumping program, it will give us the following output:

    ('opencontent', 1, 1)
    ('lhawthorn', 1, 1)
    ('steve_coppin', 0, 1)
    ('davidkocher', 0, 1)
    ('hrheingold', 0, 1)
    ...
    ('cnxorg', 0, 2)
    ('knoop', 0, 1)
    ('kthanos', 0, 2)
    ('LectureTools', 0, 1)
    ...
    55 rows.

    We can see that we have properly recorded that we have visited lhawthorn and opencontent. Also the accounts cnxorg and kthanos already have two followers. Since we now have retrieved the friends of three people (drchuck, opencontent, and lhawthorn) our table has 52 rows of friends to retrieve.

    Each time we run the program and press enter it will pick the next unvisited account (e.g., the next account will be steve_coppin), retrieve their friends, mark them as retrieved, and for each of the friends of steve_coppin either add them to the end of the database or update their friend count if they are already in the database.

    Since the program's data is all stored on disk in a database, the spidering activity can be suspended and resumed as many times as you like with no loss of data.

    Basic data modeling

    The real power of a relational database is when we create multiple tables and make links between those tables. The act of deciding how to break up your application data into multiple tables and establishing the relationships between the tables is called data modeling. The design document that shows the tables and their relationships is called a data model.

    Data modeling is a relatively sophisticated skill and we will only introduce the most basic concepts of relational data modeling in this section. For more detail on data modeling you can start with:

    http://en.wikipedia.org/wiki/Relational_model

    Let's say for our Twitter spider application, instead of just counting a person's friends, we wanted to keep a list of all of the incoming relationships so we could find a list of everyone who is following a particular account.

    Since everyone will potentially have many accounts that follow them, we cannot simply add a single column to our Twitter table. So we create a new table that keeps track of pairs of friends. The following is a simple way of making such a table:

    CREATE TABLE Pals (from_friend TEXT, to_friend TEXT)

    Each time we encounter a person who drchuck is following, we would insert a row of the form:

    INSERT INTO Pals (from_friend,to_friend) VALUES ('drchuck', 'lhawthorn')

    As we are processing the 20 friends from the drchuck Twitter feed, we will insert 20 records with "drchuck" as the first parameter so we will end up duplicating the string many times in the database.

    This duplication of string data violates one of the best practices for database normalization which basically states that we should never put the same string data in the database more than once. If we need the data more than once, we create a numeric key for the data and reference the actual data using this key.

    In practical terms, a string takes up a lot more space than an integer on the disk and in the memory of our computer, and takes more processor time to compare and sort. If we only have a few hundred entries, the storage and processor time hardly matters. But if we have a million people in our database and a possibility of 100 million friend links, it is important to be able to scan data as quickly as possible.

    We will store our Twitter accounts in a table named People instead of the Twitter table used in the previous example. The People table has an additional column to store the numeric key associated with the row for this Twitter user. SQLite has a feature that automatically adds the key value for any row we insert into a table using a special type of data column (INTEGER PRIMARY KEY).

    We can create the People table with this additional id column as follows:

    CREATE TABLE People
        (id INTEGER PRIMARY KEY, name TEXT UNIQUE, retrieved INTEGER)

    Notice that we are no longer maintaining a friend count in each row of the People table. When we select INTEGER PRIMARY KEY as the type of our id column, we are indicating that we would like SQLite to manage this column and assign a unique numeric key to each row we insert automatically. We also add the keyword UNIQUE to indicate that we will not allow SQLite to insert two rows with the same value for name.

    Now instead of creating the table Pals above, we create a table called Follows with two integer columns from_id and to_id and a constraint on the table that the combination of from_id and to_id must be unique in this table (i.e., we cannot insert duplicate rows) in our database.

    CREATE TABLE Follows
        (from_id INTEGER, to_id INTEGER, UNIQUE(from_id, to_id) )

    When we add UNIQUE clauses to our tables, we are communicating a set of rules that we are asking the database to enforce when we attempt to insert records. We are creating these rules as a convenience in our programs, as we will see in a moment. The rules both keep us from making mistakes and make it simpler to write some of our code.

    In essence, in creating this Follows table, we are modelling a "relationship" where one person "follows" someone else and representing it with a pair of numbers indicating that (a) the people are connected and (b) the direction of the relationship.

    Relationships Between Tables
    Relationships Between Tables

    Programming with multiple tables

    We will now redo the Twitter spider program using two tables, the primary keys, and the key references as described above. Here is the code for the new version of the program:

    import urllib.request, urllib.parse, urllib.error
    import twurl
    import json
    import sqlite3
    import ssl
    
    TWITTER_URL = 'https://api.twitter.com/1.1/friends/list.json'
    
    conn = sqlite3.connect('friends.sqlite')
    cur = conn.cursor()
    
    cur.execute('''CREATE TABLE IF NOT EXISTS People
                (id INTEGER PRIMARY KEY, name TEXT UNIQUE, retrieved INTEGER)''')
    cur.execute('''CREATE TABLE IF NOT EXISTS Follows
                (from_id INTEGER, to_id INTEGER, UNIQUE(from_id, to_id))''')
    
    # Ignore SSL certificate errors
    ctx = ssl.create_default_context()
    ctx.check_hostname = False
    ctx.verify_mode = ssl.CERT_NONE
    
    while True:
        acct = input('Enter a Twitter account, or quit: ')
        if (acct == 'quit'): break
        if (len(acct) < 1):
            cur.execute('SELECT id, name FROM People WHERE retrieved=0 LIMIT 1')
            try:
                (id, acct) = cur.fetchone()
            except:
                print('No unretrieved Twitter accounts found')
                continue
        else:
            cur.execute('SELECT id FROM People WHERE name = ? LIMIT 1',
                        (acct, ))
            try:
                id = cur.fetchone()[0]
            except:
                cur.execute('''INSERT OR IGNORE INTO People
                            (name, retrieved) VALUES (?, 0)''', (acct, ))
                conn.commit()
                if cur.rowcount != 1:
                    print('Error inserting account:', acct)
                    continue
                id = cur.lastrowid
    
        url = twurl.augment(TWITTER_URL, {'screen_name': acct, 'count': '100'})
        print('Retrieving account', acct)
        try:
            connection = urllib.request.urlopen(url, context=ctx)
        except Exception as err:
            print('Failed to Retrieve', err)
            break
    
        data = connection.read().decode()
        headers = dict(connection.getheaders())
    
        print('Remaining', headers['x-rate-limit-remaining'])
    
        try:
            js = json.loads(data)
        except:
            print('Unable to parse json')
            print(data)
            break
    
        # Debugging
        # print(json.dumps(js, indent=4))
    
        if 'users' not in js:
            print('Incorrect JSON received')
            print(json.dumps(js, indent=4))
            continue
    
        cur.execute('UPDATE People SET retrieved=1 WHERE name = ?', (acct, ))
    
        countnew = 0
        countold = 0
        for u in js['users']:
            friend = u['screen_name']
            print(friend)
            cur.execute('SELECT id FROM People WHERE name = ? LIMIT 1',
                        (friend, ))
            try:
                friend_id = cur.fetchone()[0]
                countold = countold + 1
            except:
                cur.execute('''INSERT OR IGNORE INTO People (name, retrieved)
                            VALUES (?, 0)''', (friend, ))
                conn.commit()
                if cur.rowcount != 1:
                    print('Error inserting account:', friend)
                    continue
                friend_id = cur.lastrowid
                countnew = countnew + 1
            cur.execute('''INSERT OR IGNORE INTO Follows (from_id, to_id)
                        VALUES (?, ?)''', (id, friend_id))
        print('New accounts=', countnew, ' revisited=', countold)
        print('Remaining', headers['x-rate-limit-remaining'])
        conn.commit()
    cur.close()
    
    # Code: http://www.py4e.com/code3/twfriends.py

    This program is starting to get a bit complicated, but it illustrates the patterns that we need to use when we are using integer keys to link tables. The basic patterns are:

    1. Create tables with primary keys and constraints.

    2. When we have a logical key for a person (i.e., account name) and we need the id value for the person, depending on whether or not the person is already in the People table we either need to: (1) look up the person in the People table and retrieve the id value for the person or (2) add the person to the People table and get the id value for the newly added row.

    3. Insert the row that captures the "follows" relationship.

    We will cover each of these in turn.

    Constraints in database tables

    As we design our table structures, we can tell the database system that we would like it to enforce a few rules on us. These rules help us from making mistakes and introducing incorrect data into our tables. When we create our tables:

    cur.execute('''CREATE TABLE IF NOT EXISTS People
        (id INTEGER PRIMARY KEY, name TEXT UNIQUE, retrieved INTEGER)''')
    cur.execute('''CREATE TABLE IF NOT EXISTS Follows
        (from_id INTEGER, to_id INTEGER, UNIQUE(from_id, to_id))''')

    We indicate that the name column in the People table must be UNIQUE. We also indicate that the combination of the two numbers in each row of the Follows table must be unique. These constraints keep us from making mistakes such as adding the same relationship more than once.

    We can take advantage of these constraints in the following code:

    cur.execute('''INSERT OR IGNORE INTO People (name, retrieved)
        VALUES ( ?, 0)''', ( friend, ) )

    We add the OR IGNORE clause to our INSERT statement to indicate that if this particular INSERT would cause a violation of the "name must be unique" rule, the database system is allowed to ignore the INSERT. We are using the database constraint as a safety net to make sure we don't inadvertently do something incorrect.

    Similarly, the following code ensures that we don't add the exact same Follows relationship twice.

    cur.execute('''INSERT OR IGNORE INTO Follows
        (from_id, to_id) VALUES (?, ?)''', (id, friend_id) )

    Again, we simply tell the database to ignore our attempted INSERT if it would violate the uniqueness constraint that we specified for the Follows rows.

    Retrieve and/or insert a record

    When we prompt the user for a Twitter account, if the account exists, we must look up its id value. If the account does not yet exist in the People table, we must insert the record and get the id value from the inserted row.

    This is a very common pattern and is done twice in the program above. This code shows how we look up the id for a friend's account when we have extracted a screen_name from a user node in the retrieved Twitter JSON.

    Since over time it will be increasingly likely that the account will already be in the database, we first check to see if the People record exists using a SELECT statement.

    If all goes well5 inside the try section, we retrieve the record using fetchone() and then retrieve the first (and only) element of the returned tuple and store it in friend_id.

    If the SELECT fails, the fetchone()[0] code will fail and control will transfer into the except section.

        friend = u['screen_name']
        cur.execute('SELECT id FROM People WHERE name = ? LIMIT 1',
            (friend, ) )
        try:
            friend_id = cur.fetchone()[0]
            countold = countold + 1
        except:
            cur.execute('''INSERT OR IGNORE INTO People (name, retrieved)
                VALUES ( ?, 0)''', ( friend, ) )
            conn.commit()
            if cur.rowcount != 1 :
                print('Error inserting account:',friend)
                continue
            friend_id = cur.lastrowid
            countnew = countnew + 1

    If we end up in the except code, it simply means that the row was not found, so we must insert the row. We use INSERT OR IGNORE just to avoid errors and then call commit() to force the database to really be updated. After the write is done, we can check the cur.rowcount to see how many rows were affected. Since we are attempting to insert a single row, if the number of affected rows is something other than 1, it is an error.

    If the INSERT is successful, we can look at cur.lastrowid to find out what value the database assigned to the id column in our newly created row.

    Storing the friend relationship

    Once we know the key value for both the Twitter user and the friend in the JSON, it is a simple matter to insert the two numbers into the Follows table with the following code:

    cur.execute('INSERT OR IGNORE INTO Follows (from_id, to_id) VALUES (?, ?)',
        (id, friend_id) )

    Notice that we let the database take care of keeping us from "double-inserting" a relationship by creating the table with a uniqueness constraint and then adding OR IGNORE to our INSERT statement.

    Here is a sample execution of this program:

    Enter a Twitter account, or quit:
    No unretrieved Twitter accounts found
    Enter a Twitter account, or quit: drchuck
    Retrieving http://api.twitter.com/1.1/friends ...
    New accounts= 20  revisited= 0
    Enter a Twitter account, or quit:
    Retrieving http://api.twitter.com/1.1/friends ...
    New accounts= 17  revisited= 3
    Enter a Twitter account, or quit:
    Retrieving http://api.twitter.com/1.1/friends ...
    New accounts= 17  revisited= 3
    Enter a Twitter account, or quit: quit

    We started with the drchuck account and then let the program automatically pick the next two accounts to retrieve and add to our database.

    The following is the first few rows in the People and Follows tables after this run is completed:

    People:
    (1, 'drchuck', 1)
    (2, 'opencontent', 1)
    (3, 'lhawthorn', 1)
    (4, 'steve_coppin', 0)
    (5, 'davidkocher', 0)
    55 rows.
    Follows:
    (1, 2)
    (1, 3)
    (1, 4)
    (1, 5)
    (1, 6)
    60 rows.

    You can see the id, name, and visited fields in the People table and you see the numbers of both ends of the relationship in the Follows table. In the People table, we can see that the first three people have been visited and their data has been retrieved. The data in the Follows table indicates that drchuck (user 1) is a friend to all of the people shown in the first five rows. This makes sense because the first data we retrieved and stored was the Twitter friends of drchuck. If you were to print more rows from the Follows table, you would see the friends of users 2 and 3 as well.

    Three kinds of keys

    Now that we have started building a data model putting our data into multiple linked tables and linking the rows in those tables using keys, we need to look at some terminology around keys. There are generally three kinds of keys used in a database model.

    • A logical key is a key that the "real world" might use to look up a row. In our example data model, the name field is a logical key. It is the screen name for the user and we indeed look up a user's row several times in the program using the name field. You will often find that it makes sense to add a UNIQUE constraint to a logical key. Since the logical key is how we look up a row from the outside world, it makes little sense to allow multiple rows with the same value in the table.

    • A primary key is usually a number that is assigned automatically by the database. It generally has no meaning outside the program and is only used to link rows from different tables together. When we want to look up a row in a table, usually searching for the row using the primary key is the fastest way to find the row. Since primary keys are integer numbers, they take up very little storage and can be compared or sorted very quickly. In our data model, the id field is an example of a primary key.

    • A foreign key is usually a number that points to the primary key of an associated row in a different table. An example of a foreign key in our data model is the from_id.

    We are using a naming convention of always calling the primary key field name id and appending the suffix _id to any field name that is a foreign key.

    Using JOIN to retrieve data

    Now that we have followed the rules of database normalization and have data separated into two tables, linked together using primary and foreign keys, we need to be able to build a SELECT that reassembles the data across the tables.

    SQL uses the JOIN clause to reconnect these tables. In the JOIN clause you specify the fields that are used to reconnect the rows between the tables.

    The following is an example of a SELECT with a JOIN clause:

    SELECT * FROM Follows JOIN People
        ON Follows.from_id = People.id WHERE People.id = 1

    The JOIN clause indicates that the fields we are selecting cross both the Follows and People tables. The ON clause indicates how the two tables are to be joined: Take the rows from Follows and append the row from People where the field from_id in Follows is the same the id value in the People table.

    Connecting Tables Using JOIN
    Connecting Tables Using JOIN

    The result of the JOIN is to create extra-long "metarows" which have both the fields from People and the matching fields from Follows. Where there is more than one match between the id field from People and the from_id from Follows, then JOIN creates a metarow for each of the matching pairs of rows, duplicating data as needed.

    The following code demonstrates the data that we will have in the database after the multi-table Twitter spider program (above) has been run several times.

    import sqlite3
    
    conn = sqlite3.connect('friends.sqlite')
    cur = conn.cursor()
    
    cur.execute('SELECT * FROM People')
    count = 0
    print('People:')
    for row in cur:
        if count < 5: print(row)
        count = count + 1
    print(count, 'rows.')
    
    cur.execute('SELECT * FROM Follows')
    count = 0
    print('Follows:')
    for row in cur:
        if count < 5: print(row)
        count = count + 1
    print(count, 'rows.')
    
    cur.execute('''SELECT * FROM Follows JOIN People
                ON Follows.to_id = People.id
                WHERE Follows.from_id = 2''')
    count = 0
    print('Connections for id=2:')
    for row in cur:
        if count < 5: print(row)
        count = count + 1
    print(count, 'rows.')
    
    cur.close()
    
    # Code: http://www.py4e.com/code3/twjoin.py

    In this program, we first dump out the People and Follows and then dump out a subset of the data in the tables joined together.

    Here is the output of the program:

    python twjoin.py
    People:
    (1, 'drchuck', 1)
    (2, 'opencontent', 1)
    (3, 'lhawthorn', 1)
    (4, 'steve_coppin', 0)
    (5, 'davidkocher', 0)
    55 rows.
    Follows:
    (1, 2)
    (1, 3)
    (1, 4)
    (1, 5)
    (1, 6)
    60 rows.
    Connections for id=2:
    (2, 1, 1, 'drchuck', 1)
    (2, 28, 28, 'cnxorg', 0)
    (2, 30, 30, 'kthanos', 0)
    (2, 102, 102, 'SomethingGirl', 0)
    (2, 103, 103, 'ja_Pac', 0)
    20 rows.

    You see the columns from the People and Follows tables and the last set of rows is the result of the SELECT with the JOIN clause.

    In the last select, we are looking for accounts that are friends of "opencontent" (i.e., People.id=2).

    In each of the "metarows" in the last select, the first two columns are from the Follows table followed by columns three through five from the People table. You can also see that the second column (Follows.to_id) matches the third column (People.id) in each of the joined-up "metarows".

    Summary

    This chapter has covered a lot of ground to give you an overview of the basics of using a database in Python. It is more complicated to write the code to use a database to store data than Python dictionaries or flat files so there is little reason to use a database unless your application truly needs the capabilities of a database. The situations where a database can be quite useful are: (1) when your application needs to make many small random updates within a large data set, (2) when your data is so large it cannot fit in a dictionary and you need to look up information repeatedly, or (3) when you have a long-running process that you want to be able to stop and restart and retain the data from one run to the next.

    You can build a simple database with a single table to suit many application needs, but most problems will require several tables and links/relationships between rows in different tables. When you start making links between tables, it is important to do some thoughtful design and follow the rules of database normalization to make the best use of the database's capabilities. Since the primary motivation for using a database is that you have a large amount of data to deal with, it is important to model your data efficiently so your programs run as fast as possible.

    Debugging

    One common pattern when you are developing a Python program to connect to an SQLite database will be to run a Python program and check the results using the Database Browser for SQLite. The browser allows you to quickly check to see if your program is working properly.

    You must be careful because SQLite takes care to keep two programs from changing the same data at the same time. For example, if you open a database in the browser and make a change to the database and have not yet pressed the "save" button in the browser, the browser "locks" the database file and keeps any other program from accessing the file. In particular, your Python program will not be able to access the file if it is locked.

    So a solution is to make sure to either close the database browser or use the File menu to close the database in the browser before you attempt to access the database from Python to avoid the problem of your Python code failing because the database is locked.

    Glossary

    attribute
    One of the values within a tuple. More commonly called a "column" or "field".
    constraint
    When we tell the database to enforce a rule on a field or a row in a table. A common constraint is to insist that there can be no duplicate values in a particular field (i.e., all the values must be unique).
    cursor
    A cursor allows you to execute SQL commands in a database and retrieve data from the database. A cursor is similar to a socket or file handle for network connections and files, respectively.
    database browser
    A piece of software that allows you to directly connect to a database and manipulate the database directly without writing a program.
    foreign key
    A numeric key that points to the primary key of a row in another table. Foreign keys establish relationships between rows stored in different tables.
    index
    Additional data that the database software maintains as rows and inserts into a table to make lookups very fast.
    logical key
    A key that the "outside world" uses to look up a particular row. For example in a table of user accounts, a person's email address might be a good candidate as the logical key for the user's data.
    normalization
    Designing a data model so that no data is replicated. We store each item of data at one place in the database and reference it elsewhere using a foreign key.
    primary key
    A numeric key assigned to each row that is used to refer to one row in a table from another table. Often the database is configured to automatically assign primary keys as rows are inserted.
    relation
    An area within a database that contains tuples and attributes. More typically called a "table".
    tuple
    A single entry in a database table that is a set of attributes. More typically called "row".

     


    1. https://docs.python.org/3/library/html.parser.html

    2. Cookie image copyright CC-BY https://www.flickr.com/photos/dinner...es/23570475099

    3. If you are curious about where the list class is defined, take a look at (hopefully the URL won't change) https://github.com/python/cpython/bl...s/listobject.c - the list class is written in a language called "C". If you take a look at that source code and find it curious you might want to explore a few Computer Science courses.

    4. SQLite actually does allow some flexibility in the type of data stored in a column, but we will keep our data types strict in this chapter so the concepts apply equally to other database systems such as MySQL.

    5. In general, when a sentence starts with "if all goes well" you will find that the code needs to use try/except.


    14: Object-oriented programming is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?