Challenge

Project 1 - English Dictionary App & Library Book Management System

1) ENGLISH DICTIONARY APP

 

#1. Database-Software Interface

import pickle #serialization package
import os

def saveDictionary(tmp_dict):
    '''
    This function recives a python dictionary,
    and saves it to a serialized file words.txt
    '''
    try:
        # 'wb' means open file with writing binary mode
        with open('words.txt', 'wb') as dictionary:
            pickle.dump(tmp_dict, dictionary)
        # dictionary = open('words.txt', 'wb')
        # pickle.dump(tmp_dict, dictionary)
        # dictionary.close()
        print("SUCCESS: writing to dictionary words.txt")
    except Exception as e:
        print(f"ERROR: {e}")


def getDictionary():
    '''
    This function returns a python dictionary,
    after de-serializing the file words.txt
    '''
    try:
        # 'rb' means open file with reading binary mode
        with open('words.txt', 'rb') as dictionary:
            tmp_dict = pickle.load(dictionary)
        print("SUCCESS: reading from dictionary words.txt")
        return tmp_dict
    except Exception as e:
        print(f"ERROR: {e}")


#2. User-Software Interface

def addNewWord():
    '''
    This function provides functionalities for n==1,
    that is, it creates a new word in the dictionary
    '''
    print("__________")
    print("NEW WORD ADDER:")
    print("__________")
    print("Enter a Word to add")
    word = input(">>")
    print(f"Enter a meaning of {word} to add")
    meaning = input(">>")
    dictionary = getDictionary()
    dictionary[word] = meaning
    saveDictionary(dictionary)

    # after the work of this function is done, we need to go back to the menu
    main_menu()


def findMeaning():
    '''
    This function provides functionalities for n==2,
    that is, it finds a word in the dictionary
    '''
    print("__________")
    print("WORD FINDER:")
    print("__________")
    print("Enter a Word to find")
    word = input(">>")
    dictionary = getDictionary()
    # check if the word exists
    # if it does, print its meaning
    if word in dictionary:
        print(f"Found Entry: [{word} : {dictionary[word]}]")
    else:
    # otherwise throw an error and go back to the submenu of findMeaning()
        print("ERROR: The word doesn't exist!")
        findMeaning()

    # after the work of this function is done, we need to go back to the menu
    main_menu()


def updateWord():
    '''
    This function provides functionalities for n==3,
    that is, it updates a word in the dictionary
    '''
    print("__________")
    print("WORD UPDATER:")
    print("__________")
    print("Enter an old Word to edit")
    word = input(">>")
    # check if the word exists
    # if it does, update it
    dictionary = getDictionary()
    if word in dictionary:
        print(f"Enter a New Meaning to edit [{word} : {dictionary[word]}] to:")
        new_meaning = input(">>")
        dictionary[word] = new_meaning
        saveDictionary(dictionary)
        print(f"SUCCESS: Updated to [{word} : {dictionary[word]}]")
    else:
    # otherwise throw an error and go back to the submenu of updateWord()
        print("ERROR: The word doesn't exist! impossible to update!")
        updateWord()

    # after the work of this function is done, we need to go back to the menu
    main_menu()


def main_menu():
    '''
    This function prints Main Menu:
    __________
    ENGLISH DICTIONARY APP
    __________
    1. Add a new word
    2. Find the meaning
    3. Update a word
    4. Exit
    __________
    '''
    if not os.path.exists(os.path.join("words.txt")):
        try:
            with open('words.txt', 'wb') as dictionary:
                pickle.dump({}, dictionary)
            print("SUCCESS: writing to dictionary words.txt")
        except Exception as e:
            print(f"ERROR: {e}")
    print("__________")
    print("ENGLISH DICTIONARY APP")
    print("__________")
    print("1. Add a new word")
    print("2. Find the meaning")
    print("3. Update a word")
    print("4. Exit")
    print("__________")
    # n is a way to choose items from the menu
    n = int(input(">>"))
    if n==1:
        # if n==1 call the function that asks for a new word to save
        addNewWord()
    elif n==2:
        # if n==2 call the function that asks for a word to search
        findMeaning()
    elif n==3:
        # if n==3 call the function that asks for which word to edit
        updateWord()
    elif n==4:
        # if n==4 then exit the program
        exit()
    else:
        # otherwise, show the error, and print the menu again
        print("ERROR: Please Enter a digit according to Menu.")
        main_menu()


# Script to Run

main_menu() # to initiate the program
#print(getDictionary())
#dictionary = {}
#saveDictionary(dictionary)
 
 
 
 
2) LBMS
 
 
# db.py

import sqlite3

def init_db():
    '''
    call me, i'll initiate and give you a connection and a cursor to my database
    '''
    connection = sqlite3.connect("LBMS.db")
    cursor =  connection.cursor()
    print("Database connection established successfully")
    create_table(cursor)
    return(connection, cursor)


def term_db(connection):
    '''
    give me a database connection i'll terminate it
    '''
    connection.close()
    print("Database connection de-established successfully")


def create_table(cursor):
    '''
    give me a cursor to database i'll create book table in it
    '''
    sql_command = """CREATE TABLE if not exists books (
        title varchar(60) not null,
        author varchar(30) not null,
        year INTEGER not null,
        ISBN INTEGER,
        unique (title, author, year, ISBN)
    );"""
    cursor.execute(sql_command)  


def read_table(cursor):
    '''
    give me a cursor to database, i'll read it all and send you
    '''
    sql_command = "SELECT * FROM books"
    cursor.execute(sql_command)  
    data = cursor.fetchall()
    return(data)


def add_to_table(cursor, title, author, year, isbn):
    '''
    give me a cursor to database, and a book, i'll add it to the books table
    '''
    sql_command = f"INSERT INTO books VALUES ('{title}', '{author}', {year}, {isbn});"
    try:
        cursor.execute(sql_command)
        return(0, "success")
    except sqlite3.IntegrityError as e:
        return(111, e)
    except sqlite3.OperationalError as e:
        return(112, e)


def remove_from_table(cursor, title, author, year, isbn):
    '''
    give me a cursor to database, and a book, i'll remove it.
    '''
    try:
        sql_command = f'''DELETE FROM books WHERE title="{title}";'''
        cursor.execute(sql_command)
        sql_command = f'''DELETE FROM books WHERE author="{author}";'''
        cursor.execute(sql_command)
        sql_command = f'''DELETE FROM books WHERE year="{year}";'''
        cursor.execute(sql_command)
        sql_command = f'''DELETE FROM books WHERE isbn="{isbn}";'''
        cursor.execute(sql_command)
    except:
        return(-1)


def search_from_table(cursor, title, author, year, isbn):
    '''
    give me a cursor to database, and a book, i'll search it.
    '''
    try:
        cursor.execute("SELECT * FROM books WHERE title=?", (title,))
        searched_titles = cursor.fetchall()
        cursor.execute("SELECT * FROM books WHERE author=?", (author,))
        searched_authors = cursor.fetchall()
        cursor.execute("SELECT * FROM books WHERE year=?", (year,))
        searched_years = cursor.fetchall()
        cursor.execute("SELECT * FROM books WHERE ISBN=?", (isbn,))
        searched_isbns = cursor.fetchall()
        return(searched_titles + searched_authors + searched_years + searched_isbns)
    except:
        return(-1)


def save_changes(connection):
    '''
    give me a connection to database i'll save the changes
    '''
    connection.commit()
 
# database_gui_interface

def list_all_books(listArea):
    connection, cursor = init_db()
    print("listing books")
    books = read_table(cursor)
    listArea.delete('0','end')
    for i in range(len(books)):
        listArea.insert(i,f"{books[i][0]}, {books[i][1]}, {books[i][2]}, {books[i][3]}")
    term_db(connection)


def search_book(listArea, title, author, year, isbn):
    connection, cursor = init_db()
    print("search book")
    title = title.get()
    author = author.get()
    year = year.get()
    isbn = isbn.get()
    res = search_from_table(cursor, title, author, year, isbn)
    if res==-1:
        messagebox.showerror("Error!", "something went wrong!")
    if res!=-1:
        listArea.delete('0','end')
        for i in range(len(res)):
            listArea.insert(i,res[i])  
    term_db(connection)


def add_book(title, author, year, isbn):
    connection, cursor = init_db()
    print("adding book")
    title = title.get()
    author = author.get()
    year = year.get()
    isbn = isbn.get()
    code, error = add_to_table(cursor, title, author, year, isbn)
    if code==111:
        messagebox.showerror("Error!", f"Book Already Exists!\n\nerror code: {code}\nerror:{error}")
    if code==112:
        messagebox.showerror("Error!", f"Please, fill in the input fields appropriately.\n\ntitle: string, author: string, year: integer, year: integer.\n\nerror code: {code}\nerror:{error}")
    save_changes(connection)
    term_db(connection)

def issue_book(title, author, year, isbn):
    connection, cursor = init_db()
    print("issue book")
    messagebox.showwarning("Undefined", "this function is not yet defined!")
    term_db(connection)

def delete_book(title, author, year, isbn):
    connection, cursor = init_db()
    print("delte book")
    title = title.get()
    author = author.get()
    year = year.get()
    isbn = isbn.get()
    code = remove_from_table(cursor, title, author, year, isbn)
    if code==-1:
        messagebox.showerror("Error!", "something went wrong!")
    messagebox.showinfo("Deleted!", f"Deleted the book: {title}, {author}, {year}, {isbn}")        
    save_changes(connection)
    term_db(connection)
 
# gui

from tkinter import *
from tkinter import ttk
from tkinter import messagebox

def gui():
    # 1. root
    root = Tk()
    root.title("LBMS | Library Book Management System")

    # 2. widgets
    title = StringVar()
    author = StringVar()
    year = StringVar()
    isbn = StringVar()

    title_label = Label(root, text="title:", padx=10)
    author_label = Label(root, text="author:", padx=10)
    year_label = Label(root, text="year:", padx=10)
    isbn_label = Label(root, text="isbn:", padx=10)

    title_entry = Entry(root, textvariable=title)
    author_entry = Entry(root, textvariable=author)
    year_entry = Entry(root, textvariable=year)
    isbn_entry = Entry(root, textvariable=isbn)


    scrollbar = Scrollbar(root, orient="vertical")
    listArea = Listbox(root, bg='grey', width=100, yscrollcommand=scrollbar.set)

    list_all_btn = Button(root, text="List All", command=lambda:list_all_books(listArea))
    search_btn = Button(root, text="Search", command=lambda:search_book(listArea, title, author, year, isbn))
    add_btn = Button(root, text="Add", command=lambda:add_book(title, author, year, isbn))
    issue_btn = Button(root, text="Issue", command=lambda:issue_book(title, author, year, isbn))
    del_btn = Button(root, text="delete", command=lambda:delete_book(title, author, year, isbn))
    exit_btn = Button(root, text="Exit", command=root.destroy)

    scrollbar.config(command=listArea.yview)

    title_label.grid(row=0, column=0)
    author_label.grid(row=0, column=2)
    year_label.grid(row=0, column=4)
    isbn_label.grid(row=0, column=6)


    title_entry.grid(row=0, column=1)
    author_entry.grid(row=0, column=3)
    year_entry.grid(row=0, column=5)
    isbn_entry.grid(row=0, column=7)

    listArea.grid(row=1, column=0, columnspan=8, sticky="nsew")
    scrollbar.grid(row=1, column=9, sticky="ns")

    list_all_btn.grid(row=2, column=0)
    search_btn.grid(row=2, column=1)
    add_btn.grid(row=2, column=2)
    issue_btn.grid(row=2, column=3)
    del_btn.grid(row=2, column=4)
    exit_btn.grid(row=2, column=7)

    # 3. loop
    print("GUI loop INITIATING")
    root.mainloop()
    print("GUI loop TERMINATED")
 
# run
gui()

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.