Dec-18-2021, 03:56 AM
(This post was last modified: Dec-18-2021, 03:56 AM by Pedroski55.)
It is not a good idea to reinvent the wheel!
Seems to me, you need a database. I would use MySQL for this.
Python can interact with MySQL via the module pymysql.
json files are mini databases. They are a way of saving and passing on data, if MySQL seems difficult.
The code below will make a json file of all your products and product details.
You can change it to make a json file for your clients.
Then all you need is to read up on how to selectively display json file data.
Personally, I think you need a class for your products and clients.
I never use classes for my simple stuff, so I don't know much about them.
There is a guy here, deanhystad, he seems to be quite an expert on classes.
Maybe he can show you how to do this with classes.
Seems to me, you need a database. I would use MySQL for this.
Python can interact with MySQL via the module pymysql.
json files are mini databases. They are a way of saving and passing on data, if MySQL seems difficult.
The code below will make a json file of all your products and product details.
You can change it to make a json file for your clients.
Then all you need is to read up on how to selectively display json file data.
Personally, I think you need a class for your products and clients.
I never use classes for my simple stuff, so I don't know much about them.
There is a guy here, deanhystad, he seems to be quite an expert on classes.
Maybe he can show you how to do this with classes.
def myApp(): # Step 1 # get the module import json # collect data # this is a list of things we need to know about each product # need some kind of check to keep 'product_id' unique collect_data = ['product_id', 'name', 'producer','category','price', 'stock'] # save the information we collect in the list: product_data product_data = [] # a function to collect our data # this function returns a dictionary with product data # this dictionary is saved in the list: product_data def get_product_data(): product_dict = {} for info in collect_data: product_dict[info] = input(f'Please enter the {info}: ') return 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() product_data.append(answers) reply = 'maybe' elif reply == 'no': break # Step 2 # use a DICTIONARY COMPREHENSION to make the list: product_data into a dictionary product_data_dict = {i:product_data[i] for i in range(len(product_data))} # look at product_data_dict for item in product_data_dict.items(): print(json.dumps(item, indent = 4, sort_keys=True)) # Step 3 # save user_data_dict as a .json file # USE YOUR PATH mypath = '/home/pedro/temp/product_data_json.json' # open 'a' in case the file already exists with open(mypath, 'a') as json_file: json.dump(product_data_dict, json_file) # Step 4 # open the file you just saved in mypath # USE YOUR PATH 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 Then read up on how to display the contents of a json file """