Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Store a product/item in a inventory program

I'm a new programmer and new to the forum :) and right now I am working on a very simple cash register program. I was wondering if anyone has got any tips on how to store different types of products from the store in the program.
I want there to also be an option in the program to make a new item.

Is it easiest to store them in a list , or should each item have their own variable?
If the second option is preferable, is there a way to customize a variable as the user of the program when adding a new product?

Sorry if I'm being unclear, just looking for a guideline! :)

or maybe if there already is a similar thread you can just direct me there and I'll delete this one!
The product can be represented as dict:

your_product = {'product_id': 1337, 'product_name': 'FooLaser', 'qty': 42, 'price': 13.37}
You can nest data types in python. For example if your program should have more than one product.
product_database = [
    {'product_id': 1337, 'product_name': 'FooLaser1', 'qty': 42, 'price': 13.37},
    {'product_id': 1338, 'product_name': 'FooLaser2', 'qty': 42, 'price': 13.57},
    {'product_id': 1339, 'product_name': 'FooLaser3', 'qty': 42, 'price': 13.77},
    {'product_id': 1340, 'product_name': 'FooLaser4', 'qty': 42, 'price': 13.97},
# product_database is a list with dicts inside
Then a function to search by 'product_id' and return the reference to the mutable dict.
def get_product_by_product_id(pid, db):
    for product in db:
        if product['product_id'] == pid:
            return product
            # the returned product is not a copy of the dict
            # you get the reference to the object in the list back
            # and you can mutate the object
        return {}
        # just return an empty dict, to
        # get the same data type back, if the product_id
        # was not found in the list of dicts
This in action:
product_to_mutate = get_product_by_product_id(1338, product_database)
product_to_mutate['price'] = 55.4
print('Database has been changed')
In Python you have many ways to make your own data structure.
The next step is serialization.

The built-in tools we have in Python land:

I pick the json and pickle as example:
import json
import pickle

def save_json(content, output_file):
    with open(output_file, 'w') as fd:
        json.dump(content, fd)

def save_pickle(content, output_file):
    with open(output_file, 'wb') as fd:
        pickle.dump(content, fd)

def load_json(input_file):
    with open(input_file) as fd:
        return json.load(fd)

def load_pickle(input_file):
    with open(input_file, 'rb') as fd:
        return pickle.load(fd)
The difference between json and pickle is that json means javascript object notation and is represented as text.
The pickle protocol is binary python specific and incompatible between the different python versions (different protocol level).
Pickle can represent more Python objects as json, but not all objects.

Another serialization method is the csv standard. We know it from Excel.
The csv modules is also included in Python. But using csv for mutable data is not good.
This format is often used to exchange data.

Later you can use classes to simplify the handling of your data and to have a nice interface.
After later the relational databases can be very interesting for you. If you're learning
the Django-Framework you'll see very early Models, which define the database structure,
but you not using the SQL syntax to do this. This tools are called object relational mappers.
So you can do this also very easily, if you use for example SQLAlchemy for Python.
They have good tutorials.
snippsat likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die:
All humans together. We don't need politicians!

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Does PrettyTable store outside of program? Mark17 5 159 Oct-19-2019, 02:41 PM
Last Post: Mark17
  Product expression. jarrod0987 1 384 Dec-13-2018, 11:32 AM
Last Post: buran
  Python inventory system with dicts. 2skywalkers 7 643 Aug-23-2018, 05:06 PM
Last Post: ichabod801
  Python simple store inventory system. 2skywalkers 3 2,097 Aug-19-2018, 06:12 PM
Last Post: Gribouillis
  Product of maximum in first array and minimum in second Thethispointer 9 1,064 Jan-19-2018, 07:38 PM
Last Post: Thethispointer

Forum Jump:

Users browsing this thread: 1 Guest(s)