From 06c639db6b98affab4abf07e57a90e2fcb5402ef Mon Sep 17 00:00:00 2001 From: rcoh Date: Sat, 5 Feb 2011 22:34:34 -0500 Subject: Early stages of param-binding in xml. Functional. RCOH --- operationscore/SmootCoreObject.py | 11 ++++++++--- tests/TestConfigLoaders.py | 9 ++++++++- util/ColorOps.py | 4 ++++ util/Config.py | 10 ++++++++++ util/Geo.py | 7 +++++++ 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/operationscore/SmootCoreObject.py b/operationscore/SmootCoreObject.py index 0d32773..51b84e3 100644 --- a/operationscore/SmootCoreObject.py +++ b/operationscore/SmootCoreObject.py @@ -2,6 +2,7 @@ import pdb import threading import thread import util.Config as configGetter +import types class SmootCoreObject(object): """SmootCoreObject is essentially a super-object class which grants us some niceties. It allows @@ -34,9 +35,13 @@ class SmootCoreObject(object): def __setitem__(self,k, item): self.argDict[k] = item - def __getitem__(self, item): - if item in self.argDict: - return self.argDict[item] + def __getitem__(self, key): + if key in self.argDict: + item = self.argDict[key] + if isinstance(item, types.FunctionType): + return item(self.argDict) #resolve the lambda function, if it exists + else: + return item else: return None def __contains__(self, item): diff --git a/tests/TestConfigLoaders.py b/tests/TestConfigLoaders.py index c79bbf1..c7e2b7a 100644 --- a/tests/TestConfigLoaders.py +++ b/tests/TestConfigLoaders.py @@ -29,6 +29,13 @@ class TestConfigLoaders(unittest.TestCase): result.write('tests/testdata/inheritanceTESTout.xml') assert filecmp.cmp('tests/testdata/inheritanceTESTout.xml',\ 'tests/testdata/inheritanceTRUTH.xml') - + #Tests our fancy new XML Eval Function + def test_eval(self): + assert Config.attemptEval('5') == 5 + assert Config.attemptEval('{5:10, 12:15}') == {5:10, 12:15} + singleLayerLambda = Config.attemptEval('${Val}$*5') + assert singleLayerLambda({'Val':2}) == 10 + doubleLayerLambda = Config.attemptEval("${Val1}$*'${Val2}$'") + assert doubleLayerLambda({'Val1':3})({'Val2':7}) == 21 if __name__ == '__main__': unittest.main() diff --git a/util/ColorOps.py b/util/ColorOps.py index 4b1162a..796a902 100644 --- a/util/ColorOps.py +++ b/util/ColorOps.py @@ -40,3 +40,7 @@ def randomBrightColor(): hue, sat, val = colorsys.hsv_to_rgb(hue, sat, val) ret = [hue, sat, val] return floatToIntColor(ret) + +class Color(object): + def __init__(self, r,g,b): + self.rep = [r,g,b] diff --git a/util/Config.py b/util/Config.py index 6fdb0d4..4153313 100644 --- a/util/Config.py +++ b/util/Config.py @@ -1,4 +1,5 @@ from xml.etree.ElementTree import * +import re import sys import xml import pdb @@ -113,6 +114,15 @@ def pullArgsFromItem(parentNode): def attemptEval(val): try: + if '${' in val and '}$' in val: #TODO: this could be a little cleaner + dictVal = re.sub("'\$\{(.+)\}\$'", "b['\\1']", val) #replace all expressions like {blah} with a['blah'] + dictVal = re.sub("\$\{(.+)\}\$", "a['\\1']", dictVal) #replace all expressions like {blah} with a['blah'] + if "'${" and "}$'" in val: #nested lambda madness + lambdaVal = eval('lambda a: lambda b: ' + dictVal) + else: + lambdaVal = eval('lambda a:'+dictVal) #TODO: nested lambdas + return lambdaVal #convert referential objects to lambda expressions which resolve + #dynamically val = eval(val) except (NameError, SyntaxError): val = str(val) diff --git a/util/Geo.py b/util/Geo.py index 0dde80b..43817ad 100644 --- a/util/Geo.py +++ b/util/Geo.py @@ -32,3 +32,10 @@ def windtrail(x,y,height,center,width): b=center c=width return a*((math.exp(-((x-b))/(c)))**2)*(math.exp(-((y))/(0.2*c)))**2 + +class Location(object): + def __init__(self,x=0,y=0): + self.x = x + self.y = y + def __add__(self, b): + return Location(self.x+b.x, self.y+b.y) -- cgit v1.2.3