Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
menu while loop
#9
(Dec-18-2021, 03:56 AM)Pedroski55 Wrote: 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.

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

    """

I try to save the dictionary in a json file in the folder called project but I automatically create another file in the main folder and add the information there. I tried 3 methods and the same thing happens with everything
    if name == 'products':
        m = 'D:\python visio\proiect\clients.json'
        #m = 'D:\python visio\proiect\clients.json'
        #m = 'proiect\products.json
        with open(m, 'w') as f:
            json.dump(answer, f)

    
FIXED
Reply


Messages In This Thread
menu while loop - by 3lnyn0 - Dec-17-2021, 03:13 PM
RE: menu while loop - by BashBedlam - Dec-17-2021, 07:03 PM
RE: menu while loop - by 3lnyn0 - Dec-28-2021, 12:58 PM
RE: menu while loop - by Pedroski55 - Dec-18-2021, 03:56 AM
RE: menu while loop - by 3lnyn0 - Dec-21-2021, 07:17 PM
RE: menu while loop - by snippsat - Dec-18-2021, 10:59 AM
RE: menu while loop - by Gribouillis - Dec-19-2021, 10:59 AM
RE: menu while loop - by BashBedlam - Dec-20-2021, 03:24 AM
RE: menu while loop - by Gribouillis - Dec-20-2021, 07:09 AM
RE: menu while loop - by BashBedlam - Dec-20-2021, 09:32 PM
RE: menu while loop - by bowlofred - Dec-21-2021, 08:06 PM
RE: menu while loop - by Pedroski55 - Dec-22-2021, 04:30 AM
RE: menu while loop - by BashBedlam - Dec-28-2021, 08:04 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020