Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading