[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

calculating a string equation

Astan Chee

2/11/2010 9:43:00 PM

Hi,
I have some variables in my script that looks like this:
vars = {'var_a':'10','var_b':'4'}
eqat = "(var_a/2.0) <= var_b"
result = "(var_a+var_b)/7"
What I'm trying to do is to plug in var_a and var_b's values from vars
into eqat and see if eqat returns true or false as well as getting the
value of result if these variables were "plugged in". How do I do this?
I'm also expecting eqat and result to contain various python
mathematical operators like **, and compounded ()'s.
I'm not sure how to convert the equation; if I have to make a bunch of
if-statements or if there is a python function that already does
something like this.
Thanks for any help.
Cheers
Astan

2 Answers

Arnaud Delobelle

2/11/2010 9:50:00 PM

0

Astan Chee <astan.chee@al.com.au> writes:

> Hi,
> I have some variables in my script that looks like this:
> vars = {'var_a':'10','var_b':'4'}
> eqat = "(var_a/2.0) <= var_b"
> result = "(var_a+var_b)/7"
> What I'm trying to do is to plug in var_a and var_b's values from vars
> into eqat and see if eqat returns true or false as well as getting the
> value of result if these variables were "plugged in". How do I do
> this?
> I'm also expecting eqat and result to contain various python
> mathematical operators like **, and compounded ()'s.
> I'm not sure how to convert the equation; if I have to make a bunch of
> if-statements or if there is a python function that already does
> something like this.

Yes: eval()

>>> vars = {'var_a':10 ,'var_b':4}
>>> eqat = "(var_a/2.0) <= var_b"
>>> result = "(var_a+var_b)/7"
>>> eval(eqat, vars)
False
>>> eval(result, vars)
2

(Note that I have slightly modified your vars dictionary)
See a recent thread about the dangers of eval().

--
Arnaud

Chris Rebert

2/20/2010 11:22:00 PM

0

On Sat, Feb 20, 2010 at 3:07 PM, Astan Chee <astan.chee@al.com.au> wrote:
> Arnaud Delobelle wrote:
> Astan Chee <astan.chee@al.com.au> writes:
> Hi,
> I have some variables in my script that looks like this:
> vars = {'var_a':'10','var_b':'4'}
> eqat = "(var_a/2.0) <= var_b"
> result = "(var_a+var_b)/7"
> What I'm trying to do is to plug in var_a and var_b's values from vars
> into eqat and see if eqat returns true or false as well as getting the
> value of result if these variables were "plugged in". How do I do
> this?
> I'm also expecting eqat and result to contain various python
> mathematical operators like **, and compounded ()'s.
> I'm not sure how to convert the equation; if I have to make a bunch of
> if-statements or if there is a python function that already does
> something like this.
>
>
> Yes: eval()
>
>
>
> vars = {'var_a':10 ,'var_b':4}
> eqat = "(var_a/2.0) <= var_b"
> result = "(var_a+var_b)/7"
> eval(eqat, vars)
>
>
> False
>
> Hi,
> I now have a slight problem with this. This doesnt seem to work:
>>>> vars = {'var a':10 ,'var b':4}
>>>> eqat = "(var a/2.0) <= var b"
>>>> eval(eqat, vars)
>
> Traceback (most recent call last):
>   File "<pyshell#11>", line 1, in <module>
>     eval(eqat, vars)
>   File "<string>", line 1
>     (var a/2.0) <= var b
>          ^
> SyntaxError: invalid syntax
>
> So eval can't take spaces? is there a way to go around this?

eval() requires the string be valid Python code and conform to
Python's syntax. Obviously your string isn't valid Python.
You can either require beforehand that variables not be prefixed with
"var " as they are currently, or you can get rid of the "var"s
yourself:

vars = {'a':10 ,'b':4}
equat = equat.replace('var ','')
eval(equat, vars)

but this could cause problems if you have a variable whose names ends
with "var"; using a regular expression for the removal would fix that.

Cheers,
Chris
--
http://blog.re...