Challenge

Project 1 - English Dictionary App & Library Book Management System

#!/usr/bin/env python
# coding: utf-8

# ### Project 1A: English Dictionary App

# In[1]:


import json


# In[2]:


# all required functions for Project 1A are defined here
def newWord():
""" To add new word """
word = input('Please enter a word')
meaning = input('Please provide meaning of '+word)
words[word.title()]=meaning
f = open('words.txt','w')
json.dump(words,f)
f.close()
print('Word :'+word+"Added successfully")
print('-'*50)

def printWordMeaning():
""" To print word user required """
word = input('Please enter word you want to Search ....')
if word.title() in words:
print('Word:'+word+"\n Meaning:"+words[word.title()])
else:
print('Word:'+word+"not Available in list please Add it....")
print('-'*50)

def updateWordMeaning():
""" To update Meaning of the Word """
word = input('Enter word you want to update.......')
if word.title() in words:
print('Previous Meaning :\n'+words[word.title()])
else:
print('Word not in list, We will add it as new word')
meaning = input('Enter new Meaning .......\n')
words[word.title()]=meaning
f = open('words.txt','w')
json.dump(words,f)
f.close()
print('Word :'+word+" updated successfully")
print('-'*50)

def loadDictionary():
""" To load the dictionary from word.txt
if
"""
try:
f = open('words.txt','r')
words = json.load(f)
f.close()
except:
words = dict()
return words

def selector(choose):
""" based on choose value activity perfomed """
if(choose==1):
newWord()
elif(choose==2):
printWordMeaning()
elif(choose==3):
updateWordMeaning()

def menu():
"""Menu to user and taking input from user"""

print("""Main Menu

Add a new word
Find the meaning
Update a word
Exit""")
choose = input("Enter 1,2,3,4.........")
try:
choose = int(choose)
except:
print('Please provide proper input!!!!')
choose=5
return choose


# In[3]:


#enter
print('WELCOME My Dictionary')

words = dict()
flag=False
choose = 5

while choose!=4:
if not flag :
words = loadDictionary()
flag=True
elif choose in [1,2,3]:
selector(choose)
else:
print('\nPlease select option based on below Menu!!!')

choose = menu()
print('-'*50)

# exit
print('ThankQ for using My Dictionary , COMEBACK AGAIN')


# ### Project 1B: Library Book Management System

# In[4]:


#imporing required packages

from tkinter import *
import sqlite3
import tkinter.ttk as ttk
import tkinter.messagebox as tkMessageBox
from tkinter import simpledialog


# In[9]:


#database connection
def DatabaseConnection():
""" To connet wit database and creating cursor"""
global dbcon,cursor
dbcon = sqlite3.connect('libarary.db')
cursor = dbcon.cursor()
CreateTable()


def CreateTable():
"""Creating table books"""
cursor.execute('Create Table if not exists '+
'books( ISBN varchar2(20) primary key,'+
'BookName varchar2(100) NOT NULL,'+
'Author varchar2(30) NOT NULL,'+
'Year varchar2(4) NOT NULL,'+
"Issued varchar2(1) default 'N',"+
"rollnoStudent varchar2(10) default 'NULL')")
def AddRecord():
""" To add records to the books table"""
if '' in [ISBN.get(),BookName.get(),Author.get(),Year.get()]:
text_result.config(text='Please provide all Details')

elif (not Year.get().isdigit()) and len(Year.get())!=4 :
text_result.config(text='Please provide year in 4 digits format YYYY eg:1989')

else:
cursor.execute('insert into books(ISBN,BookName,Author,Year) values(?,?,?,?)',
(
str(ISBN.get()),
str(BookName.get()),
str(Author.get()),
str(Year.get())
)
)
dbcon.commit()
ISBN.set("")
BookName.set("")
Author.set("")
Year.set("")
text_result.config(text="Record inserted successfully!!!")

def Display():
""" To display all the records available """
output.delete(* output.get_children() )
cursor.execute('select * from books')
fetch = cursor.fetchall()
for data in fetch:
#print(data)
output.insert('','end',values=data)

def Exit():
""" To exit from app"""
result = tkMessageBox.askquestion('Do You Want to exit?(y/n)',icon='warning')
#print(result)
if result=='yes':
dbcon.close()
window.destroy()

def issuebook():
""" To issue a book to student with the provide roll no"""
if '' == ISBN.get():
text_result.config(text='Please provide ISBN number of the book')
cursor.execute('select * from books where ISBN = ?', (str(ISBN.get()),))
fetch = cursor.fetchall()
if len(fetch)==0 :
text_result.config(text='book is not available with ISBN number')
elif len(fetch)>1:
text_result.config(text='more than 2 book are available delete one!!! try again')
else:
rollno = simpledialog.askstring("Input", "Enter student rollno",
parent=window)
if rollno==None or rollno.strip()=="":
text_result.config(text='Book is not issued are roll no is not provided ')
return
cursor.execute('update books set Issued=?,rollnoStudent=? where ISBN = ?',('Y',str(rollno),str(ISBN.get())))
dbcon.commit()
Display()
text_result.config(text='Book with ISBN:{} is issued to rollno :{}'.format(str(ISBN.get()),str(rollno)))


def bookreturned():
""" student returned book"""
if '' == str(ISBN.get()):
text_result.config(text='Please provide ISBN number of the book')
ISBN.set("")
cursor.execute('select * from books where ISBN = ?', (str(ISBN.get()),))
fetch = cursor.fetchall()
if len(fetch)==0 :
text_result.config(text='book is not available with ISBN number')
elif len(fetch)>1:
text_result.config(text='more than 2 book are available delete one!!! try again')
else:
rollno = fetch[0][-1]
cursor.execute('update books set Issued=?,rollnoStudent=? where ISBN = ?',('N','NULL',str(ISBN.get())))
dbcon.commit()
Display()
text_result.config(text='Book with ISBN:{} is returned by student with rollno :{}'.format(str(ISBN.get()),str(rollno)))

def filterrecords():
""" searching records based on given input """
constraints = (
str(ISBN.get()) if str(ISBN.get())!="" else '%',
str(BookName.get()) if str(BookName.get())!="" else '%',
str(Author.get()) if str(Author.get())!="" else '%',
str(Year.get()) if str(Year.get())!="" else '%'
)
if constraints.count('%')==4:
text_result.config(text='Please provide details to search !!!')
return
cursor.execute('select * from books where ISBN like ? and BookName like ? and Author like ? and Year like ?',constraints)
fetch = cursor.fetchall()
if len(fetch)==0:
output.delete(* output.get_children() )
text_result.config(text='No recordes are available with provided details ')
else:
text_result.config(text='{} recordes are available with provided details '.format(len(fetch)))
output.delete(* output.get_children() )
for data in fetch:
#print(data)
output.insert('','end',values=data)

def deleterecord():
""" Deleting recrods based on given constraints """
constraints = (
str(ISBN.get()) if str(ISBN.get())!="" else '%',
str(BookName.get()) if str(BookName.get())!="" else '%',
str(Author.get()) if str(Author.get())!="" else '%',
str(Year.get()) if str(Year.get())!="" else '%'
)
if constraints.count('%')==4:
text_result.config(text='Please provide details to featch records and delete !!!')
return
cursor.execute('select * from books where ISBN like ? and BookName like ? and Author like ? and Year like ?',constraints)
fetch = cursor.fetchall()
if len(fetch)==0:
output.delete(* output.get_children() )
text_result.config(text='No recordes are available with provided details ')
else:
filterrecords();
text_result.config(text='Records displayed in right window will get deleted')
result = tkMessageBox.askquestion('Do You Want to permentely delete?(y/n)',icon='warning')
if result=='yes':
cursor.execute('delete from books where ISBN like ? and BookName like ? and Author like ? and Year like ?',constraints)
text_result.config(text='{} records are deleted'.format(len(fetch)))
dbcon.commit()
else:
text_result.config(text='Delete operations is terminated')


# In[10]:


#tkinter window
DatabaseConnection()
window = Tk()
window.title('Library Book Management Application')
window.geometry('900x450')


header =Label(window, text='LIBRARY MANAGEMENT SYSTEM',font=('arial',25,'bold'), bg='blue', fg='White')
header.pack()
#header.config(justify='center')

#left frame for input
lf = Frame(window, bg='yellow')
lf.place(relx=.01, y=50, relwidth=0.48, relheight=0.9)
# input frame
lif = Frame(lf)
lif.place(relx=0.02,rely=0.02,relwidth=0.96,relheight=0.75)

#book title
Label(lif).grid(row=0,column=0)
booktitlelabel = Label(lif,text='Book Title:',font=('arial',10,'bold'),height=1,width=20)
booktitlelabel.grid(row=1,column=0)
BookName = StringVar()
booktitleinput = Entry(lif,textvariable=BookName,width=30)
booktitleinput.grid(row=1,column=1)
Label(lif).grid(row=2,column=0)

#Author
authorlabel = Label(lif,text='Author',font=('arial',10,'bold'),height=1,width=20)
authorlabel.grid(row=3,column=0)
Author = StringVar()
authorinput = Entry(lif,textvariable=Author,width=30)
authorinput.grid(row=3,column=1)
Label(lif).grid(row=4,column=0)

#year
yearlabel = Label(lif,text='Year',font=('arial',10,'bold'),height=1,width=20)
yearlabel.grid(row=5,column=0)
Year = StringVar()
yearinput = Entry(lif,textvariable=Year,width=30)
yearinput.grid(row=5,column=1)
Label(lif).grid(row=6,column=0)

#ISBN
ISBNlabel = Label(lif,text='ISBN',font=('arial',10,'bold'),height=1,width=20)
ISBNlabel.grid(row=7,column=0)
ISBN = StringVar()
ISBNinput = Entry(lif,textvariable=ISBN,width=30)
ISBNinput.grid(row=7,column=1)
Label(lif).grid(row=8,column=0)

#output Frame
lof = Frame(lf)
lof.place(relx=0.02,rely=0.70,relwidth=0.96,relheight=0.13)
text_result = Label(lof)
text_result.pack()

#buttons frame
lbf = Frame(lf)
lbf.place(relx=0.02,rely=0.88,relwidth=0.96,relheight=0.1)
display = Button(lbf,text='Display',width=7,command=Display)
display.grid(row=0,column=0)
search = Button(lbf,text='Search',width=7,command=filterrecords)
search.grid(row=0,column=1)
add = Button(lbf,text='Add',width=7,command=AddRecord)
add.grid(row=0,column=2)
issue = Button(lbf,text='Issue',width=7,command=issuebook)
issue.grid(row=0,column=3)
returned = Button(lbf,text='Return',width=7,command=bookreturned)
returned.grid(row=0,column=4)
delete_ = Button(lbf,text='Delete',width=7,command=deleterecord)
delete_.grid(row=0,column=5)
exit_ = Button(lbf,text='Exit',command=Exit,width=7)
exit_.grid(row=0,column=6)

#right frame for output
rf= Frame(window, bg='blue')
rf.place(relx=.51, y=60, relwidth=0.48, relheight=0.75)

#output frame
output = ttk.Treeview(rf,columns=('ISBN','Book Title','Author','Year','Issued','RollNo'))
output.heading('ISBN',text='ISBN',anchor=W)
output.heading('Book Title',text='Book Title',anchor=W)
output.heading('Author',text='Author',anchor=W)
output.heading('Year',text='Year',anchor=W)
output.heading('Issued',text='Issued',anchor=W)
output.heading('RollNo',text='RollNo',anchor=W)

output.column('#0',stretch=NO,minwidth='0',width=0)
output.column('#1',stretch=NO,minwidth='0',width=100)
output.column('#2',stretch=NO,minwidth='0',width=150)
output.column('#3',stretch=NO,minwidth='0',width=100)
output.column('#4',stretch=NO,minwidth='0',width=50)
output.column('#5',stretch=NO,minwidth='0',width=50)
output.column('#6',stretch=NO,minwidth='0',width=100)
scrollbary = Scrollbar(rf,orient=VERTICAL,command=output.yview)
scrollbary.pack(side=RIGHT,fill=Y)
scrollbarx = Scrollbar(rf,orient=HORIZONTAL,command=output.xview)
scrollbarx.pack(side=BOTTOM,fill=X)
output.pack(expand=YES)
window.mainloop()
dbcon.close()


#

# In[ ]:

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.