0

I have few classes and configuration is used via conf.ini file. In the test class, when I create an object of a class, conf file is always loaded for each object. Is it possible that it should be load just at start (like static in Java) or load it on demand i.e., only the conf part required is loaded. I have to build it like a module and users have to import it in their code later. How can I do it ?

Hafiz Muhammad Shafiq
  • 6,781
  • 10
  • 49
  • 92

1 Answers1

1

You can do it with some 'instance' object. So, as the example, you can use Singleton as Here: Creating a singleton in Python

class Singleton(type):
    """Realization of the pattern 'Singleton'."""

    _instances = {}

    def __call__(cls, *args, **kwargs):
        """General function which return the instance."""
        if cls not in cls._instances:
            cls._instances[cls] = \
                super(Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]

And after this write

class Config(Configuration):
    __metaclass__ = Singleton
    pass

Here Configuration - your class with config.

Ihor Voronin
  • 154
  • 7