Question: Pycharm programming! The assignment is to extend each basic backend CRUD function with the functionality that it first read the data from a file and
Pycharm programming!
The assignment is to extend each basic backend CRUD function with the functionality that it first read the data from a file and create the inventory list. After the function is done doing its operation (for instance, inserting a new item of insert-time), it saves the inventory list back to the file. All CRUD function should access and share the same inventory file. Note, that the test program in the def main() function should work unchanged. You can store the data to a simple text file using a comma-separated-value format or to an excel-based file with the appropriate Python file I/O reader/writer. Deliverables ..
A) an insert new record statement, B) a update record statement and C) a delete record statement. .
Basic backend pycharm file:
import mvc_exceptions as mvc_exc item_type = "product" items = list() def create_item(name, price, quantity): # reference global list of items global items # search first if that item already exists results = list(filter(lambda x: x['name'] == name, items)) #if we find an existing item with the name, we raise an exception if results: raise mvc_exc.ItemAlreadyStored('"{}" already stored!'.format(name)) #if not, we append the item to the dictionary else: items.append({'name': name, 'price': price, 'quantity': quantity}) # bulk create times def create_items(app_items): global items items = app_items # read a particular item def read_item(name): global items myitems = list(filter(lambda x: x['name'] == name, items)) if myitems: return myitems[0] else: raise mvc_exc.ItemNotStored( 'Can\'t read "{}" because it\'s not stored'.format(name)) # read all items def read_items(): global items return [item for item in items] def update_item(name, price, quantity): global items # Python 3.x removed tuple parameters unpacking (PEP 3113), so we have to do it manually (i_x is a tuple, idxs_items is a list of tuples) idxs_items = list( filter(lambda i_x: i_x[1]['name'] == name, enumerate(items))) if idxs_items: i, item_to_update = idxs_items[0][0], idxs_items[0][1] items[i] = {'name': name, 'price': price, 'quantity': quantity} else: raise mvc_exc.ItemNotStored( 'Can\'t update "{}" because it\'s not stored'.format(name)) def delete_item(name): global items # Python 3.x removed tuple parameters unpacking (PEP 3113), so we have to do it manually (i_x is a tuple, idxs_items is a list of tuples) idxs_items = list( filter(lambda i_x: i_x[1]['name'] == name, enumerate(items))) #print("index items", idxs_items) if idxs_items: i, item_to_delete = idxs_items[0][0], idxs_items[0][1] #print("idxs_items[0][0], idxs_items[0][1]: ", idxs_items[0][0], " item in list, value:", idxs_items[0][1]) del items[i] else: raise mvc_exc.ItemNotStored( 'Can\'t delete "{}" because it\'s not stored'.format(name)) ################## testing the basic backend functions ################# def main(): my_items = [ {'name': 'bread', 'price': 2.5, 'quantity': 20}, {'name': 'milk', 'price': 4.0, 'quantity': 10}, {'name': 'eggs', 'price': 4.0, 'quantity': 5}, ] # CREATE create_items(my_items) #create_item('wine', price=10.0, quantity=15) # if we try to re-create an object we get an ItemAlreadyStored exception try: create_item('wine', price=10.0, quantity=10) except Exception as e: print(e) exit() # READ print('READ items') print(read_items()) # if we try to read an object not stored we get an ItemNotStored exception # print('READ meat') # print(read_item('meat')) print('READ bread') print(read_item('bread')) # UPDATE print('UPDATE bread') update_item('bread', price=2.0, quantity=30) print(read_item('bread')) # if we try to update an object not stored we get an ItemNotStored exception # print('UPDATE chocolate') # update_item('chocolate', price=10.0, quantity=20) # DELETE print('DELETE wine') delete_item('wine') # if we try to delete an object not stored we get an ItemNotStored exception # print('DELETE chocolate') # delete_item('chocolate') print('READ items') print(read_items()) if __name__ == '__main__': main()
mvc_exceptions file:
class ItemAlreadyStored(Exception): pass class ItemNotStored(Exception): pass
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
