Coverage for src/susi/utils/annotators/singleton.py: 73%
15 statements
« prev ^ index » next coverage.py v7.5.0, created at 2025-08-22 09:20 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2025-08-22 09:20 +0000
1"""
2Python annotator to create singletons
4Usage:
5<pre>
6@singleton
7class MyCass:
8 pass
9</pre>
11@author hoelken
12"""
15def singleton(clazz):
16 """Singleton annotator ensures the annotated class is a singleton"""
18 class ClassW(clazz):
19 """Creates a new sealed class from the object to create."""
20 _instance = None
22 def __new__(cls, *args, **kwargs):
23 if ClassW._instance is None:
24 ClassW._instance = super(ClassW, cls).__new__(clazz, *args, **kwargs)
25 ClassW._instance._sealed = False
27 return ClassW._instance
29 def __init__(self, *args, **kwargs):
30 if self._sealed:
31 return
33 super(ClassW, self).__init__(*args, **kwargs)
34 self._sealed = True
36 ClassW.__name__ = clazz.__name__
37 return ClassW