Library_Module
Tue Mar 15 2022 03:34:43 GMT+0000 (Coordinated Universal Time)
Saved by @Vishal_Kumar #python
# -- Importing some important modules ...
# from collections import OrderedDict
from logging import exception
import os
import json
from datetime import date
import time
import Student_Module as std
def clear(): return os.system('cls' if os.name in ('nt', 'dos') else 'clear')
# >> ....................................... Auxillary functions ....................................... <<
# // Function for spacing
def tab(n):
a=" "
b=str()
for i in range(n):
b=b+a
return b
# // Function to return today's date
def today_date():
dateIs = date.isoformat(date.today())
return dateIs
# // Function for returning the no. of available records of books
def serial_no():
f = open("Stationaries.txt")
ser = int()
while True:
try:
a=f.readline()
if a=="":
break
ser = ser+1
print(ser)
except:
break
f.close()
return ser+1
# // Function for printing the list of books/students
def printList(takeList,arg1):
clear()
bookName=[]
Class=[]
Bool=[]
Status=[]
Date=[]
Sn=[]
for item in takeList:
bookName.append(item['Book'])
Class.append(item['Class'])
Bool.append(item['Bool'])
Status.append(item['Status'])
Date.append(item['Date'])
Sn.append(item['SerialNo'])
if arg1 == "C" or arg1 == "D":
print(" Sn. I Book I Class I Type I Date I Status I")
for i in range(len(Sn)):
stat=Status[i]
if stat==True:
stats="Available"
elif stat==False:
stats="Unavailable"
print(f" {i}.{tab(4)}{bookName[i]}{tab(15-len(bookName[i]))}{Class[i]}{tab(9-len(str(Class[i])))}{Bool[i]}{tab(8-len(str(Bool[i])))}{tab(3)}{Date[i]} {stats}\n")
elif arg1=="P":
pass
# >> ..................................................................................................... <<
# >> ........................................... Main Functions .......................................... <<
# // Returns the list of books [returns list]
def listOfBooks():
clear()
f = open("Stationaries.txt")
def sortDateWise():
sorted_list1 = []
while True:
try:
a=f.readline()
b=json.loads(a)
sorted_list1.append(b)
except:
break
print(sorted_list1)
time.sleep(2)
return sorted_list1
def sortClassWise():
a = sortDateWise()
li = {}
sorted_list2 = []
for item in a:
dict1 = item
sr = dict1['SerialNo']
cl = dict1['Class']
li[sr] = cl
sort_orders = sorted(li.items(), key=lambda x: x[1])
# print(sort_orders)
# time.sleep(4)
for item in sort_orders:
seri = item[0]
for item2 in a:
seri2 = item2['SerialNo']
if seri2 == seri:
sorted_list2.append(item2)
print(sorted_list2)
time.sleep(2)
return sorted_list2
def sortPendingWise():
pass
print("""
Sort according to Date or Class -
\n 1.Date [D]
\n 2.Class [C]
\n 3.Pending Books[P]
""")
user_response = input("Choose any one : ")
if user_response == "d":
sorted_list = sortDateWise()
f.close()
return sorted_list,"D"
elif user_response == "c":
sorted_list = sortClassWise()
f.close()
return sorted_list,"C"
elif user_response == "p":
sorted_list = sortPendingWise()
f.close()
return sorted_list,"P"
# // Add new item
def addNewBook():
f = open("Stationaries.txt", "a")
clear()
print("Fill the data below ...\n")
a = str(input("Subject : "))
b = int(input("Class : "))
c = input("Book[b] or Copy[c] : ")
if c == "b":
bol = True
elif c == "c":
bol = False
dict_of_item = {"Book": a, "Class": b, # -- Here Bool and Status class are boolean keys in this dict_of_item
"Bool": bol, "Status":True, # -- Bool [True:Book & False:Copy] and Status[True:Availabe and False:Unavailable]
"Date": today_date(), "SerialNo": serial_no()}
str_of_dict=json.dumps(dict_of_item)
f.write(str_of_dict+'\n')
f.close()
# // If user wants to borrow any book
def borrowBook():
f=open("Stationaries.txt","r")
#.. ***********Input section************
std_name=input("Enter the student's name : ")
std_class=input("Enter its class :")
result=std.check(std_name,std_class)
d=str()
if result==True:
bk_name=input("Enter the name of the book/subject : ")
while True:
try:
a=f.readline()
if a==" ":
break
b=json.loads(a)
book_name=b['Book']
avail=b['Status']
if avail==True:
if bk_name.casefold()==book_name.casefold():
b['Status']=False
c=json.dumps(b)
d=d+c+'\n'
else:
c=json.dumps(b)
d=d+c+'\n'
else:
c=json.dumps(b)
d=d+c+'\n'
except:
break
elif result==False:
pass
fn=open("Stationaries.txt","w")
fn.write(d)
fn.close()
f.close()
# // If user wants to return any borrowed book
def returnBook():
pass
# // library function
def libraryHeader():
while True:
clear()
print("""
1.See the list of Books/Copies\n
2.Add new Book/Copy\n
3.Borrow any Book/Copy\n
4.Return the Book/Copy\n
""")
# try:
user_input=int()
user_input=int(input("Enter Your Choice [1, 2, 3, 4]: "))
if user_input == 1:
list_of_books,Wise=listOfBooks()
printList(list_of_books,Wise)
break
elif user_input == 2:
addNewBook()
break
elif user_input == 3:
borrowBook()
break
elif user_input == 4:
returnBook()
break
else:
print("Invalid Input !")
# except:
# print("Invalid Input !")
time.sleep(2)
clear()
# .. Main body of the program
if __name__ == "__main__":
clear()
This is new and update version as it uses json module instead of pickle module as it is easy , and better.



Comments