olivia_finder.utilities.singleton_decorator

 1class _SingletonWrapper:
 2    """
 3    A singleton wrapper class. Its instances would be created
 4    for each decorated class.
 5    """
 6
 7    def __init__(self, cls):
 8        self.__wrapped__ = cls
 9        self._instance = None
10
11    def __call__(self, *args, **kwargs):
12        """
13        Returns a single instance of decorated class
14        
15        Parameters
16        ----------
17        
18        *args
19            Variable length argument list
20        **kwargs
21            Arbitrary keyword arguments
22        """
23        if self._instance is None:
24            self._instance = self.__wrapped__(*args, **kwargs)
25        return self._instance       
26    
27    def destroy(self):
28        """
29        Destroys the instance of decorated class
30        """
31        self._instance = None
32
33def singleton(cls):
34    """
35    A singleton decorator. Returns a wrapper objects. A call on that object
36    returns a single instance object of decorated class. Use the __wrapped__
37    attribute to access decorated class directly in unit tests
38
39    Returns
40    -------
41    _SingletonWrapper
42        A singleton wrapper class
43    """
44    return _SingletonWrapper(cls)
def singleton(cls):
34def singleton(cls):
35    """
36    A singleton decorator. Returns a wrapper objects. A call on that object
37    returns a single instance object of decorated class. Use the __wrapped__
38    attribute to access decorated class directly in unit tests
39
40    Returns
41    -------
42    _SingletonWrapper
43        A singleton wrapper class
44    """
45    return _SingletonWrapper(cls)

A singleton decorator. Returns a wrapper objects. A call on that object returns a single instance object of decorated class. Use the __wrapped__ attribute to access decorated class directly in unit tests

Returns
  • _SingletonWrapper: A singleton wrapper class