diff options
author | rcoh <rcoh@mit.edu> | 2010-12-08 16:39:50 -0500 |
---|---|---|
committer | rcoh <rcoh@mit.edu> | 2010-12-08 16:39:50 -0500 |
commit | b335b746523ffd59db1402b097a802b3fd99eaac (patch) | |
tree | 74333be1820f3d2666358c3b009beb14bf929256 /behaviors/ModifyParam.py | |
parent | 353ab16db64c86122c0fcb9e1852b85c14b354b8 (diff) |
Code for the demo. Everything works afaik. Contains a couple more optimizations. Contains modify param behavior. Improved support for recursive hooks. Modifications to SmootCoreObject to get us closer to a fully multi-threaded system. This should be the last commit directly to master. All further commits should be on subranches and get merged.
Diffstat (limited to 'behaviors/ModifyParam.py')
-rw-r--r-- | behaviors/ModifyParam.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/behaviors/ModifyParam.py b/behaviors/ModifyParam.py new file mode 100644 index 0000000..38b8cd5 --- /dev/null +++ b/behaviors/ModifyParam.py @@ -0,0 +1,27 @@ +from operationscore.Behavior import * +import Util +import pdb +#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}) +class ModifyParam(Behavior): + def processResponse(self, sensorInputs, recursiveInputs): + paramType = self['ParamType'] + paramName = self['ParamName'] + paramOp = self['ParamOp'] + if paramType == 'Sensor': + searchSet = sensorInputs + elif paramType == 'Recurse': + searchSet = recursiveInputs + else: + raise Exception('Unknown Param Type') + for behaviorInput in searchSet: + if paramName in behaviorInput: + try: + paramOp = paramOp.replace('{val}', 'behaviorInput[paramName]') #convert the {val} marker to something we can execute + behaviorInput[paramName] = eval(paramOp) + except: + raise Exception('Bad operation. Use things like \'{val}*5\', \'{val}+5\', exp({val}) etc.') + if paramType == 'Sensor': #return accordingly + return (searchSet, recursiveInputs) + if paramType == 'Recurse': + return (sensorInputs, searchSet) + |