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

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!

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

Is there a way to execute code every hour while still having other code run? [duplicate]

This question already has answers here:
How to repeatedly execute a function every x seconds?
(22 answers)
Closed 3 years ago.
I am new to python. I am making a bot to play Realm Grinder. Right now I am using PyAutoGui to control the mouse to click on certain options. I want my looping code to stop every hour and execute a different line of code. Then I need it to return to the main code and branch off again in half an hour.
I know I can't use sleep to do it and I have considered using a variable, but I don't know how to set up numeric variables yet. The python documentation I have found is very confusing.
Right now I have:
def realmGrinder():\
(code here)
while True:
(code here)
realmGrinder
Use asyncio.sleep(3600) to handle your delay. You'll want multithreading and the asyncio modules.
Have your loop check the current time at convenient intervals, executing the other code when an hour has passed. You'll want datetime for this; if you don't mind slipping a little over an hour on each interval, then simply time.time() will do the job.

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.

Stopping the Program [duplicate]

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.

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