[Zope] evaluating a string as an expression

Tony McDonald tony.mcdonald@ncl.ac.uk
Mon, 04 Mar 2002 08:58:42 +0000


On 4/3/02 5:55 am, "Robert Rottermann" <robert@redcor.ch> wrote:

> You can do that in an external method.
> (I hope there is a better answer than the on I have to offer tough)
> 
> Robert
> ----- Original Message -----
> From: "Greg Conway" <greg@gmlnt.com>
> To: "Zope@Zope. Org" <zope@zope.org>
> Sent: Monday, March 04, 2002 1:19 AM
> Subject: [Zope] evaluating a string as an expression
> 
> 
>> Hi all,
>> 
>> I'm managing (very slowly!) to ease myself from DTML to Python, but I have
>> just hit a bit of a snag...
>> 
>> I have a string of variables, such as value1, value2, value3, etc.
>> 
>> I wish to find the values of these variables as expressions within python,
>> if that makes sense!
>> 
>> What I've done is stored these values within a form using a loop, and now
> I
>> need to retreive the values in python. I can see them in the request, and
>> can access them via REQUEST.specify1, REQUEST.specify2, etc - but not
>> programmatically!
>> 

Robert is right - External Methods can do *almost* anything.

It looks like you create variables from a form, specify1, specify2 ...
specifyn

And you want to get the value of those variables (they're not really python
expressions like '3*x + 4' are they? In that case you will need an External
Method. If the values are something like 'fish', 'leg', 'foot', then this
should do it.

<dtml-var "REQUEST['specify1']"> ... <dtml-var "REQUEST['specifyn']">

If you want to do this programmaticaly, (untested! :)

your_list = ['specify1', 'specify2' ...]

<dtml-in your_list>
 <dtml-var "REQUEST[_['sequence-item']]">
</dtml-var>

(I tested this code, and it picks up the URL0, URL1 and URL2 variables).
<dtml-let urllist="['0', '1', '2']">
<dtml-in urllist>
<dtml-var sequence-item>
<dtml-var "REQUEST['URL' + _['sequence-item']]"><br>
</dtml-in>
</dtml-let>

This is easier to do in python though (pass my_list as parameter) (again,
untested, and no checks to make sure those variables *do* exist)

request=context.REQUEST
for a_var in my_list:
    print request[a_var]
return printed

hth
Tone
-- 
Dr Tony McDonald,  Assistant Director, FMCC, http://www.fmcc.org.uk/
The Medical School, Newcastle University Tel: +44 191 243 6140
A Zope list for UK HE/FE  http://www.fmcc.org.uk/mailman/listinfo/zope