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

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!
Reply
#2
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
    else:
        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)
print(product_to_mutate)
product_to_mutate['price'] = 55.4
print(product_to_mutate)
print('Database has been changed')
print(product_database)
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:
open()
pickle
json
sqlite3

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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information Showing trendline formula in a table per product Carlossxx 0 629 May-03-2023, 08:34 AM
Last Post: Carlossxx
  Advice for Inventory System Extra 0 1,280 Feb-18-2022, 09:25 PM
Last Post: Extra
  Remove an item from a list contained in another item in python CompleteNewb 19 5,543 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  How to add product details in exe generated by pyinstaller arex786 1 8,252 Oct-10-2021, 11:00 AM
Last Post: Sran012
  Largest product in a grid (projecteuler problem11) tragical 1 2,241 Sep-14-2020, 01:03 PM
Last Post: Gribouillis
  Communicating Roblox's Inventory API with python? 4TH4RV 1 2,028 Jun-22-2020, 10:30 AM
Last Post: snippsat
  Blending calculator from final product xerxes106 0 1,588 Dec-05-2019, 10:32 AM
Last Post: xerxes106
  Make dual vector dot-product more efficient technossomy 3 2,487 Nov-28-2019, 09:27 PM
Last Post: Gribouillis
  Does PrettyTable store outside of program? Mark17 5 2,647 Oct-19-2019, 02:41 PM
Last Post: Mark17
  What area unit the opposite ways in which to understand if an inventory in Python is Smhbugra 2 2,429 May-27-2019, 07:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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