I am trying to figure out how to create a little Python script that can take the following parameters:
- prompt - string
- time to wait as an integer
- number of characters before stopping
The last one is the number of characters I can enter before the program stops accepting characters and starts processing the input. I've seen some people use Python's select.select method, but that doesn't account for the 3rd item. I'm leaning towards curses, although I don't know if it supports a timeout which is making me think of threads. Any insights would be great! This will run on Linux with Python 2.6.
Ok, I have achieved it :D.
It's fairly incomplete; I just put a few values to test it. A few words of explanation:
curses
for that,end_time
), and then passend_time - current_time
in seconds as timeout toselect()
,Okay. This thread is a few years idle, but I spent a good hour exploring this (and related) threads for a clean solution. In the end, I decided to use the tool that already works well: bash's
read
. Here is an example asking for a power level. The first line in thetry
block addresses the question at hand (of course, this only works if you are starting your python script from a bash shell.) Here you have 3 seconds to enter up to 3 characters. (The rest of thetry
block converts to int and makes sure it is in an expected range.)