How to Reference List of Dicitionaries from One Class to Another?

Question:

I am currently working on an inventory system and trying to implement a class that serves as a "fast fact finer" for my database, which is managed by my Database Management class.

The csv file looks like this:

enter image description here

I have the following code:

class DataBase_Management(object):
   
   def __init__(self):
       self.result = []


   def make_dict_items(self):
       with open("Items2.csv") as fp:
           reader = csv.reader(fp)
           labels = next(reader, None)
           result = []
           for row in reader:
               if row:
                   row[0] = int(row[0])
                   row[1] = float(row[1])
                   row[2] = int(row[2])
                   pairs = zip(labels, row)
                   self.result.append(dict(pairs))
   
   def get_listofdicts(self):
       return self.result

The above class manages the list of dicts, through several methods that add items, delete items, etc (not included for simplicity to question).

I also have this class I am working on:


class DB_Fact_Finder():

    def __init__(self, management):
        self.management = management

    def return_item_num(self, item):
        items = self.management.get_listodicts()
        return items[item]["Item #"]

    def return_price(self, item):
        items = self.management.get_listodicts()
        return items[item]["Price "]
        

    def return_name(self, item):
        items = self.management.get_listodicts()
        return self.result[item]["Name"]
        

    def return_qnty(self, item):
         items = self.management.get_listodicts()
         return self.result[item]["Quantity"]

Basically, I want the DB_Fact_Finder class to be able to look at the self.result list of dictionaries defined in the management class, and be able to find specific items such as price, quantity, etc. I tried to implement this in the above code, but feel like I am missing something key to be able to reference the self.result from another class?. How would I do this?

Asked By: py_coder1019

||

Answers:

Usage: create instance of DataBase_Management, populate results list, pass to DB_Fact_Finder

db_management = DataBase_Management()
db_management.make_dict_items()
db_fact_finder = DB_Fact_Finder(db_management)

# example usage:
price_1 = db_fact_finder.return_price(0)
print(item_num)
# 5.99

Note there are some problems with the methods of DB_Fact_Finder.

This does not work:

def return_name(self, item):
        items = self.management.get_listodicts()
        return self.result[item]["Name"]

There will not be a self.results property of a DB_Fact_Finder object as you have the class defined. Same issue exists in def return_qnty

The first two methods are calling self.management correctly.

Answered By: crunker99
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.