1

I have a class instance I want to access in other modules. This class loads config values using configParser to update an class instance __dict__ attribute as per this post:

I want to access this instance in other module. The instance is only created in the main.py file where it has access to the required parameters, which come via command line arguments.

I have three files: main.py, config.py and file.py. I don't know the best way to access the instance in the file.py. I only have access to it in main.py and not other modules.

I've looked at the following answers, here and here but they don't fully answer my scenario.

#config.py
class Configuration():
    def __init__(self, *import_sections):
        #use configParser, get config for relevant sections, update self.__dict__

#main.py
from config import Configuration
conf = Configuration('general', 'dev')
# other lines of code use conf instance ... e.g. config.log_path in log setup

#file.py
#I want to use config instance like this:
class File():
    def __init__(self, conf.feed_path):
       # other code here...

Options considered:

  1. Initialise Configuration in config.py module

    In config.py after class definition I could add:

    conf = Configuration('general', 'dev')
    

    and in file.py and main.py:

    from config import conf
    

    but the general and dev variables are only found in main.py so doesn't look like it will work.

  2. Make Configuration class a function

    I could make it a function and create a module-level dictionary and import data into other modules:

    #config.py
    conf = {}
    def set_config(*import_section):
        # use configParser, update conf dictionary
        conf.update(...) 
    

    This would mean referring to it as config.conf['log_path'] for example. I'd prefer conf.log_path as it's used multiple times.

  3. Pass via other instances

    I could pass the conf instance as parameters via other class instances from main.py, even if the intermediate instances don't use it. Seems very messy.

  4. Other options?

    Can I use Configuration as an instance somehow?

Community
  • 1
  • 1
Stagg
  • 2,660
  • 5
  • 34
  • 32

1 Answers1

2

By changing your Configuration class into a Borg, you are guaranteed to get a common state from wherever you want. You can either provide initialization through a specific __init__:

#config.py
class Configuration:
    __shared_state = {}
    def __init__(self, *import_sections):
        self.__dict__ = self.__shared_state
        if not import_sections: # we are not initializing this time
            return
        #your old code verbatim

initialization is donne as usual with a c = config.Configuration('general','dev') and any call to conf = config.Configuration() will get the state that c created.

or you can provide an initialization method to avoid tampering with the shared state in the __init__:

#config.py
class Configuration:
    __shared_state = {}
    def __init__(self):
        self.__dict__ = self.__shared_state

    def import(self, *import_sections):
        #your old __init__

that way there is only one meaning to the __init__ method, which is cleaner.

In both cases, you can get the shared state, once initialized, from anywhere in your code by using config.Configuration().

301_Moved_Permanently
  • 4,007
  • 14
  • 28