| | |
Converting string to python code
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
Now I want to convert it into code:
Any ideas?
Thanks.
python Syntax (Toggle Plain Text)
string = "Class( 0,100 )"
Now I want to convert it into code:
python Syntax (Toggle Plain Text)
Class( 0,100 )
Any ideas?
Thanks.
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.
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)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
(Not posting much)
My Blog: Yatantrika
Migrate to Standard C++ :When to tell your C++ Code is Non-Standard.
Please Read before posting: How To Ask Questions The Smart Way
Please know that this is a security risk. Use the exec function:
python Syntax (Toggle Plain Text)
>>> exec("print('df')") df
Hate to bother you, but I have one more question. I have this string:
and when I do exec:
python doesn't return anything (like init values that have been printed and returned statements). What could be the problem?
python Syntax (Toggle Plain Text)
temp_string = "Vent( Screen, 100, 484, 'Vent' )"
python Syntax (Toggle Plain Text)
exec(temp_string)
•
•
Join Date: Dec 2006
Posts: 1,071
Reputation:
Solved Threads: 299
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
Well, I have a class imported from a file:
in the main program, I read a text file which outputs:
I assign that string from the text file to a var called 'temp_string', and then I go:
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
python Syntax (Toggle Plain Text)
from lib.class import *
python Syntax (Toggle Plain Text)
Vent( 100,300, 'Vent' )
python Syntax (Toggle Plain Text)
exec(my_string)
![]() |
Similar Threads
- Display Python code on Blogspot (Python)
- python code security or set up a complied python code with a password (Python)
- Converting String to Long (Java)
- converting string[][] to float[] (C)
- Help with Python code and accessing ZODB (Python)
- String to Int (C++)
- Converting a String into an integer array (C)
- Converting String to Integer help (C++)
Other Threads in the Python Forum
- Previous Thread: Should I use a global variable (array in this case) or should i return the array
- Next Thread: wxpython help
Views: 717 | Replies: 11
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied address ansi backend beginner changecolor class code conversion coordinates copy corners curves customdialog dan08 dictionary directory dynamic edit examples excel feet file float font format ftp function generator getvalue gui halp homework i/o iframe images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive screensaverloopinactive scrolledtext server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






