Re: Using eval with substitutions



abhishek@xxxxxxxxxxxxxxxx wrote:

Fredrik Lundh wrote:
(I'm afraid I don't really understand the point of your examples; what
is it you're really trying to do here ?)

A function is passed a bunch of string expressions like,
x = "a+b"
y= "x*a"
z= "x+y"

where a and b are assumed to have been assigned values in the local
namespace. Now I have to evaluate another string such as,
"z+y+x"

So as you say, I could do:
x=eval(x), y=eval(y), z=eval(z) and finally eval("z+y+x") but the
problem is that the initial strings are in no particular order, so I
don't know the sequence in which to perform the first 3 evaluations. I
was wondering if there was a simple way to 'pattern-match' so that the
required substitutions like z->x+y->x+x*a->(a+b)+(a+b)*a could be done
automatically.

Here is something to start with:

class EvalDict(object):
def __init__(self, namespace):
self.namespace = namespace
def __getitem__(self, key):
value = self.namespace[key]
if isinstance(value, str):
self.namespace[key] = value = eval(value, {}, self)
return value

def smart_eval(expr, namespace):
return eval(expr, {}, EvalDict(namespace))

def f():
a = 2
b = 3
x = "a+b"
y = "x*a"
z = "x+y"

return smart_eval("x + y + z", locals())

if __name__ == "__main__":
print f()

Beware of infinite recursion:

# RuntimeError: maximum recursion depth exceeded
smart_eval("a + b", dict(a="b", b="a"))

Peter
.



Relevant Pages

  • Re: Bracketed types
    ... keywords of the intrinsic types of VB, the namespace containing the custom type is imported, and you want to ... Dim s As [String] ' Binds to the custom 'String' class. ... Using brackets in VB.NET helps with this and also to address conflicts with inherited keywords, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: XmlSerializer
    ... XmlSerializerNamespaces emptyNamespace = new XmlSerializerNamespaces; ... //Serialize the array, using the empty namespace ... > public class FormField { ... > public FormField(string Name, string Value) { ...
    (microsoft.public.dotnet.xml)
  • Re: XslTransform is not matching node when source XML contains an xmlns attribute
    ... >> in my XSLT. ... > element) are in the namespace with namespace URI ... > If you try to decode a byte stream like a memory stream to a string then ... > string yourself if all you want is a string, ...
    (microsoft.public.dotnet.xml)
  • Re: The Nature of God- String Theory
    ... Bosonic string theories are 26-dimensional, ... And so in HighSchoolMath (a namespace) we say "space is 3D because ... Fuller was present at the birth of "D talk" in the early 1900s. ... Ivory Towerites (i.e. we don't need their permission to roll out our ...
    (soc.religion.quaker)
  • Re: Bracketed types
    ... keywords of the intrinsic types of VB, the namespace containing the custom type is imported, and you want to use the type without qualifying it by its namespace: ... Dim s As [String] ' Binds to the custom 'String' class. ...
    (microsoft.public.dotnet.languages.vb)