Converting string to python code

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Converting string to python code

 
0
  #1
Jun 17th, 2009
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:
  1. string = "Class( 0,100 )"

Now I want to convert it into code:
  1. Class( 0,100 )

Any ideas?
Thanks.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 793
Reputation: siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of siddhant3s has much to be proud of 
Solved Threads: 135
siddhant3s's Avatar
siddhant3s siddhant3s is offline Offline
Master Poster

Re: Converting string to python code

 
0
  #2
Jun 17th, 2009
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.
Siddhant Sanyam
(Not posting much)
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: Converting string to python code

 
0
  #3
Jun 17th, 2009
I would be getting the string from a text file, but i'll try what you recommended
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,607
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Converting string to python code

 
0
  #4
Jun 17th, 2009
Please know that this is a security risk. Use the exec function:

  1. >>> exec("print('df')")
  2. df
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: Converting string to python code

 
0
  #5
Jun 17th, 2009
Thanks! but, why is the exec function is a risk?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,607
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Converting string to python code

 
0
  #6
Jun 17th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: Converting string to python code

 
0
  #7
Jun 17th, 2009
oh I see. lol with the stuff i'm working on, I won't need to worry about that, but thanks for the tip!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: Converting string to python code

 
0
  #8
Jun 17th, 2009
Hate to bother you, but I have one more question. I have this string:
  1. temp_string = "Vent( Screen, 100, 484, 'Vent' )"
and when I do exec:
  1. exec(temp_string)
python doesn't return anything (like init values that have been printed and returned statements). What could be the problem?
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,028
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 290
woooee woooee is offline Offline
Veteran Poster

Re: Converting string to python code

 
0
  #9
Jun 18th, 2009
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 ).
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 58
Reputation: besktrap is an unknown quantity at this point 
Solved Threads: 1
besktrap's Avatar
besktrap besktrap is offline Offline
Junior Poster in Training

Re: Converting string to python code

 
0
  #10
Jun 18th, 2009
Well, I have a class imported from a file:
  1. from lib.class import *
in the main program, I read a text file which outputs:
  1. Vent( 100,300, 'Vent' )
I assign that string from the text file to a var called 'temp_string', and then I go:
  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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC