Return all objects from active memory in Python without using the garbage collector?

180 Views Asked by At

I have been working with large data sets on some open source software I've been working on (wxStocks on github). I am analyzing stocks, and since all stocks are loaded into active memory when the program starts, I see no reason to use a database structure.

However, it's been challenging to organize the files due to my need to use a global variable list that holds all the stocks so I can easily return a stock via it's ticker string.

I have come up with a solution via the garbage collector:

def return_stock_from_active_memory(ticker):
    ticker = ticker.upper()
    for obj in gc.get_objects():
        if type(obj) is Stock:
            if obj.symbol == ticker:
                return obj

I have been informed from more experienced python programmers that I am playing with fire by even importing the garbage collector in the first place, and that it is considered extremely bad form, still, this and another function I've written that returns all stock objects are extremely useful.

Is the function above an acceptable way to find an object in active memory via a unique attribute string? If not, how should I go about finding objects while moving between different files without creating an infinite loop importing variables into each respective file.

0

There are 0 best solutions below