def application(environ, start_response): headers = [('Content-Type', 'text/plain; charset=utf-8')] path = environ['PATH_INFO'] params = urllib.parse.parse_qs(environ['QUERY_STRING']) #db = Simpledb('data.txt') if path == '/insert': start_response('200 OK', headers) params1 = params['key'][0] params2 = params['value'][0] Simpledb.add("data.txt", str(params1), int(params2)) return ["INSERTED".encode()] elif path == '/select': params1 = params['key'][0] print(params1) s = Simpledb.find("data.txt", str(params1)) start_response('200 OK', headers) if s != None: return ["RETRIEVED: ".encode() + str(s).encode()] else: return ['NULL'.encode()] elif path == '/delete': start_response('200 OK', headers) params1 = params['key'][0] s = Simpledb.delete("data.txt", str(params1)) return ["DELETED".encode()] elif path == '/update': params1 = params['key'][0] params2 = params['value'][0] Simpledb.update("data.txt", str(params1), int(params2)) start_response('200 OK', headers) return ["UPDATED".encode()] else: start_response('404 Not Found', headers) return ['Status 404: Resource not found'.encode()]
def application(environ, start_response): headers = [('Content-Type', 'text/plain; charset=utf-8')] path = environ['PATH_INFO'] params = urllib.parse.parse_qs(environ['QUERY_STRING']) db = Simpledb('db.txt') if path == '/insert': start_response('200 OK', headers) db.insert(params['key'][0], params['value'][0]) return ['Inserted'.encode()] elif path == '/select': s = db.select_one(params['key'][0]) start_response('200 OK', headers) if s: return [s.encode()] else: return ['NULL'.encode()] elif path == '/delete': start_response('200 OK', headers) d = db.delete(params['key'][0]) if d: return ['Deleted'.encode()] else: return ['NULL'.encode()] elif path == '/update': start_response('200 OK', headers) u = db.update(params['key'][0], params['value'][0]) if u: return ['Updated'.encode()] else: return ['NULL'.encode()] else: start_response('404 Not Found', headers) return ['Status 404: Resource not found'.encode()]
while status != 'a' and status != 'f' and status != 'd' and status != 'u' and status != 'q': status = input( 'Select one of the following actions:\n\'a\' for Adding a new contact\n\'f\' for searching for an existing contact\n\'d\' for Deleting an existing contact\n\'u\' for updating an existing contact\n\'q\' for Exiting the program' ) if status == 'a': file.key = input('What is the person\'s name?') file.value = input('And their phone number?') file.insert() print('All done!') input('Press enter to continue:') elif status == 'f': file.key = input('What is the person\'s name?') print('Here is their phone number:') print(file.select_one()) input('Press enter to continue:') elif status == 'd': file.key = input('What is this person\'s name?') file.delete() print('Consider it done!') input('Press enter to continue:') elif status == 'u': file.key = input('What is this person\'s name?') file.value = input('What is this person\s new phone nubmer') file.update() print('Yippee-Ki-Yay, it\'s done!') input('Press enter to continue:') elif status == 'q': break print('Thank you for using this program') print('Have a nice day!')
'Would you like to:\n1:Delete a phone number?\n2:Update a phone number?\n3:Add another phone number?\n4:Find a phone number?\n5:Quit the program\nEnter a number 1-5' ) choice = input() while choice != '1' and choice != '2' and choice != '3' and choice != '4' and choice != '5': print('Please type 1, 2, 3, 4, or 5.') choice = input() if choice == '1' or choice == '2' or choice == '3' or choice == '4' or choice == '5': break if choice == '1': print('Whose number would you like deleted (enter a name)') name = input() db.delete(name) print('Number is deleted.') if choice == '2': print('Whose number would you like to update?') name = input() print('What number would you like to update with?') number = input() db.update(name, number) if choice == '3': print('What number would you like added?') number = input() print('Whose number is it?') name = input() db.add(name, number) if choice == '4': print('Whose number is it?') name = input() db.find(name) if choice == '5': loop = False
command = commandUser() c = str(command) if (c == 'a'): key = input("Enter a name: ") key = key.lower() value = input("Enter the phone number(only enter digits): ") value = int(value) Simpledb.add("data.txt", key, value) elif (c == 'f'): key = input("Enter the name you would like to search for: ") key = key.lower() Simpledb.find("data.txt", key) elif (c == 'd'): key = input("Enter the name you would like to delete: ") key = key.lower() Simpledb.delete("data.txt", key) elif (c == 'u'): key = input("Enter the name you would like to search for: ") key = key.lower() Simpledb.update("data.txt", key) elif (c == 'q'): break else: print("Please enter a valid option.")
print(key.title() + "'s number is " + num) else: print(key.title() + ' is not in the directory') # When the delete command is entered, the user should be prompted for a name, which the program then searches for. # If the name is found, the name and the phone number should be deleted. If the name is not found, a message should say so. elif choice == 'd': key = input('Enter the name to be deleted: ') key_found = db.delete(key) if not key_found: print('The entered name was not found in the directory') # When the update command is entered, the user should be prompted for a name, which the program then searches for. # If the name is not found, a message should say so. # If the name is found, the user should be prompted for the new phone number, which should replace the old phone number in the database. elif choice == 'u': key = input('Enter the name to be updated: ') is_in = db.select_one(key) if is_in == None: print('The name was not found in the directory') else: value = input('Enter an updated phone number for ' + key + ' : ') db.update(key, value) elif choice == 'q': break print("* * * * * * * * * * * * * * * * * * * * * ")
def calls(choice): # If "a" is chosen, it will ask for a name and number to be added. if choice == 'a' or choice == 'A': name = input("What is the name of the person?") phonenumber = input("What is their number?") mydirectory = Simpledb(filename, name, phonenumber) mydirectory.insert() text = mydirectory.__repr__() print(text) print( "The text above shows the name, phonenumber, and filename that was added." ) # If "f" is chosen, it will ask for the name and see whether or not there is a phonenumber available to print. if choice == 'f' or choice == 'F': phonenumber = '' name = input( "What is the name of the person whose number you are trying to find?" ) v = Simpledb(filename, name, phonenumber) j = v.select_one() text = v.__repr__() if j == 'Not Found': print("Not found. We are sorry.") else: print(text) print( "Find Completed! We found the name with the number above in the file listed." ) # If "d" is chosen, it will ask for the name and decide whether or not it can be deleted. if choice == 'd' or choice == 'D': phonenumber = '' name = input( "What is the name of the person whose number you are trying to delete?" ) z = Simpledb(filename, name, phonenumber) j = z.delete() text = z.__repr__() if j == "Not Found!": print('Not found') else: print(text) print( 'Found and Deleted! We deleted this name and number from the file listed.' ) #If "u" is chosen, it will ask for the name and decide whether an updated number is possible or not. if choice == 'u' or choice == 'U': phonenumber = '' name = input( "What is the name of the person whose number you are trying to update?" ) v = Simpledb(filename, name, phonenumber) b = v.select_one() if b == "Not Found": print("Not found!") else: phonenumber = input("What is the new number of this person?") b = Simpledb(filename, name, phonenumber) b.update() text = b.__repr__() print(text) print( "Completed! It has been successfully updated with the new number " + phonenumber)