calculating a string equation

A

Astan Chee

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
 
A

Arnaud Delobelle

Astan Chee said:
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()
2

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

Chris Rebert

Arnaud Delobelle wrote:
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:

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top