Package web2py :: Package gluon :: Module thread_local_singleton
[hide private]
[frames] | no frames]

Source Code for Module web2py.gluon.thread_local_singleton

 1  import threading 
 2   
3 -class Singleton(dict):
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()
29 - def __init__(self):
30 if not hasattr(Singleton.thread,str(self.__class__)): 31 setattr(Singleton.thread,str(self.__class__),{})
32 - def __getitem__(self,key):
33 return getattr(Singleton.thread,str(self.__class__))[key]
34 - def __setitem__(self,key,value):
35 getattr(Singleton.thread,str(self.__class__))[key]=value
36 - def __setattr__(self,key,value):
37 getattr(Singleton.thread,str(self.__class__))[key]=value
38 - def __delattr__(self,key):
39 del getattr(Singleton.thread,str(self.__class__))[key]
40 - def get(self,key,value):
41 return getattr(Singleton.thread,str(self.__class__)).get(key,value)
42 - def __getattr__(self,key):
43 return getattr(Singleton.thread,str(self.__class__)).get(key,None)
44 - def __repr__(self):
45 return '<Storage ' + repr(getattr(Singleton.thread,str(self.__class__))) + '>'
46 - def __str__(self):
47 return str(getattr(Singleton.thread,str(self.__class__)))
48 - def __getstate__(self):
49 return getattr(Singleton.thread,str(self.__class__))
50 - def __setstate__(self, value):
51 setattr(Singleton.thread,str(self.__class__),value)
52 - def __cmp__(self,other):
53 return 0
54 - def __contains__(self,value):
55 return value in getattr(Singleton.thread,str(self.__class__))
56 - def __hash__(self):
57 return hash(getattr(Singleton.thread,str(self.__class__)))
58 - def __len__(self):
59 return len(getattr(Singleton.thread,str(self.__class__)))
60 - def has_key(self,key):
61 return key in self
62 - def keys(self):
63 return getattr(Singleton.thread,str(self.__class__)).keys()
64 - def values(self):
65 return getattr(Singleton.thread,str(self.__class__)).values()
66 - def items(self):
67 return getattr(Singleton.thread,str(self.__class__)).items()
68 - def iterkeys(self):
69 return getattr(Singleton.thread,str(self.__class__)).iterkeys()
70 - def itervalues(self):
71 return getattr(Singleton.thread,str(self.__class__)).itervalues()
72 - def iteritems(self):
73 return getattr(Singleton.thread,str(self.__class__)).iteritems()
74 - def update(self,*a,**b):
75 return getattr(Singleton.thread,str(self.__class__)).update(*a,**b)
76 - def popitem(self):
77 return getattr(Singleton.thread,str(self.__class__)).popitem()
78 - def clear(self):
79 return getattr(Singleton.thread,str(self.__class__)).clear()
80 - def copy(self):
81 return getattr(Singleton.thread,str(self.__class__)).copy()
82 - def __iter__(self):
83 return getattr(Singleton.thread,str(self.__class__)).__iter__()
84 - def get_state(self):
85 return getattr(Singleton.thread,str(self.__class__))
86 - def set_state(self,storage):
87 setattr(Singleton.thread,str(self.__class__),storage)
88 89 if __name__ == '__main__': 90 import doctest 91 doctest.testmod() 92