def isSame(self, otherReward): for currVal in self.DataSet: getterStr = getSetterName(currVal, prefix='get') + '()' otherVal = eval('otherReward.' + getterStr) myVal = eval('self.' + getterStr) if otherVal != myVal: return False return True
def isSame(self, otherReward): for currVal in self.DataSet: getterStr = getSetterName(currVal, prefix = 'get') + '()' otherVal = eval('otherReward.' + getterStr) myVal = eval('self.' + getterStr) if otherVal != myVal: return False continue return True
def _handleUnlockParams(self): for param in self._paramsSet: # call the applier, if there is one applier = getattr(self, getSetterName(param, 'apply'), None) if applier is not None: self._curParamStack.append(param) applier() self._curParamStack.pop() self._priorValuesStack.pop() if hasattr(self, 'handleParamChange'): self.handleParamChange(tuple(self._paramsSet.keys())) del self._paramsSet
def copyFrom(self, other, strict=False): # if 'strict' is true, other must have a value for all of our data items # otherwise we'll use the defaults for name in self.getDataNames(): if hasattr(other, getSetterName(name, 'get')): setattr(self, name, getSetter(other, name, 'get')()) else: if strict: raise "object '%s' doesn't have value '%s'" % (other, name) else: setattr(self, name, self.getDefaultValue(name)) # support 'p = POD.POD().copyFrom(other)' syntax return self
def _compileDefaultDataSet(cls): if '_DataSet' in cls.__dict__: # we've already compiled the defaults for this class return # create setters & getters for this class if 'DataSet' in cls.__dict__: for name in cls.DataSet: setterName = getSetterName(name) if not hasattr(cls, setterName): def defaultSetter(self, value, name=name): setattr(self, name, value) cls.__dict__[setterName] = defaultSetter getterName = getSetterName(name, 'get') if not hasattr(cls, getterName): def defaultGetter(self, name=name): return getattr(self, name) cls.__dict__[getterName] = defaultGetter # this dict will hold all of the aggregated default data values for # this particular class, including values from its base classes cls._DataSet = {} bases = list(cls.__bases__) # process in reverse of inheritance order, so that base classes listed first # will take precedence over later base classes bases.reverse() for curBase in bases: # skip multiple-inheritance base classes that do not derive from POD if issubclass(curBase, POD): # make sure this base has its dict of data defaults curBase._compileDefaultDataSet() # grab all inherited data default values cls._DataSet.update(curBase._DataSet) # pull in our own class' default values if any are specified if 'DataSet' in cls.__dict__: cls._DataSet.update(cls.DataSet)
def setterStub(self, value, param=param, origSetterName=origSetterName): # should we apply the value now or should we wait? # if this obj's params are locked, we track which values have # been set, and on unlock, we'll call the applyers for those # values if self._paramLockRefCount > 0: priorValues = self._priorValuesStack[-1] if param not in priorValues: try: priorValue = getSetter(self, param, 'get')() except: priorValue = None priorValues[param] = priorValue self._paramsSet[param] = None getattr(self, origSetterName)(value) else: # prepare for call to getPriorValue try: priorValue = getSetter(self, param, 'get')() except: priorValue = None self._priorValuesStack.append({ param: priorValue, }) getattr(self, origSetterName)(value) # call the applier, if there is one applier = getattr(self, getSetterName(param, 'apply'), None) if applier is not None: self._curParamStack.append(param) applier() self._curParamStack.pop() self._priorValuesStack.pop() if hasattr(self, 'handleParamChange'): self.handleParamChange((param, ))
def _addAttrib(self, name, defaultVal, type): setattr(self, name, StateVar(defaultVal)) setattr(self, getSetterName(name), Functor(self._setter, name)) self._attribNames.append(name)
def __init__(self, *args, **kwArgs): assert issubclass(self.ParamSet, ParamObj.ParamSet) # If you pass in a ParamSet obj, its values will be applied to this # object in the constructor. params = None if len(args) == 1 and len(kwArgs) == 0: # if there's one argument, assume that it's a ParamSet params = args[0] elif len(kwArgs) > 0: assert len(args) == 0 # if we've got keyword arguments, make a ParamSet out of them params = self.ParamSet(**kwArgs) self._paramLockRefCount = 0 # these hold the current value of parameters while they are being set to # a new value, to support getPriorValue() self._curParamStack = [] self._priorValuesStack = [] # insert stub funcs for param setters, to handle locked params for param in self.ParamSet.getParams(): # set the default value on the object setattr(self, param, self.ParamSet.getDefaultValue(param)) setterName = getSetterName(param) getterName = getSetterName(param, 'get') # is there a setter defined? if not hasattr(self, setterName): # no; provide the default def defaultSetter(self, value, param=param): #print '%s=%s for %s' % (param, value, id(self)) setattr(self, param, value) self.__class__.__dict__[setterName] = defaultSetter # is there a getter defined? if not hasattr(self, getterName): # no; provide the default. If there is no value set, return # the default def defaultGetter( self, param=param, default=self.ParamSet.getDefaultValue(param)): return getattr(self, param, default) self.__class__.__dict__[getterName] = defaultGetter # have we already installed a setter stub? origSetterName = '%s_ORIG' % (setterName, ) if not hasattr(self, origSetterName): # move the original setter aside origSetterFunc = getattr(self.__class__, setterName) setattr(self.__class__, origSetterName, origSetterFunc) """ # if the setter is a direct member of this instance, move the setter # aside if setterName in self.__dict__: self.__dict__[setterName + '_MOVED'] = self.__dict__[setterName] setterFunc = self.__dict__[setterName] """ # install a setter stub that will a) call the real setter and # then the applier, or b) call the setter and queue the # applier, depending on whether our params are locked """ setattr(self, setterName, types.MethodType( Functor(setterStub, param, setterFunc), self, self.__class__)) """ def setterStub(self, value, param=param, origSetterName=origSetterName): # should we apply the value now or should we wait? # if this obj's params are locked, we track which values have # been set, and on unlock, we'll call the applyers for those # values if self._paramLockRefCount > 0: priorValues = self._priorValuesStack[-1] if param not in priorValues: try: priorValue = getSetter(self, param, 'get')() except: priorValue = None priorValues[param] = priorValue self._paramsSet[param] = None getattr(self, origSetterName)(value) else: # prepare for call to getPriorValue try: priorValue = getSetter(self, param, 'get')() except: priorValue = None self._priorValuesStack.append({ param: priorValue, }) getattr(self, origSetterName)(value) # call the applier, if there is one applier = getattr(self, getSetterName(param, 'apply'), None) if applier is not None: self._curParamStack.append(param) applier() self._curParamStack.pop() self._priorValuesStack.pop() if hasattr(self, 'handleParamChange'): self.handleParamChange((param, )) setattr(self.__class__, setterName, setterStub) if params is not None: params.applyTo(self)