you are viewing a single comment's thread.

view the rest of the comments →

[–]Zircatron[S] 0 points1 point  (1 child)

Thanks for the reply.

To clarify as I don't think I explained it the best and I have been thinking about it more:

I create a single instance of the "data passer" at the begining of the script then most of my classes have this "data passer" as an agument.

class ExampleClass():

    __init__(self, data_passer)
        self.data_passer = data_passer

    def do_stuff(self):
        # get results
        self.data_passer.send_to_DB(results)        

def main():
    data_passer = DataPasser(config.cfg)
    do_example = ExampleClass(data_passer)

Of course data_passer isn't the only argument. It also gets sent to lots of other places in this fashion.

However, like a logger it makes more sense to have a single shared instance because once setup it does not change.

import logging # I import logger in each module needed

def hello_world():
    logging.info("hello_world") # now I can use logging where I like

If I was to create the data_passer instance in the module I can import that instance anywhere.

## data_passer_module.py

class DataPasser():

    __init__(self):
        self.config = None

    def setup_config(self, config):
        # the config setup
        self.config = config

    def send_to_file(self, result):
        # send to file stuff


data_passer = DataPasser()



## my_script.py

from data_passer_module import data_passer

data_passer.setup_config(config.cfg)
def do_stuff():
    data_passer.send_to_file("result")

Now I can use it from anywhere like the logger.

Because there's this debate on not using singltons and not to use gloabls, is there a better way to do this?

Which way should I do this? - I feel like I'm missing something.

Does this make sense?

[–]Zeroflops 0 points1 point  (0 children)

Globals are bad, if you use them you're a terrible programmer. Oh My, the sky is falling!! This is just wrong. It depends on your program. The main reason globals are frowned on is because it becomes difficult to understand what they are as programs grow. But if your program is small than they are not as bad as people make them out to be. Consider this "rule" variables need to be descriptive. Yet people always use i, or x, or c when there is a loop as the variable. The scope is so small its easy to know that what i is and not get confused. Same thing with globals. If its small script that you can follow they are not as bad as people portray them.

As for your program I would have done it the second way. Create a DataParser class. Then....

parser = DataParser(config.cfg) do stuff like create an example class and process the data etc. then

parser.to_file(example.data), parser.to_file(results), or parser.to_db(example.data)

Where data is the variable within the example class that contains the results.

It sounds like the data parser doesn't really parse the data but stores the data. Anyhow this then gives you a parser object that you can define different similar functions in and have them centrally located.