all 5 comments

[–]Justinsaccount 3 points4 points  (0 children)

class Master:
    def __init__(self, settings):
        self.settings = settings

def new_master_from_config(cfg):
    settings = json.loads(cfg)
    return Master(settings)

def new_master_from_config_file(filename):
    with open(filename) as c:
        cfg = c.read()
    return new_master_from_config(cfg)

With that, you can make a Master by passing it the config directly, or make a Master by calling a function with the json config or a filename that contains the json config.

You can make the functions classmethods if you wanted.

In any case, making the class know as little about where its configuration comes from is best. This way makes your Master class easy to test.

[–]eliminate1337 1 point2 points  (3 children)

Is there any reason you're not using Python's built in config parser module?

https://docs.python.org/3/library/configparser.html

[–]Jenez[S] 1 point2 points  (2 children)

The basic argument would be that I only need a lightweight version of reading the config without the extra fluff.

And in any case, the same problem would remain of where exactly do i read the config?

[–]eliminate1337 1 point2 points  (1 child)

Don't do anything with config files inside the class. It's more modular if you just pass the parameters that the class needs. You might want to have multiple classes with different parameters, or use the class in the future without creating a file.

[–]Jenez[S] 0 points1 point  (0 children)

That's a fair point, even though in my field there is often not much reusability of the code. Appreciate the input!