79230600

Date: 2024-11-27 14:06:07
Score: 1
Natty:
Report link

Ellaborating on @sahasrara62 comment I made my config object into a singleton

class Singleton(type):
    _instances = {}

    def __call__(cls, *args, **kwargs):
        if cls not in cls._instances:
            cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
        cls._instances[cls](**kwargs)
        return cls._instances[cls]


class U2DBLiveConfig(metaclass=Singleton):

    def __init__(self, config_file_path=CONFIG_PATH):
        self.__uopy_session = None

    def __call__(self, *args, **kwargs):
        if kwargs and 'uopy_session' in kwargs:
            self.__uopy_session = kwargs['uopy_session']

Now I can use the with statement like this:

with create_uopy_session() as uopy_session, U2DBLiveConfig(uopy_session) as cfg:
    # some code...

And I can assign the session to the configuration object.

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @sahasrara62
  • Self-answer (0.5):
  • Low reputation (0.5):
Posted by: Héctor C.