Changeset 108

Show
Ignore:
Timestamp:
11/22/07 14:44:58 (1 year ago)
Author:
edsuom
Message:

Fix to make Parameterizable more robust

Files:

Legend:

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

    r101 r108  
    2626import struct, base64 
    2727 
     28 
    2829try: 
    2930    import scipy as s 
    3031except: 
    31     ImportError( 
    32         "Packing is primarily for use with SciPy arrays, but Scipy not found") 
     32    pass 
     33 
     34 
     35def needsPacking(obj): 
     36    return str(type(obj)).startswith("<type 'numpy.") 
    3337 
    3438 
  • projects/Twisted-Goodies/trunk/twisted_goodies/pybywire/params.py

    r102 r108  
    2727from twisted.spread import pb, jelly 
    2828from twisted.python.reflect import namedObject 
     29import pack 
    2930 
    3031 
    31 def parallow(instances): 
     32def parallow(*instances): 
    3233    """ 
    3334    Call this function with one or more arguments containing standard string 
     
    3637    """ 
    3738    for stringRep in instances: 
     39        # Load the class for the string representation 
    3840        cls = namedObject(stringRep) 
     41        # Allow instances of the class, including its type and module 
    3942        pb.globalSecurity.allowInstancesOf(cls) 
    4043 
     
    5558 
    5659    """ 
     60    name = None 
    5761    instances = [] 
    5862    paramNames, keyAttrs = [], {} 
     
    7175                object.__setattr__(self, name, value) 
    7276        for name, value in kw.iteritems(): 
    73             setattr(self, name, value) 
     77            object.__setattr__(self, name, value) 
    7478        myClass = self.__class__ 
    7579        stringRep = "%s.%s" % (myClass.__module__, myClass.__name__) 
    7680        if stringRep not in self.instances: 
    7781            self.instances.append(stringRep) 
    78             # Make sure you can accept copies of me coming back, too. 
    79             pb.globalSecurity.allowInstancesOf(myClass) 
     82            # Make me the unjellyable for myself, and ensure that my broker can 
     83            # accept copies of me coming back, too. 
     84            pb.setUnjellyableForClass(stringRep, myClass) 
    8085 
    8186    def __getattr__(self, name): 
     
    8691 
    8792    def __str__(self): 
    88         if hasattr(self, 'name')
     93        if self.name
    8994            return self.name 
    9095        return self.__class__.__name__.replace("_", " ") 
     
    145150        as being safe for unjellying. 
    146151        """ 
    147         state = {
     152        state = {'name':self.name
    148153        for sequence in (self.keyAttrs.keys(), self.paramNames): 
    149154            for name in sequence: 
    150155                if hasattr(self, name): 
    151                     state[name] = getattr(self, name) 
     156                    value = getattr(self, name) 
     157                    if pack.needsPacking(value): 
     158                        if '_packedNames' not in state: 
     159                            state['_packedNames'] = [] 
     160                            packer = pack.Packer() 
     161                        state['_packedNames'].append(name) 
     162                        packer.append(value) 
     163                    else: 
     164                        state[name] = value 
     165        if 'packer' in locals(): 
     166            state['_packedValues'] = packer() 
    152167        return state 
    153168 
    154  
    155  
    156  
     169    def setStateFor(self, unjellier, state): 
     170        """ 
     171        Sets my state from the supplied I{state} dict, as usual except for any 
     172        packed values present. 
     173        """ 
     174        if '_packedNames' in state: 
     175            packedNames = state.pop('_packedNames') 
     176            unpacker = pack.Unpacker(state.pop('_packedValues')) 
     177            for k, value in enumerate(unpacker): 
     178                object.__setattr__(self, packedNames[k], value) 
     179        self.__dict__.update(state)