Query a user for input with a timeout.
Project description
Description
A tiny, simplistic little alternative to the standard Python input()-function allowing you to specify a timeout for the function.
pyTimedInput should work on both Windows and Linux, though no exceedingly extensive testing has been done and there might be bugs.
Install
$ pip3 install pyTimedInput
Usage
timedInput() works similar to Python’s default input() - function, asking user for a string of text, but timedInput() allows you to also define an amount of time the user has to enter any text or how many consecutive seconds to wait for input, if the user goes idle.
def timedInput(prompt="", timeOut=5, forcedTimeout=False, endCharacters=['\x1b', '\n', '\r'])
The function timedInput() from pyTimedInput accepts the following parameters:
- prompt, str: a string to show the user as a prompt when waiting for input.
Defaults to an empty string.
- timeout, int: how many seconds to wait before timing out.
Defaults to 5 seconds.
- forcedTimeout, bool: whether to wait for ‘timeout’ many consecutive seconds of idle time or simply time out regardless of user-input.
Defaults to False, ie. consecutive.
- endCharacters [], list: what characters to consider as end-of-input.
Defaults to new-line, carrier-feed and ESC-key.
The function returns a tuple of:
str: a string containing whatever the user typed, regardless of whether the function timed out or not.
bool: whether the function timed out or not.
from pyTimedInput import timedInput
userText, timedOut = timedInput("Please, do enter something: ")
if(timedOut):
print("Timed out when waiting for input.")
print(f"User-input so far: '{userText}'")
else:
print(f"User-input: '{userText}'")
timedKey() waits for the user to press one of a set of predefined keys, with a timeout, while ignoring any keys not on the list.
def timedKey(prompt="", timeOut=5, forcedTimeout=False, endCharacters=['y', 'n'])
The function timedKey() from pyTimedInput accepts the following parameters:
- prompt, str: a string to show the user as a prompt when waiting for input.
Defaults to an empty string.
- timeout, int: how many seconds to wait before timing out.
Defaults to 5 seconds.
- forcedTimeout, bool: whether to wait for ‘timeout’ many consecutive seconds of idle time or simply time out regardless of user-input.
Defaults to False, ie. consecutive.
- endCharacters [], list: what characters to accept.
Defaults to ‘y’ and ‘n’.
The function returns a tuple of:
str: a string containing the key user pressed, if on the endCharacters - list, or an empty string.
bool: whether the function timed out or not.
from pyTimedInput import timedKey
userText, timedOut = timedKey("Please, press 'y' to accept or 'n' to decline: ", endCharacters=['y', 'n'])
if(timedOut):
print("Timed out when waiting for input. Pester the user later.")
else:
if(userText == "y"):
print("User consented to selling their first-born child!")
else:
print("User unfortunately declined to sell their first-born child!")
Exceptions
Both timedInput() and timedKey() require an interactive shell to function and will raise a Runtimerror - exception otherwise, which will need to be caught in any script that will be used both interactively and non-interactively.
License
MIT
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pyTimedInput-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | de9caf2b3f6cfdf45dd9adf752efbdaa0802052db4da38aea770fd6b18e7d1b8 |
|
MD5 | 11d29d8c35d5eebd4867d89f13d8680f |
|
BLAKE2b-256 | 7433d76f2e2e741af65754f27824ea8eba77f18bdeeeccf71369bc48af44a5d4 |