def singleton(theClass):
""" decorator for a class to make a singleton out of it """
classInstances = {}
def getInstance(*args, **kwargs):
""" creating or just return the one and only class instance.
The singleton depends on the parameters used in __init__ """
key = (theClass, args, str(kwargs))
if key not in classInstances:
classInstances[key] = theClass(*args, **kwargs)
return classInstances[key]
return getInstance
# Example
@singleton
class A:
""" test class """
def __init__(self, key=None, subkey=None):
self.key = key
self.subkey = subkey
def __repr__(self):
return "A(id=%d, %s,%s)" % (id(self), self.key, self.subkey)
def tests():
""" some basic tests """
testCases = [ (None, None), (10, 20), (30, None), (None, 30) ]
instances = set()
instance1 = None
instance2 = None
for key, subkey in testCases:
if key == None:
if subkey == None: instance1, instance2 = A(), A()
else: instance1, instance2 = A(subkey=subkey), A(subkey=subkey)
else:
if subkey == None: instance1, instance2 = A(key), A(key)
else: instance1, instance2 = A(key, subkey=subkey), A(key, subkey=subkey)
print("instance1: %-25s" % instance1, " instance2: %-25s" % instance2)
assert instance1 == instance2
assert instance1.key == key and instance1.subkey == subkey
instances.add(instance1)
assert len(instances) == len(testCases)
tests()
Diff to Previous Revision
--- revision 2 2012-04-12 14:16:50
+++ revision 3 2012-04-18 06:13:25
@@ -7,14 +7,7 @@
The singleton depends on the parameters used in __init__ """
key = (theClass, args, str(kwargs))
if key not in classInstances:
- if len(args) > 0 and len(kwargs) == 0:
- classInstances[key] = theClass(*args)
- elif len(args) == 0 and len(kwargs) > 0:
- classInstances[key] = theClass(**kwargs)
- elif len(args) > 0 and len(kwargs) > 0:
- classInstances[key] = theClass(*args, **kwargs)
- else:
- classInstances[key] = theClass()
+ classInstances[key] = theClass(*args, **kwargs)
return classInstances[key]
return getInstance
@@ -23,6 +16,7 @@
@singleton
class A:
+ """ test class """
def __init__(self, key=None, subkey=None):
self.key = key
self.subkey = subkey
@@ -31,6 +25,7 @@
return "A(id=%d, %s,%s)" % (id(self), self.key, self.subkey)
def tests():
+ """ some basic tests """
testCases = [ (None, None), (10, 20), (30, None), (None, 30) ]
instances = set()
instance1 = None
@@ -44,7 +39,7 @@
if subkey == None: instance1, instance2 = A(key), A(key)
else: instance1, instance2 = A(key, subkey=subkey), A(key, subkey=subkey)
- print(instance1, instance2)
+ print("instance1: %-25s" % instance1, " instance2: %-25s" % instance2)
assert instance1 == instance2
assert instance1.key == key and instance1.subkey == subkey
instances.add(instance1)