archey4/archey/singleton.py
Samuel FORESTIER 2e282fc905
Improves singleton definitions and their testing (#49)
+ Publishes some test cases for `Singleton` meta-class and `Processes` class
* Explicits that `Singleton` is an abstract meta-class
* Fixes and improves `Configuration` test cases
2019-10-08 17:37:05 +00:00

20 lines
688 B
Python

"""Simple singleton meta-class definition"""
from abc import ABCMeta as AbstractBaseMetaClass
class Singleton(AbstractBaseMetaClass):
"""
Taken from : <https://stackoverflow.com/q/6760685/10599709>
This meta-class allows us to declare `Configuration` as a singleton.
This way, we are able to import `Configuration` in multiple modules, ...
... whereas it is effectively loaded only once.
You cannot instantiate this meta-class directly.
"""
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]