943,929 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1136
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
Jun 17th, 2009
0

Converting string to python code

Expand Post »
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:
python Syntax (Toggle Plain Text)
  1. string = "Class( 0,100 )"

Now I want to convert it into code:
python Syntax (Toggle Plain Text)
  1. Class( 0,100 )

Any ideas?
Thanks.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008
Jun 17th, 2009
0

Re: Converting string to python code

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.
Reputation Points: 1486
Solved Threads: 140
Practically a Posting Shark
siddhant3s is offline Offline
816 posts
since Oct 2007
Jun 17th, 2009
0

Re: Converting string to python code

I would be getting the string from a text file, but i'll try what you recommended
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008
Jun 17th, 2009
0

Re: Converting string to python code

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

python Syntax (Toggle Plain Text)
  1. >>> exec("print('df')")
  2. df
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Jun 17th, 2009
0

Re: Converting string to python code

Thanks! but, why is the exec function is a risk?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008
Jun 17th, 2009
0

Re: Converting string to python code

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.
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
Jun 17th, 2009
0

Re: Converting string to python code

oh I see. lol with the stuff i'm working on, I won't need to worry about that, but thanks for the tip!
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008
Jun 17th, 2009
0

Re: Converting string to python code

Hate to bother you, but I have one more question. I have this string:
python Syntax (Toggle Plain Text)
  1. temp_string = "Vent( Screen, 100, 484, 'Vent' )"
and when I do exec:
python Syntax (Toggle Plain Text)
  1. exec(temp_string)
python doesn't return anything (like init values that have been printed and returned statements). What could be the problem?
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008
Jun 18th, 2009
0

Re: Converting string to python code

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 ).
Reputation Points: 741
Solved Threads: 692
Nearly a Posting Maven
woooee is offline Offline
2,307 posts
since Dec 2006
Jun 18th, 2009
0

Re: Converting string to python code

Well, I have a class imported from a file:
python Syntax (Toggle Plain Text)
  1. from lib.class import *
in the main program, I read a text file which outputs:
python Syntax (Toggle Plain Text)
  1. Vent( 100,300, 'Vent' )
I assign that string from the text file to a var called 'temp_string', and then I go:
python Syntax (Toggle Plain Text)
  1. 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
Reputation Points: 10
Solved Threads: 1
Junior Poster in Training
besktrap is offline Offline
58 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Should I use a global variable (array in this case) or should i return the array
Next Thread in Python Forum Timeline: wxpython help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC