Stopping the Program [duplicate] - python

This question already has answers here:
How to exit from Python without traceback?
(10 answers)
Closed 9 years ago.
I am creating a program that would be a lot simpler to write if I could just end the program if something happened. However, whenever I use sys.exit(), that gives an error message, and I want a way to exit that doesn't show a message or anything. I just want a clean exit. If you know of anything I can use for this, please tell me.

Take a look on this: Here (look at os._exit)
Sys.exit(0) means 'All is good' but if you have an error you should still exit with 1.

Related

How do I execute a string containing Python, without creating a new file or using "os" module? [duplicate]

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

How to detect whether a program is running on the shell or from a program (IDLE) [duplicate]

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.

Python REPL in Django [duplicate]

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.

Make A Time limit for users to input command Python3 [duplicate]

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!

Only allow 1 instance of a python script [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python: single instance of program
What is the best way to insure that only 1 copy of a python script is running? I am having trouble with python zombies. I tired creating a write lock using open("lock","w"), but python doesn't notify me if the file already has a write lock, it just seems to wait.
Try:
import os
os.open("lock", os.O_CREAT|os.O_EXCL)
The documentation for os.open and its flags.
Your question is similar to this one: What is the best way to open a file for exclusive access in Python?. The answers given there should help you with your issue.
(Use the flag combination portalocker.LOCK_EX!|portalocker.LOCK_NB to return quickly. If the file is locked by another process, your script should get an exception.)

Categories

Resources