Changeset 114

Show
Ignore:
Timestamp:
12/04/07 01:12:36 (1 year ago)
Author:
edsuom
Message:

Misc fixes to pybywire

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/Twisted-Goodies/trunk/twisted_goodies/pybywire/params.py

    r109 r114  
    7979    """ 
    8080    __metaclass__ = ParaMeta 
    81      
     81 
    8282    name = None 
    8383    paramNames, keyAttrs = [], {} 
     
    102102            self.cache = {} 
    103103            return self.cache 
     104        if name == 'registry': 
     105            return ParaMeta.registry 
    104106        raise AttributeError("No attribute '%s'" % name) 
    105107 
     
    112114        """ 
    113115        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
    115117        """ 
    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() 
    119120        object.__setattr__(self, name, value) 
    120121 
     
    138139            value = paramVector[k] 
    139140            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 any 
    144         arguments supplied. The arguments must be hashable directly, but the 
    145         key attributes are hashed with some recursion as needed and can be 
    146         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]) 
    157141 
    158142    #--- Jelly/Unjelly API ---------------------------------------------------- 
  • projects/Twisted-Goodies/trunk/twisted_goodies/pybywire/test/test_pack.py

    r101 r114  
    103103        self.thingy = self.Thingy() 
    104104 
     105    def _unpack(self, X): 
     106        return list(pack.Unpacker(X)) 
     107 
    105108    def test_wrapsFunction(self): 
    106109        substitute = pack.packwrap(lambda x: 2*x) 
    107         self.failUnlessEqual(substitute(10), 20
     110        self.failUnlessEqual(self._unpack(substitute(10)), [20]
    108111 
    109112    def test_wrapsMethod(self): 
    110113        result = pack.packwrap(self.thingy.stupidMethod)() 
    111         self.failUnlessEqual(result, 10
     114        self.failUnlessEqual(self._unpack(result), [10]
    112115 
    113116    def test_unpacksOneScalarArg(self): 
  • projects/Twisted-Goodies/trunk/twisted_goodies/pybywire/test/test_params.py

    r109 r114  
    3535     
    3636    def func(self, x): 
    37         key = self.key('foo') 
     37        key = 'foo' 
    3838        if key not in self.cache: 
    3939            self.counter = getattr(self, 'counter', 0) + 1 
     
    6161        y2 = self.ct.func(1.0) 
    6262        self.failUnlessEqual(self.ct.counter, 1) 
    63         self.ct.c = 3.0 
    64         y3 = self.ct.func(2.0) 
    65         self.failUnlessEqual(self.ct.counter, 1) 
    6663        self.failUnlessEqual(len(self.ct.cache), 1) 
    6764         
     
    7471        self.failUnlessEqual(len(self.ct.cache), 1) 
    7572 
    76     def test_addsToCacheOnAttrChange(self): 
     73    def test_clearsCacheOnAttrChange(self): 
    7774        y1 = self.ct.func(1.0) 
    7875        self.ct.b = 1.0 
     
    8077        self.failIfEqual(y1, y2) 
    8178        self.failUnlessEqual(self.ct.counter, 2) 
    82         self.failUnlessEqual(len(self.ct.cache), 2
     79        self.failUnlessEqual(len(self.ct.cache), 1
    8380 
    8481