Package: singleton-classes
singleton_classes - package for different Singleton classes and metaclassesThis package contains different classes to handle singleton instances.
The following type of singleton class are available :Singleton : A single instance is availableSingletonByClass : A single instance is available per class using this patternSingletonByClassAndId : A single instance with a given ID is available per class using this patternBasic UseExample with the Singleton classfromsingleton_classesimportSingleton# =============================================================================# You just have to inherit from the Singleton class to benefit from the featureclassA(Singleton):def__init__(self,a):self.a=aa=A(8)# =====================================================================================# Once a first instance is created, you can acces it from anywhere by calling the class# with no arguemntb=A()bisa>>>True# =======================================================# Trying to create a new instance will raise an Exceptionb=A(8)>>>singleton_classes.exceptions.singleton_exception.SingletonException:<class'__main__.A'> has been called with arguments. Unauthorized operation for Singleton sub classes !Example with the SingletonByClass classfromsingleton_classesimportSingletonByClass# =============================================================================# You just have to inherit from the SingletonByClass class to benefit from the featureclassA(SingletonByClass):def__init__(self,a):self.a=aclassB(SingletonByClass):def__init__(self,a):self.a=aclassC(B):passa=A(8)# =========================================================# You can create an instance per class even for sub classesb=B(8)c=C(8)# =====================================================================================# Just like before, once a first instance is created, you can acces it from anywhere# by calling the class with no arguemnta2=A()b2=B()c2=C()a2isa>>>Trueb2isb>>>Truec2isc>>>True# =======================================================# Trying to create a new instance will raise an Exceptionc=C(8)>>>singleton_classes.exceptions.singleton_by_class_exception.SingletonByClassException:<class'__main__.C'> has been called with arguments. Unauthorized operation for SingletonByClass sub classes !Example with the SingletonByClassAndId classSometime, you just want to avoid data multiplication. You want your data to be easily accessible, but you do not want to create an new instance each time and gather the related data.For instance, you have clients for your comapany, each client has a unique ID. You want to easily access the data of client through its ID, the SingletonByClassAndId class is designed for this case.fromsingleton_classesimportSingletonByClassAndId# ====================================================================================# You have to inherit from the SingletonByClassAndId class to benefit from the feature#  * the first argument of the __init__ method must be the ID#  * don't forget to add the call to the super __init__ method with the ID !classSingleClient(SingletonByClassAndId):def__init__(self,client_id,a):super().__init__(client_id)self.a=aclassGroupClient(SingletonByClassAndId):def__init__(self,client_id,a):super().__init__(client_id)self.a=ajohn=SingleClient("John Doe",8)# ====================================================================# You can create an instance per class AND per ID even for sub classesjane=SingleClient("Jane Doe",8)company_john=GroupClient("John Doe",8)# ID is unique per class# =====================================================================================# Just like before, once a first instance is created, you can acces it from anywhere# by calling the class with no arguemntjohn2=SingleClient("John Doe")jane2=SingleClient("Jane Doe")company_john2=GroupClient("John Doe")john2isjohn>>>Truejane2isjane>>>Truecompany_john2iscompany_john>>>True# =======================================================# Trying to create a new instance will raise an Exceptionjohn3=SingleClient("John Doe",8)>>>singleton_classes.exceptions.singleton_by_class_and_id_exception.SingletonByClassAndIdException:<class'__main__.SingleClient'> has been called with more arguments than just id. Unauthorized operation for SingletonByClassAndId sub classes !Special methodFirst, for the SingletonByClassAndId, you might require to get the id from an instance.
Use the 'get_singleton_id' method for this.john.get_singleton_id()>>>JohnDoeSometime, the singleton feature is a bit to restrictive. You may want to create a new instance for some reason.Two class methods allow such a mechanism :new_unregistered_instance : creates a new instance but do not register it : If you call your class, you will still get the previous instance.new_ref_instance : creates a new instance and register it : If you call your class, you will get this precise instanceThese two methods have to be called with the same arguments as when you create an instance and are available for the three singleton classesfromsingleton_classesimportSingletonByClassclassMySingletonClass(SingletonByClass):def__init__(self,a):self.a=aa=MySingletonClass("Ref instance")a2=MySingletonClass.new_unregistered_instance("Unregistred instance")a3=MySingletonClass()a2isa>>>Falsea3isa>>>Truea2=MySingletonClass.new_ref_instance("Registred Instance")a3=MySingletonClass()a2isa>>>Falsea3isa2>>>True