Hi all. Is there any way that if I give python a string, it can convert it into code? Example, say I am given this string:

string = "Class( 0,100 )"

Now I want to convert it into code:

Class( 0,100 )

Any ideas?
Thanks.

Recommended Answers

All 11 Replies

How are you giving the strings?
If you are giving a complete python program in the string, you do a trick:
Write all the string into a file and execute it with the interpreter by using a execv or system command.

I would be getting the string from a text file, but i'll try what you recommended

Please know that this is a security risk. Use the exec function:

>>> exec("print('df')")
df

Thanks! but, why is the exec function is a risk?

You are loading code from a string into your application and running it. Unless you have compete and absolute control over the string (you create it yourself, and no part of it comes from outside our program), there is always a way for someone to inject some malicious code into your program.

oh I see. lol with the stuff i'm working on, I won't need to worry about that, but thanks for the tip!

Hate to bother you, but I have one more question. I have this string:

temp_string = "Vent( Screen, 100, 484, 'Vent' )"

and when I do exec:

exec(temp_string)

python doesn't return anything (like init values that have been printed and returned statements). What could be the problem?

commented: I am also facing a similar problem. I would like to know how you solved it? +0

What does it return, an error message? exec() will work on things like print statements or 1+2-3, but if you call a function or class then you must already have that function defined, otherwise an error message. Perhaps if you explained what you are trying to do, there would be an easier way, like a generic class that you would pass the strings in the file to as parameters --> some_class( the_string ).

Well, I have a class imported from a file:

from lib.class import *

in the main program, I read a text file which outputs:

Vent( 100,300, 'Vent' )

I assign that string from the text file to a var called 'temp_string', and then I go:

exec(my_string)

It doesn't work, but I write up some code that (based of the strings from the text file) recognizes the first four characters in a line, a.k.a 'Vent', and then gets the string(s) from in between the characters '(' and ',' and then ',' and ','. The last argument just stays a string. I then pass a new instance with those new given values. It's no big deal if the exec thing doesn't work, but thanks any ways

commented: I am also facing a similar problem. I would like to know how you solved it? +0

The function exec() does not know where class Vent is coming from.
You could try something like:
exec(mystring, globals())

Now you are putzing around with the global dictionary, dangerous territory indeed!

i think this should work.. i didn't test it out yet coz this comp doesnt hav python installed... try it out n let me know..(im a noob tho.. dnt mind if it doesnt:))

def str2code(s):
	if (s[0] == '\'' or s[0] == '\"') or (s[-1] == '\'' or s[-1] == '\"'):
		del s[0]
		del s[-1]
		return s
#print str2code("testing")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.