Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Codes are provided, just want to implement the separate 6 files below in the main.py If the following is my current python code: main.py: import

Codes are provided, just want to implement the separate 6 files below in the main.py

If the following is my current python code:

main.py:

import pickledb from datetime import date import sqlite3 def DictName(Part1): #get current date today = date.today() # mm/dd/YY d1 = today.strftime("%mm%dd%Y") return str(Part1+'%%'+d1) def RemoveAll(): #remove all data from key value store db db.deldb() db = pickledb.load('Student.db', False) def print_Menu(bHasStudentInfo): #print menu #Add/edit student if(bHasStudentInfo): print("A) Edit Student Info") else: print("A) Add Student Info") #display student info print("B) Display Student Info") #save student info to long term db print("C) Register Student") #display all reg students print("D) Display All Register Student") print("Q) Quit") def getMenuOption(): option = str(input("Please enter in your option...")) option = option.upper() while ((option  "D") and (option != "Q") ): print_Menu() option = str(input("Please enter in your option...")) option = option.upper() return option def AddStudent(bStudentInfoExists,Name,Age,GPA,CellPh): if (bStudentInfoExists): print("Current Name:", Name) # prompt and get new name print("Name:") Name = GetValidString() if (not bStudentInfoExists): #create key value db if required db.dcreate(DictName('Student')) db.set(DictName('StudentExists'),1) # add name to dictionary in key value db db.dadd(DictName('Student'),('Name','test')) if (bStudentInfoExists): print("Current Age:", Age) # prompt and get new Age Age = GetValidAge() # add age to dictionary in key value db db.dadd(DictName('Student'), ('Age',Age)) if (bStudentInfoExists): print("Current GPA:", GPA) GPA = GetValidGpa() db.dadd(DictName('Student'), ('GPA',GPA)) if (bStudentInfoExists): print("Current Cell Phone Num:", CellPh) print("Cell Phone Num:") CellPh = GetValidString() db.dadd(DictName('Student'), ('CellPh',CellPh)) #set flag as now student info exists bStudentInfoExists = True return bStudentInfoExists,Name,Age,GPA,CellPh def GetValidString(): val = input("Enter in a value") val = val.strip() while (len(val)120)): val = int(input("Enter in an age")) return val def GetValidGpa(): val = float(input("Enter in a gpa")) while ((val4.2)): val = float(input("Enter in a gpa")) return val def DisplayStudent(Name,Age,GPA,CellPh): #display the student information print("Name:",Name) print("Age:",Age) print("GPA:",GPA) print("Cell Phone Num:",CellPh) def LoadStudent(): #load student from key value store Name=db.dget(DictName('Student'),'Name') Age=db.dget(DictName('Student'),'Age') GPA=db.dget(DictName('Student'),'GPA') CellPh=db.dget(DictName('Student'),'CellPh') #return student info return Name,Age,GPA,CellPh def RegStudent(Name,Age,GPA,CellPh): #connect to sqlite db conn = sqlite3.connect('RegData.db') #get cursor cur = conn.cursor() #insert new record sql_insert_query = """Insert Into Student ( 'StudentName','Age','GPA','CellPhone') Values (?, ?, ?, ?);""" cur.execute(sql_insert_query, (Name,Age,GPA,CellPh)) conn.commit() conn.close() RemoveAll() def DisplayAllRegStudents(): #connect to sqlite db conn = sqlite3.connect('RegData.db') cur = conn.cursor() #get all record from Student table cur.execute('''Select * from Student;''') print(f"{'ID':8s}",f"{'Name':20s}",f"{'Age':5s}", f"{'GPA':7s}", f"{'Cell Phone':20s}") for r in cur.fetchall(): print(f"{str(r[0]):8s}",f"{r[1]:20s}",f"{str(r[2]):5s}", f"{str(r[3]):7s}", f"{r[4]:20s}") # close database connection conn.close() def mainApp(bStudentInfoExists): option ="" #init student attributes if (not bStudentInfoExists): #if student not in key value db #set to default values Name="" Age=0 GPA = 0.0 CellPh="" else: #if student is in key value db #set to saved values Name, Age, GPA, CellPh = LoadStudent() RemoveAll() while (option != "Q"): #show menu print_Menu(bStudentInfoExists) #get option from user option = getMenuOption() if (option =="A"): #If option A chosen #add/edit student bStudentInfoExists, Name, Age, GPA, CellPh=AddStudent(bStudentInfoExists,Name,Age,GPA,CellPh) elif (option =="B"): #If option B chosen if (bStudentInfoExists == False): # If student does not exist #display err msg print("You must add a student first.") print("") else: #otherwise display student info DisplayStudent(Name,Age,GPA,CellPh) elif (option =="C"): #If option C chosen if (bStudentInfoExists == False): # If student does not exist #display err msg print("You must add a student first.") print("") else: #otherwise display student info RegStudent(Name,Age,GPA,CellPh) #reset flag bStudentInfoExists = False elif (option =="D"): DisplayAllRegStudents() return bStudentInfoExists if __name__ == '__main__': #check to see if there is student information #in the key value db if (db.get(DictName('StudentExists'))): if (db.dget(DictName('Student'),'Name')): bStudentInfoExists = True else: bStudentInfoExists = False else: bStudentInfoExists = False bStudentInfoExists = mainApp(bStudentInfoExists) #if student information was created #save the key value db if(bStudentInfoExists): db.dump() 

Now, I want to Properly implement an application with a Key Value Database that incorporates the design principles of: (TTL) keys, Atomic Aggregates, Emulating Tables, Enumerable Keys, Aggregates, and Indexes.

Directions:

You must implement the following 6 features in our main.py provided above (we're not supposed to have 6 different py files)

TTL.py:

import pickledb from datetime import date,timedelta db = pickledb.load('test.db', False) def genKey(stdNum): today = date.today() # mm/dd/YY d1 = today.strftime("%mm%dd%Y") return str('Student'+':'+d1+':'+stdNum) #student 1 stdNum = input("Enter in student number") db.dcreate(genKey(stdNum)) name = input("Enter in your name") age = int(input("Enter in your age")) gpa = float(input("Enter in your gpa")) db.dadd(genKey(stdNum),('stu-name',name)) db.dadd(genKey(stdNum),('stu-age',age)) db.dadd(genKey(stdNum),('stu-gpa',gpa)) #student 2 stdNum = input("Enter in student number") db.dcreate(genKey(stdNum)) name = input("Enter in your name") age = int(input("Enter in your age")) gpa = float(input("Enter in your gpa")) db.dadd(genKey(stdNum),('stu-name',name)) db.dadd(genKey(stdNum),('stu-age',age)) db.dadd(genKey(stdNum),('stu-gpa',gpa)) #show all print(db.getall()) #find a student stdNum = input("Enter in student number") if (db.exists(genKey(stdNum))): print(db.dgetall(genKey(stdNum))) #Delete students past time #get date 5 days ago day = date.today() + timedelta(days=-5) print(day) #Locate all key value dict stores with this # date and remove them using drem command db.dump() 

Atomic aggregates.py:

import pickledb from datetime import date,timedelta db = pickledb.load('test.db', False) def genKey(stdNum): #get current date today = date.today() d1 = today.strftime("%mm%dd%Y") return str('Student'+':'+d1+':'+stdNum) #student 1 stdNum = input("Enter in student number") name = input("Enter in your name") age = int(input("Enter in your age")) gpa = float(input("Enter in your gpa")) Record = { 'stu-name':name, 'stu-age':age, 'stu-gpa':gpa } db.set(genKey(stdNum),Record) #student 2 stdNum = input("Enter in student number") name = input("Enter in your name") age = int(input("Enter in your age")) gpa = float(input("Enter in your gpa")) Record = { 'stu-name':name, 'stu-age':age, 'stu-gpa':gpa } db.set(genKey(stdNum),Record) #show all print(db.getall()) #find a student stdNum = input("Enter in student number") if (db.exists(genKey(stdNum))): print(db.get(genKey(stdNum))) db.dump() 

Emulating tables.py:

image text in transcribed

Enumerable keys.py:

import pickledb from datetime import date,timedelta db = pickledb.load('test.db', False) def getMaxId(): stdNum = 1 while (db.exists(genKey(str(stdNum)))): stdNum+=1 return str(stdNum) def genKey(stdNum): today = date.today() d1 = today.strftime("%mm%dd%Y") return str('Student'+':'+d1+':'+stdNum) def setStudent(name,age, gpa): Record = { 'stu-name': name, 'stu-age': age, 'stu-gpa': gpa } stdNum = getMaxId() db.set(genKey(stdNum), Record) def getStudentAttrib(stdNum): if (db.exists(genKey(stdNum))): record =db.get(genKey(stdNum)) return(record['stu-name'],record['stu-age'],record['stu-gpa']) else: return None def getStudentDict(stdNum): if (db.exists(genKey(stdNum))): return(db.get(genKey(stdNum))) else: return None print(db.getall()) #std 1 name = input("Enter in your name") age = int(input("Enter in your age")) gpa = float(input("Enter in your gpa")) setStudent(name,age, gpa) #std 2 name = input("Enter in your name") age = int(input("Enter in your age")) gpa = float(input("Enter in your gpa")) setStudent(name,age, gpa) print(db.getall()) #find a student stdNum = input("Enter in student number") if (db.exists(genKey(stdNum))): print(getStudentDict(stdNum)) name,age,gpa = getStudentAttrib(stdNum) print(name) print(age) print(gpa) db.dump() 

Aggregates.py:

import pickledb from datetime import date,timedelta db = pickledb.load('test.db', False) def getMaxId(): stdNum = 1 while (db.exists(genKey(str(stdNum)))): stdNum+=1 #print(stdNum) return str(stdNum) def genKey(stdNum): today = date.today() d1 = today.strftime("%mm%dd%Y") return str('Student'+':'+d1+':'+stdNum) def setStudentNonSenior(name,age, gpa, type): setStudent(name, age, gpa, type, None) def setStudent(name,age, gpa, type, expGradSem): if (type=='Senior'): Record = { 'stu-type': type, 'stu-name': name, 'stu-age': age, 'stu-gpa': gpa, 'stu-expGrad':expGradSem } else: Record = { 'stu-type': type, 'stu-name': name, 'stu-age': age, 'stu-gpa': gpa } stdNum = getMaxId() db.set(genKey(stdNum), Record) def getStudentAttrib(stdNum): if (db.exists(genKey(stdNum))): record =db.get(genKey(stdNum)) if (record['stu-type'] == 'Senior'): return (record['stu-type'], record['stu-name'], record['stu-age'], record['stu-gpa'],record['stu-expGrad']) else: return (record['stu-type'], record['stu-name'], record['stu-age'], record['stu-gpa'], None) else: return None def getStudentDict(stdNum): if (db.exists(genKey(stdNum))): return(db.get(genKey(stdNum))) else: return None #show all print(db.getall()) #student 1 name = input("Enter in your name") age = int(input("Enter in your age")) gpa = float(input("Enter in your gpa")) setStudent(name,age, gpa,'Senior','FA2023') #student 2 name = input("Enter in your name") age = int(input("Enter in your age")) gpa = float(input("Enter in your gpa")) setStudentNonSenior(name,age, gpa,'Freshman') #show all print(db.getall()) #find a student stdNum = input("Enter in student number") if (db.exists(genKey(stdNum))): print(getStudentDict(stdNum)) type,name,age,gpa,expGrad = getStudentAttrib(stdNum) print(type) print(name) print(age) print(gpa) if (expGrad!=None): print(expGrad) db.dump() 

Indexes.py:

image text in transcribed 

image text in transcribedimage text in transcribed

Indexes.py: import pickledb from datetime import datectimedelta db = pickledb.load ('test db', False) def getMaxId (): stdNum =1 while (db.exists (genkey (str (stdNum)))): stdNum +=1 return str (stdNum) def genkey (stdNum) : \#get current date today = date.today () \# mm/dd/YY return str(Student+:+d1+:+stdNum) def setstudentNonsenior(name,age, gpa, type): setstudent (name, age, gea, type, None) def setstudent (name,age, gea, type, expGradsem): if (type=='Senior') : Record ={ 'stu-type': type, 'stu-name': name, 'stu-age': age, 'stu-gpa': gea, 'stu-expGrad':expGradsem \} else: Record ={ 'stu-type': type, 'stu-name': name. 'stuu-age': age, 'stu-gpa': gea \} dbinreatel ('Seniors') \#student 1 name = input ("Enter in your name") gpa = float (input ("Enter in your gpa")) setStudent (name, age, gpa, 'Senior', 'FA2023') \#student 2 name = input ("Enter in your name") age = int(input ("Enter in your age")) gpa = float (input ("Enter in your gea")) setstudentNonsenior (name, age, gpa, 'Freshman') \#show all print (db.getali ()) Listseniors() db dump ()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

What is the SS total ?

Answered: 1 week ago