Convert string to a statement

This is the place for queries that don't fit in any of the other categories.

Convert string to a statement

Postby stdq » Tue Jul 23, 2013 10:07 pm

Hi, everyone. So, suppose I have a string that goes like 'y = sin(x) + x'. After setting x to a value, how do I make Python evaluate the given string to as statement, to calculate the value of y? Thanks in advance!
stdq
 
Posts: 4
Joined: Tue Jul 23, 2013 10:04 pm

Re: Convert string to a statement

Postby Yoriz » Tue Jul 23, 2013 10:46 pm

Code: Select all
>>> from math import sin
>>> x = 2
>>> y = sin(x) + x
>>> print y
2.90929742683


Edit: ignor my irevelant piece of crap code, until i see micseydel's post i wondered why you where saying i have a string
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Image
User avatar
Yoriz
 
Posts: 1016
Joined: Fri Feb 08, 2013 1:35 am
Location: UK

Re: Convert string to a statement

Postby micseydel » Tue Jul 23, 2013 10:50 pm

I hope no one flames me, but I believe this is a good time for the responsible use of eval()*. The right hand side of the equation is something Python can handle, you just have to tell it to. I would recommend splitting the user's input on " = " and throwing away the left side, or better yet, simply prompt the user with "y = " so that you don't have to deal with that.

Once you're left with "sin(x) + x" and your own x variable, you just have to tell eval() to use the variables and to throw away powerful Python built-ins which aren't very secure (such as eval() itself). eval() takes three arguments, the last two optional but very important. The first is your string you want evaluated. The second and last specify what is available for eval() to use. They should include as little as possible typically, although including all of math is alright.

Code: Select all
import math

user_string = "sin(x) + x"
scope = {} # start with NOTHING

scope.update(vars(math)) # put math stuff in there
scope['x'] = math.pi # put your own x value in the scope for eval() to use

y = eval(user_string, {}, scope) # (eval_string, globals, locals)
print y


I've done more work for you here than I normally do when answering questions, because using eval() in the recommended way carries a bit of baggage. You still do have some left to do though, and if you have questions about it remember to include your entire incorrect code, in code tags, along with the entire traceback if you get it, also in code tags. And of course, tell us what your input is, what your output is, and what your desired output is.


* eval() is potentially dangerous, and is discouraged from general use because of it even in instances where the user input is considered known, because historically such assumptions are found to be false. Above, I have outlined a secure way of using it, although you could get away with less and have your program work if this wasn't a concern.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1435
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Convert string to a statement

Postby stdq » Tue Jul 23, 2013 11:15 pm

Thanks!
stdq
 
Posts: 4
Joined: Tue Jul 23, 2013 10:04 pm


Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], W3C [Linkcheck] and 5 guests