[Zope] Evaluating python with my own variable bindings

Evan Simpson evan@4-am.com
Wed, 21 Aug 2002 17:36:56 -0500


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