def __init__(self, **kwArgs): self.__class__._compileDefaultDataSet() if __debug__: # make sure all of the keyword arguments passed in # are present in our data set for arg in kwArgs.keys(): assert arg in self.getDataNames(), ( "unknown argument for %s: '%s'" % (self.__class__, arg)) # assign each of our data items directly to self for name in self.getDataNames(): # if a value has been passed in for a data item, use # that value, otherwise use the default value if name in kwArgs: getSetter(self, name)(kwArgs[name]) else: getSetter(self, name)(self.getDefaultValue(name))
def __repr__(self): argStr = '' for param in self.ParamSet.getParams(): try: value = getSetter(self, param, 'get')() except: value = '<unknown>' argStr += '%s=%s,' % (param, repr(value)) return '%s(%s)' % (self.__class__.__name__, argStr)
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 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 __init__(self, *args, **kwArgs): self.__class__._compileDefaultParams() if len(args) == 1 and len(kwArgs) == 0: # extract our params from an existing ParamObj instance obj = args[0] self.paramVals = {} for param in self.getParams(): self.paramVals[param] = getSetter(obj, param, 'get')() else: assert len(args) == 0 if __debug__: for arg in kwArgs.keys(): assert arg in self.getParams() self.paramVals = dict(kwArgs)
def __repr__(self): argStr = '' for name in self.getDataNames(): argStr += '%s=%s,' % (name, repr(getSetter(self, name, 'get')())) return '%s(%s)' % (self.__class__.__name__, argStr)
def getValue(self, name): return getSetter(self, name, 'get')()
def applyTo(self, obj): # Apply our entire set of data to another POD for name in self.getDataNames(): getSetter(obj, name)(getSetter(self, name, 'get')())
def setDefaultValues(self): # set all the default data values on ourself for name in self.getDataNames(): getSetter(self, name)(self.getDefaultValue(name))
def extractFrom(self, obj): # Extract our entire set of params from a ParamObj obj.lockParams() for param in self.getParams(): self.paramVals[param] = getSetter(obj, param, 'get')() obj.unlockParams()
def applyTo(self, obj): # Apply our entire set of params to a ParamObj obj.lockParams() for param in self.getParams(): getSetter(obj, param)(self.getValue(param)) obj.unlockParams()