Package SmootLight :: Package behaviors :: Module ModifyParam
[hide private]
[frames] | no frames]

Source Code for Module SmootLight.behaviors.ModifyParam

 1  from operationscore.Behavior import * 
 2  import math 
 3  #Class to perform a given operation on some element of an argDict.  Designed to be used a recursive hook, but can serve sensor-based functions as well.  Specify ParamType (Sensor or Recurse), ParamName, and ParamOp, (a valid python statement with the old value represented as {val}) 
4 -class ModifyParam(Behavior):
5 """ModifyParam is a powerful class to perform an action on a specified key in the Argument 6 Dictionary of a response. Specify: 7 <ParamType> -- Sensor or Recurse 8 <ParamName> -- The name of the parameter you wish to modify 9 <ParamOp> -- The modification you wish to do. Use {val} to specify the current value of the 10 parameter in question. Special hooks for {x} and {y} also exist to access the x and y 11 locations.""" 12
13 - def processResponse(self, sensorInputs, recursiveInputs):
14 paramType = self['ParamType'] 15 if paramType == None: 16 paramType = 'Sensor' 17 paramName = self['ParamName'] 18 paramOp = str(self['ParamOp']) 19 if paramType == 'Sensor': 20 searchSet = sensorInputs 21 elif paramType == 'Recurse': 22 searchSet = recursiveInputs 23 else: 24 raise Exception('Unknown Param Type') 25 for behaviorInput in searchSet: 26 if paramName in behaviorInput: #TODO: copy -> modify instead of just 27 #copying 28 paramOp = paramOp.replace('{val}', 'behaviorInput[paramName]') #convert the {val} marker to something we can execute 29 #TODO: move elsewhere 30 paramOp = paramOp.replace('{y}', "behaviorInput['Location'][1]") 31 paramOp = paramOp.replace('{x}', "behaviorInput['Location'][0]") 32 behaviorInput[paramName] = eval(paramOp) 33 if paramType == 'Sensor': #return accordingly 34 return (searchSet, recursiveInputs) 35 if paramType == 'Recurse': 36 return (sensorInputs, searchSet)
37