Changeset 114
- Timestamp:
- 12/04/07 01:12:36 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/Twisted-Goodies/trunk/twisted_goodies/pybywire/params.py
r109 r114 79 79 """ 80 80 __metaclass__ = ParaMeta 81 81 82 82 name = None 83 83 paramNames, keyAttrs = [], {} … … 102 102 self.cache = {} 103 103 return self.cache 104 if name == 'registry': 105 return ParaMeta.registry 104 106 raise AttributeError("No attribute '%s'" % name) 105 107 … … 112 114 """ 113 115 Sets the attribute I{name} to the supplied I{value}, clearing the cache 114 if the attribute is a parameter and the value is different.116 if the attribute is a parameter. 115 117 """ 116 if name in self.paramNames: 117 if getattr(self, name, None) != value: 118 self.cache.clear() 118 if name in self.paramNames or name in self.keyAttrs: 119 self.cache.clear() 119 120 object.__setattr__(self, name, value) 120 121 … … 138 139 value = paramVector[k] 139 140 setattr(self, name, value) 140 141 def key(self, *args):142 """143 Returns a key that is based on the hashes of my key attributes and any144 arguments supplied. The arguments must be hashable directly, but the145 key attributes are hashed with some recursion as needed and can be146 lists or dicts.147 """148 def superHash(x):149 if not isinstance(x, (list, tuple, dict)):150 return hash(x)151 if isinstance(x, dict):152 x = x.items()153 return sum([superHash(y) for y in x])154 155 keys = self.keyAttrs.keys(); keys.sort()156 return hash(args) + superHash([getattr(self, x) for x in keys])157 141 158 142 #--- Jelly/Unjelly API ---------------------------------------------------- projects/Twisted-Goodies/trunk/twisted_goodies/pybywire/test/test_pack.py
r101 r114 103 103 self.thingy = self.Thingy() 104 104 105 def _unpack(self, X): 106 return list(pack.Unpacker(X)) 107 105 108 def test_wrapsFunction(self): 106 109 substitute = pack.packwrap(lambda x: 2*x) 107 self.failUnlessEqual(s ubstitute(10), 20)110 self.failUnlessEqual(self._unpack(substitute(10)), [20]) 108 111 109 112 def test_wrapsMethod(self): 110 113 result = pack.packwrap(self.thingy.stupidMethod)() 111 self.failUnlessEqual( result, 10)114 self.failUnlessEqual(self._unpack(result), [10]) 112 115 113 116 def test_unpacksOneScalarArg(self): projects/Twisted-Goodies/trunk/twisted_goodies/pybywire/test/test_params.py
r109 r114 35 35 36 36 def func(self, x): 37 key = self.key('foo')37 key = 'foo' 38 38 if key not in self.cache: 39 39 self.counter = getattr(self, 'counter', 0) + 1 … … 61 61 y2 = self.ct.func(1.0) 62 62 self.failUnlessEqual(self.ct.counter, 1) 63 self.ct.c = 3.064 y3 = self.ct.func(2.0)65 self.failUnlessEqual(self.ct.counter, 1)66 63 self.failUnlessEqual(len(self.ct.cache), 1) 67 64 … … 74 71 self.failUnlessEqual(len(self.ct.cache), 1) 75 72 76 def test_ addsToCacheOnAttrChange(self):73 def test_clearsCacheOnAttrChange(self): 77 74 y1 = self.ct.func(1.0) 78 75 self.ct.b = 1.0 … … 80 77 self.failIfEqual(y1, y2) 81 78 self.failUnlessEqual(self.ct.counter, 2) 82 self.failUnlessEqual(len(self.ct.cache), 2)79 self.failUnlessEqual(len(self.ct.cache), 1) 83 80 84 81
