Changeset 108
- Timestamp:
- 11/22/07 14:44:58 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
projects/Twisted-Goodies/trunk/twisted_goodies/pybywire/pack.py
r101 r108 26 26 import struct, base64 27 27 28 28 29 try: 29 30 import scipy as s 30 31 except: 31 ImportError( 32 "Packing is primarily for use with SciPy arrays, but Scipy not found") 32 pass 33 34 35 def needsPacking(obj): 36 return str(type(obj)).startswith("<type 'numpy.") 33 37 34 38 projects/Twisted-Goodies/trunk/twisted_goodies/pybywire/params.py
r102 r108 27 27 from twisted.spread import pb, jelly 28 28 from twisted.python.reflect import namedObject 29 import pack 29 30 30 31 31 def parallow( instances):32 def parallow(*instances): 32 33 """ 33 34 Call this function with one or more arguments containing standard string … … 36 37 """ 37 38 for stringRep in instances: 39 # Load the class for the string representation 38 40 cls = namedObject(stringRep) 41 # Allow instances of the class, including its type and module 39 42 pb.globalSecurity.allowInstancesOf(cls) 40 43 … … 55 58 56 59 """ 60 name = None 57 61 instances = [] 58 62 paramNames, keyAttrs = [], {} … … 71 75 object.__setattr__(self, name, value) 72 76 for name, value in kw.iteritems(): 73 setattr(self, name, value)77 object.__setattr__(self, name, value) 74 78 myClass = self.__class__ 75 79 stringRep = "%s.%s" % (myClass.__module__, myClass.__name__) 76 80 if stringRep not in self.instances: 77 81 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) 80 85 81 86 def __getattr__(self, name): … … 86 91 87 92 def __str__(self): 88 if hasattr(self, 'name'):93 if self.name: 89 94 return self.name 90 95 return self.__class__.__name__.replace("_", " ") … … 145 150 as being safe for unjellying. 146 151 """ 147 state = { }152 state = {'name':self.name} 148 153 for sequence in (self.keyAttrs.keys(), self.paramNames): 149 154 for name in sequence: 150 155 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() 152 167 return state 153 168 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)
