Procedural abstraction?

  • There is lots of procedural abstraction in both 2.4a and 2.4b
  • Everywhere where a procedure is named is an example of procedural abstraction. Naming helps keep things clean and easy and can be re-used later
  • The same procedure being used or called more than once is also an example of procedural abstraction and this happens multiple times.
import sqlite3


def create_connection(db_file):

    conn = None

    try:
        conn = sqlite3.connect(db_file)
        return conn
    except Error as e:
        print(e)
    return conn


def create_table(conn, create_table_sql):
    try:
        c = conn.cursor()
        c.execute(create_table_sql)
    except Error as e:
        print(e)


def main():
    database = 'instance/movies.db'

    movies_table = """CREATE TABLE IF NOT EXISTS movies ( 
                                        _id integer PRIMARY KEY,
                                        _movie text,
                                        _director text,
                                        _length text,
                                        _year integer
                                    ); """  # not null makes coloum not accpet null values
    conn = create_connection(database)

    # create table
    create_table(conn, movies_table)


if __name__ == '__main__':
    main()
def read():

    database = 'instance/movies.db'
    # Connect to the database file
    conn = sqlite3.connect(database)

    # Create a cursor object to execute SQL queries
    cursor = conn.cursor()

    # Execute a SELECT statement to retrieve data from a table
    results = cursor.execute('SELECT * FROM movies').fetchall()

    # Print the results
    if len(results) == 0:
        print("empty")
    else:
        for row in results:
            print(row)

    # Close objects
    cursor.close()
    conn.close()


read()
(2, '2009 A Space Odyssey', 'Stanley Kubrick', '2 h', 1968)
(3, 'The Godfather', 'Francis Capolla', '2h 55m', 1972)
(4, 'The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008)
(5, 'Fight Club', 'David FIncher', '2h 19m', 1999)
(7, 'The Godfather', 'Francis Capolla', '2h 55m', 1972)
(8, 'The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008)
(9, 'Fight Club', 'David FIncher', '2h 19m', 1999)
def create():
    database = 'instance/movies.db'
    movie = input("Enter movie name:")
    director = input("Enter director:")
    length = input("Enter length")
    year = input("Enter year")

    # Connect to the database file
    conn = sqlite3.connect(database)

    # execute SQL commands
    cursor = conn.cursor()

    try:
        # insert data into a table
        cursor.execute("INSERT INTO movies (_movie, _director, _length, _year) VALUES (?, ?, ?, ?)",
                       (movie, director, length, year))
        # Commit the changes 
        conn.commit()
        print(f"A new movie record {movie} has been created")

    except sqlite3.Error as error:
        print("Error with inserting", error)

    # Close the cursor and connection objects
    cursor.close()
    conn.close()


create()
A new movie record ? has been created
import sqlite3
# adding movies to the table


def add():
    database = 'instance/movies.db'
    # Connect to  db file
    conn = sqlite3.connect(database)

    # execute SQL commands
    cursor = conn.cursor()

    try:
        # insert data into a table
        cursor.execute("INSERT INTO movies (_movie, _director, _length, _year) VALUES (?, ?, ?, ?)", ('The Godfather', 'Francis Capolla', '2h 55m', 1972))
        cursor.execute("INSERT INTO movies (_movie, _director, _length, _year) VALUES (?, ?, ?, ?)",('The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008))
        cursor.execute("INSERT INTO movies (_movie, _director, _length, _year) VALUES (?, ?, ?, ?)",('Fight Club', 'David FIncher',  '2h 19m', 1999))
        # Commit the changes
        conn.commit()

    except sqlite3.Error as error:
        print("Error", error)

    # Close objects
    cursor.close()
    conn.close()


add()
read()
(2, '2009 A Space Odyssey', 'Stanley Kubrick', '2 h', 1968)
(3, 'The Godfather', 'Francis Capolla', '2h 55m', 1972)
(4, 'The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008)
(5, 'Fight Club', 'David FIncher', '2h 19m', 1999)
(7, 'The Godfather', 'Francis Capolla', '2h 55m', 1972)
(8, 'The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008)
(9, 'Fight Club', 'David FIncher', '2h 19m', 1999)
(10, '?', '?', '?', '?')
(11, 'The Godfather', 'Francis Capolla', '2h 55m', 1972)
(12, 'The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008)
(13, 'Fight Club', 'David FIncher', '2h 19m', 1999)
import sqlite3

def delete():
    
    database = 'instance/movies.db'
    id = input("Enter movie id to delete")

    # Connect to the database file
    conn = sqlite3.connect(database)

    # Create a cursor object to execute SQL commands
    cursor = conn.cursor()
    
    try:
        cursor.execute("DELETE FROM movies WHERE _id = ?", (id,))
        if cursor.rowcount == 0:
            # The uid was not found in the table
            print(f"No uid {id} was not found in the table")
        else:
            # The uid was found in the table and the row was deleted
            print(f"The row with uid {id} has been deleted")
        conn.commit()
    except sqlite3.Error as error:
        print("Error in DELETE:", error)
        
    # Close the cursor and connection objects
    cursor.close()
    conn.close()
print("----Original Table:----")
read()
print("")
delete()
read()
----Original Table:----
(2, '2009 A Space Odyssey', 'Stanley Kubrick', '2 h', 1968)
(3, 'The Godfather', 'Francis Capolla', '2h 55m', 1972)
(4, 'The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008)
(5, 'Fight Club', 'David FIncher', '2h 19m', 1999)
(13, 'Fight Club', 'David FIncher', '2h 19m', 1999)

The row with uid 13 has been deleted
(2, '2009 A Space Odyssey', 'Stanley Kubrick', '2 h', 1968)
(3, 'The Godfather', 'Francis Capolla', '2h 55m', 1972)
(4, 'The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008)
(5, 'Fight Club', 'David FIncher', '2h 19m', 1999)
import sqlite3

def update():  
    
    database = 'instance/movies.db'
    id = input("Enter a movie id to update movies runtime")
    runtime = input("Enter updated runtime status")
    if runtime == 1: 
        status = "changed"
    else: 
        status = ""

    # Connect to the database file
    conn = sqlite3.connect(database)

    # Create a cursor object to execute SQL commands
    cursor = conn.cursor()

    try:
        # Execute an SQL command to update data in a table
        cursor.execute("UPDATE movies SET _length = ? WHERE _id = ?", (runtime, id))
        if cursor.rowcount == 0:
            # The uid was not found in the table
            print(f"No id {id} was not found in the table")
        else:
            print(f"Movie {id} received an updated runtime: {status}")
            conn.commit()
    except sqlite3.Error as error:
        print("Error in UPDATE:", error)
        
    
    # Close the cursor and connection objects
    cursor.close()
    conn.close()
print('----Original Databse:----')
read()   
print("")
print('----Updated Databse:----')
update()
read()
----Original Databse:----
(2, '2009 A Space Odyssey', 'Stanley Kubrick', '2 h', 1968)
(3, 'The Godfather', 'Francis Capolla', '2h 55m', 1972)
(4, 'The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008)
(5, 'Fight Club', 'David FIncher', '2h 19m', 1999)

----Updated Databse:----
Movie 5 received an updated runtime: 
(2, '2009 A Space Odyssey', 'Stanley Kubrick', '2 h', 1968)
(3, 'The Godfather', 'Francis Capolla', '2h 55m', 1972)
(4, 'The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008)
(5, 'Fight Club', 'David FIncher', '100', 1999)
def menu():
    operation = input("Enter: (C)reate (R)ead (U)pdate or (D)elete")
    if operation.lower() == 'c':
        create()
    elif operation.lower() == 'r':
        read()
    elif operation.lower() == 'u':
        update()
    elif operation.lower() == 'd':
        delete()
    elif len(operation)==0: # Escape Key
        return
    else:
        print("Please enter c, r, u, d") 
    menu() # recursion (repeats)
        
try:
    menu() # start menu
except:
    print("Perform Jupyter 'Run All' prior to starting menu")
(2, '2009 A Space Odyssey', 'Stanley Kubrick', '2 h', 1968)
(3, 'The Godfather', 'Francis Capolla', '2h 55m', 1972)
(4, 'The Dark Knight', 'Christopher Nolan', ' 2h 32m', 2008)
(5, 'Fight Club', 'David FIncher', '100', 1999)