Changeset 191
- Timestamp:
- 06/24/02 10:09:50 (6 years ago)
- Files:
-
- trunk/RBFoundation/RBFoundation/Acquisition.py (modified) (3 diffs)
- trunk/RBFoundation/RBFoundation/AttributedDict.py (modified) (1 diff)
- trunk/RBJabber/RBJabber/SubjectObserver/ProxySubject.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/RBFoundation/RBFoundation/Acquisition.py
r186 r191 40 40 41 41 class AcquisitionMixin(object): 42 """Allows for dynamic pseudo inheritence from a list of 'acquirable' objects. 43 Use sparingly, as the acquisition (both spanish and otherwise ;) is an expesive 44 propsition. Works great for a subject/observer proxy, where you want notifications 45 when values are changed.""" 46 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 47 #~ Constants / Variables / Etc. 48 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 49 42 50 __AcquirableObjects = [] 43 51 52 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 53 #~ Public Methods 54 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 55 44 56 def AddAcquirable(self, obj): 57 """Adds obj to the acquisition list, allowing for pseudo inheritance.""" 45 58 if not self.__AcquirableObjects: 46 self.__AcquirableObjects = []59 self.__AcquirableObjects = [] 47 60 self.__AcquirableObjects.append(obj) 61 62 def RemoveAcquirable(self, obj): 63 """Removes obj from the acquisition list.""" 64 self.__AcquirableObjects.remove(obj) 48 65 66 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 67 #~ Special 68 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 69 49 70 def __getattribute__(self, name): 71 """Returns attribute value from self if exists, or from an aquirable object.""" 50 72 try: 51 return object.__getattribute__(self,name)73 return super(AcquisitionMixin, self).__getattribute__(name) 52 74 except AttributeError: 53 lst = [x for x in self.__AcquirableObjects if hasattr(x, name)] 54 assert len(lst) in [0,1] 55 if lst: 56 result = getattr(lst[0], name, id(None)) 75 for x in self.__AcquirableObjects: 76 result = getattr(x, name, id(None)) 57 77 if result != id(None): 58 78 return result … … 60 80 61 81 def __setattr__(self, name, value): 82 """Sets attribute to value on self if exists, or on an aquirable object.""" 62 83 try: 63 object.__getattribute__(self,name)84 super(AcquisitionMixin, self).__getattribute__(name) 64 85 except AttributeError: 65 lst = [x for x in self.__AcquirableObjects if hasattr(x, name)]66 assert len(lst) in [0,1]67 if lst: return setattr(lst[0], name, value)68 return object.__setattr__(self,name,value)86 for x in self.__AcquirableObjects: 87 if hasattr(x, name): 88 return setattr(x, name, value) 89 return super(AcquisitionMixin, self).__setattr__(name,value) 69 90 70 91 def __delattr__(self, name): 92 """Removes attribute from self if exists, or from an aquirable object.""" 71 93 try: 72 object.__getattribute__(self,name)94 super(AcquisitionMixin, self).__getattribute__(name) 73 95 except AttributeError: 74 lst = [x for x in self.__AcquirableObjects if hasattr(x, name)]75 assert len(lst) in [0,1]76 if lst: return delattr(lst[0], name)77 return object.__delattr__(self,name)96 for x in self.__AcquirableObjects: 97 if hasattr(x, name): 98 return delattr(x, name) 99 return super(AcquisitionMixin, self).__delattr__(name) 78 100 79 101 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ … … 89 111 a.AddAcquirable(temp()) 90 112 91 assert a.v == 1 92 assert a.u == 2 93 assert a.u != a.v 94 a.u = 'new value' 95 assert a.u == 'new value' 96 a.new_val = 'a new value!' 97 assert a.new_val == 'a new value!' 113 if 1: 114 assert a.v == 1 115 assert a.u == 2 116 assert a.u != a.v 117 a.u = 'new value' 118 assert a.u == 'new value' 119 a.new_val = 'a new value!' 120 assert a.new_val == 'a new value!' 121 122 import time 123 count = 1e4 124 start = time.clock() 125 for i in xrange(count): me = i + temp.v 126 pereach = (time.clock() - start) / count 127 print "Normal:", pereach, "per", count 128 129 start = time.clock() 130 for i in xrange(count): me = i + a.v 131 pereach2 = (time.clock() - start) / count 132 print "Acquisition:", pereach2, "per", count 133 134 print "Acquisition is", pereach2/pereach, "times slower" 98 135 print "Test complete." 99 136 trunk/RBFoundation/RBFoundation/AttributedDict.py
r164 r191 102 102 except AttributeError: 103 103 pass 104 105 import time 106 class Normal(object): 107 answer = 42**2 108 normal = Normal() 109 count = 1e4 110 start = time.clock() 111 for i in xrange(count): me = i + normal.answer 112 pereach = (time.clock() - start) / count 113 print "Normal:", pereach, "per", count 114 115 start = time.clock() 116 for i in xrange(count): me = i + d.answer 117 pereach2 = (time.clock() - start) / count 118 print "Attributed Dict:", pereach2, "per", count 119 120 print "Attributed Dict is", pereach2/pereach, "times slower" trunk/RBJabber/RBJabber/SubjectObserver/ProxySubject.py
r190 r191 43 43 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 44 44 45 class ProxySubjectTriggerMixin(object, Acquisition.AcquisitionMixin): 46 __subject = None 47 def __init__(self, subject): 48 self.__subject = subject 49 self.AddAcquirable(self.__subject) 45 class ProxySubjectTriggerMixin(object): 46 _subject = None 47 48 def __init__(self, subject=None): 49 self.SetSubject(subject) 50 51 def SetSubject(self, subject): 52 self._subject = subject 50 53 51 54 def __setattr__(self, name, value): 52 55 result = self.__super.__setattr__(name, value) 53 if name != '_ ProxySubjectTriggerMixin__subject':54 self._ _subject.UpdateObserversEx({name: value})56 if name != '_subject' and self._subject: 57 self._subject.UpdateObserversEx({name: value}) 55 58 return result 56 59 57 60 def __delattr__(self, name): 58 61 result = self.__super.__delattr__(name) 59 if name != '_ ProxySubjectTriggerMixin__subject':60 self._ _subject.UpdateObserversEx({name: None})62 if name != '_subject' and self._subject: 63 self._subject.UpdateObserversEx({name: None}) 61 64 return result 62 65 … … 65 68 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 66 69 67 class ProxySubject (ProxySubjectTriggerMixin):68 def __init__(self ):69 ProxySubjectTriggerMixin.__init__(self, Subject.Subject())70 class ProxySubjectAcquisitionMixin(ProxySubjectTriggerMixin, Acquisition.AcquisitionMixin): 71 def __init__(self, subject=None): 72 ProxySubjectTriggerMixin.__init__(self, subject) 70 73 Acquisition.AcquisitionMixin.__init__(self) 71 74 72 class ProxyCategorySubject(ProxySubjectTriggerMixin): 73 def __init__(self): 74 ProxySubjectTriggerMixin.__init__(self, CategorySubject.CategorySubject()) 75 Acquisition.AcquisitionMixin.__init__(self) 75 def SetSubject(self, subject): 76 if self._subject: self.RemoveAcquirable(self._subject) 77 super(ProxySubjectAcquisitionMixin, self).SetSubject(subject) 78 if self._subject: self.AddAcquirable(self._subject) 79 80 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 81 82 class ProxySubject(ProxySubjectAcquisitionMixin): 83 def __init__(self, subject=None): 84 ProxySubjectAcquisitionMixin.__init__(self, subject or Subject.Subject()) 85 86 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 87 88 class ProxyCategorySubject(ProxySubjectAcquisitionMixin): 89 def __init__(self, subject=None): 90 ProxySubjectAcquisitionMixin.__init__(self, subject or CategorySubject.CategorySubject()) 76 91 77 92 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
