[Zope] Evaluating python with my own variable bindings

Ross Boylan RossBoylan@stanfordalumni.org
Sun, 11 Aug 2002 09:36:05 -0700


On Mon, Aug 05, 2002 at 05:16:46PM -0500, Evan Simpson wrote:
> Ross Boylan wrote:
> >I would like to bind symbols to values, e.g, Q1=True, Q2=False.... and
> >then allow the user to specify arbitary logical expressions like
> >Q3 and Q4
> >
> >or (given different bindings)
> >Q2 == 5
> 
> If you only want to allow expressions (not statements), you should 
> probably look at lib/python/Products/PageTemplates/ZRPythonExpr.py, 
> which defines encapsulated Python expressions and allows you to pass 
> arbitrary context to them.
> 
> Cheers,
> 
> Evan @ 4-am
> 
> 
This is a repeat of a previous message that seems not to have gotten
through.

Thanks, that looks like what I need.  I have a few questions about it,
though.

Is there any through the web interface for this, either for creating
one or using it?  Or is the intent that it be just used on the fly
for, say, a string that you get otherwise?

What is the deal with 'engine' that appears in the __init__ arguments
and is never used or referenced?  Similarly, 'name' is an argument
that's never used.

Is the intended useage via the __call__ interface, or the modules
call_with_ns?  In either case, an example of useage would be helpful
to me.

In particular, how do I get my namespace to the function?  Here's my
guess at how to use it:
  c= globals().copy()
  c.update({'Q2':0, 'Q3':1})
  e= PythonExpr('','Q2 or Q3', None)
  result = e(c)

Is that right?  (I want to evaluate Q2 or Q3 with those symbols bound
to false and true respectively).

I've never seen a class be a subclass of itself!  The definition is
class PythonExpr(PythonExpr):
I take it the base class refers to the name imported from the module
PythonExpr, while the class definition is in this namespace (and will
hide the other one).

Are you the creator of this module?  I think I remember that you
developed some machinery for secure python execution.  If so,
thanks--it's very handy.