Giter Club home page Giter Club logo

pytimedinput's Introduction

pytimedinput

Description

A tiny, simplistic little alternative to the standard Python input()-function allowing you to specify an optional 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()

timedInput() works similar to Python's default input() - function, asking user for a string of text, but timedInput() allows you to 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, resetOnInput=True, maxLength=0, allowCharacters="", 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, use -1 to disable.
  • resetOnInput, bool: Reset the timeout-timer any time user presses a key.
    Defaults to True.
  • maxLength, int: the maximum length of the string user is allowed to enter.
    Defaults to 0, ie. unlimited.
  • allowCharacters, str: Which characters the user is allowed to enter.
    Defaults to "", ie. any character.
  • endCharacters, str: On which characters to stop accepting input.
    Defaults to "\x1b\n\r", ie. ESC, newline and carriage-return. Cannot be empty.

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.

Example:

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()

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, resetOnInput=True, allowCharacters="")

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, use -1 to disable.
  • resetOnInput, bool: Reset the timeout-timer any time user presses a key.
    Defaults to True.
  • allowCharacters, str: Which characters the user is allowed to enter.
    Defaults to "", ie. any character.

The function returns a tuple of:

  • str: a string containing the key the user pressed or an empty string.
  • bool: whether the function timed out or not.

Example:

from pytimedinput import timedKey
userText, timedOut = timedKey("Please, press 'y' to accept or 'n' to decline: ", allowCharacters="yn")
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!")

Tip: use timedKey() for the infamous "Press any key to continue."-prompt!

from pytimedinput import timedKey
timedKey("Press any key to continue.", timeout=-1)

timedInteger() and timedFloat()

timedInteger() and timedFloat work like timedInput(), except they only allow the user to enter numbers, and comma or period in case of timedFloat. Can be used to enter a negative number.

def timedInteger(prompt="", timeout=5, resetOnInput=True, allowNegative=True)

The function timedInteger() and timedFloat() from pytimedinput accept 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, use -1 to disable.
  • resetOnInput, bool: Reset the timeout-timer any time user presses a key.
    Defaults to True.
  • allowNegative, bool: Whether to allow the user to enter a negative value or not.

The function returns a tuple of:

  • int/float or None: an integer or float, depending on which function was called or None, if no number was entered.
  • bool: whether the function timed out or not.

Example:

from pytimedinput import *
userNumber, timedOut = timedFloat("Enter a floating-point value: ")
if(not timedOut):
    if(userNumber == None):
        print("We wanted a number, but got none.")
    else:
        print(f"We should do some fancy maths with {userNumber}!")

Exceptions

All the functions 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

pytimedinput's People

Contributors

acemouse avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.