1 import threading
2
4 """
5 This class makes a thread local Singleton
6 (i.e. every threads sees a differnt singleton)
7 It also makes a different singleton for each derived class.
8 The objects behave like dictionaries and attributes/values
9 are mapped into keys/values.
10 But, while if not 'x' in a, a['x'] raises and exception a.x returns None
11 Example:
12
13 >>> a=Singleton()
14 >>> a.x=1
15 >>> b=Singleton() # same singleton as a
16 >>> print b.x
17 1
18 >>> class C(Singleton): pass
19 >>> c=C() # new singleton
20 >>> c.y=2
21 >>> d=C() # same singleton as c
22 >>> print d.x, d.y
23 None 2
24 >>> d.set_state({}) # state can be reset
25 >>> print d.x, d.y
26 None None
27 """
28 thread = threading.local()
40 - def get(self,key,value):
45 return '<Storage ' + repr(getattr(Singleton.thread,str(self.__class__))) + '>'
88
89 if __name__ == '__main__':
90 import doctest
91 doctest.testmod()
92