Dec-22-2021, 04:30 AM
I changed myApp() to only accept unique product ids.
You should be able to copy and paste myApp() directly in your Python shell, like Idle, then just write myApp() in your shell and press enter.
You just need to change mypath for your path.
You should be able to copy and paste myApp() directly in your Python shell, like Idle, then just write myApp() in your shell and press enter.
You just need to change mypath for your path.
def myApp(): # Step 1 # get the modules import json from pathlib import Path """ When you load the .json file with data = json.load(f) you have a dictionary of dictionaries the key is the product id and the value is another dictionary with product info """ # you can use a list here and give the user the chance to choose the path he or she wants mypath = '/home/pedro/temp/product_data_json.json' # check if the json you want exists chk_file = Path(mypath) if chk_file.is_file(): print(mypath, 'exists, so open it') with open(mypath) as f: data = json.load(f) else: print("File does not exist, so make data an empty dictionary!") data = {} # show the data so far for item in data.items(): print(item) if len(data) == 0: print('No data yet!') # collect data # this is a list of things we need to know about each product collect_data = ['name', 'producer','category','price', 'stock'] # a function to collect our data # this function appends product data to the dictionary 'data' def get_product_data(): product_dict = {} print('First get product_id and check it ... ') product_id = input('Please enter the product_id: ') # check if this key exists, if so, don't accept it # the product_id must be unique, so use the dict.get(key) method while not data.get(product_id) == None: print('This product id is already in use, try again.') product_id = input('Please enter the product_id: ') # now we have a unique product_id for info in collect_data: product_dict[info] = input(f'Please enter the {info}: ') data[product_id] = product_dict replies = ['yes', 'no'] reply = 'X' # still need a way to keep the product ids unique!! while not reply in replies: print('Do you want to enter a product?') reply = input('Enter yes to continue, enter no to stop ') if reply == 'yes': answers = get_product_data() reply = 'maybe' elif reply == 'no': break for key in data.keys(): print('Product ID is', key) print('Product is:', data[key]['name']) print('Product category is:', data[key]['category']) # save the data dictionary as a .json file # USE YOUR PATH # open 'w' and overwrite the existing file print('Now saving the product data, run myApp() again to add more products later ... ') with open(mypath, 'w') as json_file: json.dump(data, json_file) print('data saved to', mypath) # open the file you just saved in mypath # USE YOUR PATH print('Now reopening', mypath, 'to have a look at the data ... ') with open(mypath) as f: data = json.load(f) print('data is ' + str(len(data)) + ' entries long') # look at the content of '/home/pedro/winter2020/20PY/json/user_data_json.json' for item in data.items(): print(json.dumps(item, indent = 4, sort_keys=True)) """ You can open and append to this json file any time Make another json file for clients """