This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 7 years ago.
I want to execute python code in my browser. Now I enter code in text-field in template, took it to view, where doing following:
source = request.POST.get('source', '').replace('"', r'\"')
result = commands.getoutput('python -c "%s"' % source)
I used python's module command for this, but I think it's don't correct way.
Correct way is using code module, but I don't understand how to get result of execution code and organise REPL. Can you give a little tip how to do it, please?
UPD: I want to start interactive shell in my browser with some variables. User can write some functions to manipulate this vars in browser and see that comes out of it. I understand the danger of this, but now it is not relevant.
You need to take a look at eval and exec also it is potentially very dangerous from security point of view.
Related
This question already has answers here:
How do I execute a string containing Python code in Python?
(14 answers)
Closed 5 months ago.
This post was edited and submitted for review 5 months ago and failed to reopen the post:
Original close reason(s) were not resolved
How do I execute a string containing Python, without creating a new file, using excec() or using "os" module?
I've seen the similiar question, but the answeres always kind of created some "new file"(for example created something like this), or went out of the current code, but i want it to stay inside the code and execute it.
Example:
string_base64 = "eCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCkKeCA9ICJoZWxsbyBmZWxsb3ciCgoKcHJpbnQoeCk="
This b64 code has inside it strings and commands (+ For example imports) and it needs to run in the current file, without using exec() or modules
This Thread and the answers, didn't answer the question!
Buildin exec function seems to be doing exactly what you want:
https://docs.python.org/3/library/functions.html#exec
This question already has answers here:
What code can I use to check if script is running in IDLE?
(5 answers)
Closed 1 year ago.
I am writing a program and I need to detect whether a function is being called inside the shell or inside a file:
Is this possible? maybe with idlelib or something?
Cheers!
Origial: Duplicate question. if 'idlelib.run' in sys.modules: may be the best answer. Add and sys.stdin is not sys.__stdin__ to be even surer.
Edit: this is the correct answer to this question: how can I tell whether my code was run in IDLE or directly in Python, without IDLE. The person who closed this question thought the same thing, as the supposed duplicate answered this question.
Running code from an IDLE editor runs the code essentially the same as if it were entered in the shell. That is why print output appears in Shell, followed by a shell prompt. So without being able to verify for myself, I will not believe that identical code ran 10x faster when run from the IDLE editor.
Original: Printing lots of shorts strings is slower in the shell because of the overhead of interprocess communication. Joining strings before printing is faster because less overhead per char.
Edit: Here I was comparing running in IDLE versus running in Python without IDLE. I have seen printing speed differences of 10x in those two very different situations.
Original: If something else, ask a new question and I will look at it.
Edit: Above I said 'run from shell prompt' and 'run from editor' were essentially the same. There is a slight different between 'Shell started normally, without running a file first' and 'Shell started by first running a file from the editor'. It is the presence of attribute file in module main, which has to be imported.
================================ RESTART: Shell ====================
>>> import __main__
>>> hasattr(__main__, '__file__')
False
>>>
========================= RESTART: F:\Python\a\tem-3.py ===
# File has same two lines above.
True
# But note that subsequent calls entered here now return True.
>>> hasattr(__main__, '__file__')
True
>>>
EDIT Summary: a function run in IDLE's Shell can tell how Shell was last started or restarted. As near as I can tell, it cannot tell how it was entered. In either case, the function is compiled the same and has the same attributes. So it seems reasonable that it can only detect differences in the environment caused by the different in how the environment was set up. And the only environmental difference I could find was the presence or not of main.file when started with a file or not.
This question already has an answer here:
python IDLE shell appears not to handle some escapes correctly
(1 answer)
Closed 3 years ago.
There are several folks on here looking for backspace answers in Python. None of the questions I have searched have answered this for me, so here goes:
The Simple Goal: be able to print out a status string on one line, where the next status overwrites the first. Similar to a % complete status, where instead of scrolling a long line of 1%\n, 2%, ... etc. we just overwrite the first line with the newest value.
Now the question. When I type this in idle: print("a\bc") I get this as output: ac with what looks like an odd box with a circle between the 'a' and 'c'. The same thing happens when using sys.stdout.write().
Is this an Idle editor setting/issue? Does anyone even know if what I am trying is possible in the Idle Shell?
Thanks for any insight.
PS: Running Python 3.3.2 Idle on Windows 7, 64-bit system.
EDIT: Copying the output in Notepad++ is revealing that Python is printing out a 'backspace' character, and not actually going back a space. Perhaps what I am trying to accomplish is not possible?
Edit:
Apparently the carriage return \r and the backspace \b won't actually work within Idle because it uses a text control that doesn't render return/backspace properly.
You might be able to write some sort of patch for Idle, but it might be more trouble than it's worth (unless you really like Idle)
This doesn't answer your question in a literal fashion, but I think it might be useful to point out that generally interfaces like the one where you are describing (e.g., where one part of the screen is continuously updated, without newlines), it just generally implemented using a library like ncurses.
Python has a curses library built-in (http://docs.python.org/3.3/library/curses.html), which can more or less achieve your end goal.
This question already has answers here:
understanding the execution flow in python
(2 answers)
Closed 3 years ago.
I am trying to understand that when we execute a .py file, then from which part
of that code the python start the execution from?
E.g.when we execute a Java program, the "public static void main(String[] args)" is the location where the java start the code execution. So, when we talk about python, how does it work? I know there is a python main function
(__name__ = "__main__")
, I have gone through some article in and out of the Stackoverflow, they all say that it loads the python module, and then the python UDFs etc. So, as per my understanding, it is the location which is executed first thing. Please correct me, or guide me to some web links for my query.
If the Python code is in a method, no code will be executed unless you explicitly call the method (e.g. after checking __name__ == '__main__'). It is convention to call main method, but you can call any method as the starting point of execution.
If the Python code is not in a method, the code will be executed anytime you run or import the file.
This question already has answers here:
Time-Limited Input? [duplicate]
(2 answers)
Closed 8 years ago.
I'm starting out with python and want to know how to set a time limit for the user to input something using the input() function. I have looked at other questions and cant find one that:
a) I can understand
b) Works for me.
I am on Python 3.4 and I am a complete noob. Thank you for taking the time to answer my question!:)
I am also on windows.
Here's an earlier post that answers your question.
Basically you use the signal module to send an alarm signal to your program an interrupt the input command after a timeout.
I'm not sure there's a simpler way to do this, but to outline it broadly the "interrupted" function can be any function you like, it gets called when the timeout runs out. When the alarm triggers it raises an exception which sends you to the 'except block' of the input function.
Actually, looking at the example, I think input is a built-in function, so that function should really be called something else!