8 A non-thread-safe helper class to ease implementing singletons.
9 This should be used as a decorator -- not a metaclass -- to the
10 class that should be a singleton.
12 The decorated class can define one `__init__` function that
13 takes only the `self` argument. Also, the decorated class cannot be
14 inherited from. Other than that, there are no restrictions that apply
15 to the decorated class.
17 To get the singleton instance, use the `Instance` method. Trying
18 to use `__call__` will result in a `TypeError` being raised.
22 def __init__(self, decorated):
23 self._decorated = decorated
28 Returns the singleton instance. Upon its first call, it creates a
29 new instance of the decorated class and calls its `__init__` method.
30 On all subsequent calls, the already created instance is returned.
35 except AttributeError:
36 self._instance = self._decorated()
42 raise TypeError('Singletons must be accessed through `Instance()`.')
44 def __instancecheck__(self, inst):
45 return isinstance(inst, self._decorated)
55 return self._totalTime
61 self._startTime = time.time()
64 self._totalTime += time.time() - self._startTime