[Zope] Evaluating python with my own variable bindings

Ross Boylan RossBoylan@stanfordalumni.org
Wed, 21 Aug 2002 16:04:06 -0700


On Wed, Aug 21, 2002 at 05:36:56PM -0500, Evan Simpson wrote:
> Ross Boylan wrote:
> >>>>from Products.PageTemplates import Expressions
> >>>>myvars = {'q1':0, 'q2':1}
> >>>>e = Expressions.getEngine()
> >>>>c = e.getContext(myvars)
> >>>>from Products.PageTemplates.ZRPythonExpr import PythonExpr
> 
> You're better off skipping this last line and using 
> "Expressions.PythonExpr" directly.  That way you'll get the correct 
> implementation for whichever version of Zope you're using.
> 
> ># the last argument in the next line should be an engine
> ># but it's ignored anyway, as far as I can tell.
> >
> >>>>pe = PythonExpr('python', 'q1 or q2', c)
> 
> Yep, all of the expression type constructors take the same parameters, 
> but only some of them use the engine.  StringExpr does, for example.
> 
> >An alternate interface appears to be
> >pe = e.compile('python: q1 or q2')
> 
> This is probably the best method if you want to allow general TALES 
> expressions.  In any case, you will typically want to instantiate a 
> single engine to use for all your expressions and make a new context 
> object every time you evaluate an expression object.
> 
> Sorry I wasn't more help earlier.
> 
> Cheers,
> 
> Evan @ 4-am
> 
> 

Thanks for the info.  Can you say whether it is safe to persist either
the engine or the PythonExpr instance?