Skip to main content

Alternative to classic input with support for restricting certain characters

Project description

Restricted Input

Important Change

Made a small backward incompatible change. Instead of exclude, use allow. I changed it because:

  1. There was a typo anyway. It was exlude instead of exclude!
  2. I feel like allow has a better and clearer meaning than exclude. exclude might be confusing sometimes.

Overview

A way of imposing restrictions in input().

For example, if you want the user to only enter numbers, you could use this. Only want uppercase letters? Works here.

It uses msvcrt.getch/alternative in *nix to get the keystroke and check it in real time.

Installation

Install restricted_input from pypi using:

pip install restricted_input

How to use it

Import it

from restricted_input import r_input

r_input is the function which can be used instead of input()

It has five arguments

variable = r_input(prompt, input_type, allow, maxlength, warning)
  • prompt: is the prompt to be given for the input dialog. string

  • input_type : is the type of data to which the input should be restricted to. string

    It can be

    • "normal": normal text

    • "string_all": Allows only string, space is not allowed

    • "string_upper": Allows only uppercase string, space is not allowed

    • "string_lower": Allows only lowercase string, space is not allowed

    • "specials" Allows only special characters - `` ~ ! @ # $ % ^ & * ( ) - _ = + [ { ] } ; : ' " , < . > / ? \ |`. Space not included

    • "integer": Allows only integers, doesn't allow -, . or space

    • "float" : Allows only integers, including . single time. Space or - not included

    • "version": Allows only integers, including . multiple times. Space or - not included

    • "nothing" . Obviously allows nothing. Useful in scenarios where you need only a limited number of characters.

    To get the list of input_types:

    from restricted_input import get_input_types
    print(get_input_types)
    
  • allow: is the argument for allowing certain characters. string or list

    For example, if you want to allow hyphens in integers, you can use

    r_input("Enter the number: ", "integer", "-")
    

    If you want to allow input of the character v, a,b and g in version type input, use:

    r_input("Enter the version: ", "version", "vabg")
    # Or
    r_input("Enter the version: ", "version", ["v", "a", "b", "g"])
    
  • maxlength: is the argument for maximum length for input. Useful in scenarios where only single character is required.integer

  • warning: is the argument for any warnings is the user has entered something which isn't allowed. string

Example

Example GIF

Issues/limitations

  • It will not work in some IDLEs like Spyder or PyCharm. This is because some IDLEs emulate a terminal which might not work with this module. Try to run the program in external terminal.

  • You tell me!

Contributing

If you need help, don't hesitate to open an issue. Pull requests are welcome, but please state the reason for it.

License

This project is under MIT License

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

restricted_input-0.3.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

restricted_input-0.3.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file restricted_input-0.3.0.tar.gz.

File metadata

  • Download URL: restricted_input-0.3.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1

File hashes

Hashes for restricted_input-0.3.0.tar.gz
Algorithm Hash digest
SHA256 bd0f19c51965a0ef0b39eee446a4bf74c1fc9cac3e807b9c9a792b2f92086fe8
MD5 ab1737863048361928e76d908a69317f
BLAKE2b-256 1b52cded8bc810a11b2f0cd823314a7969928bff74cda51d055656324d1a7a3f

See more details on using hashes here.

File details

Details for the file restricted_input-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: restricted_input-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1

File hashes

Hashes for restricted_input-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3007b18d0465c95abfc15356cda6161b374df26f7ea06d6c990d0bdee0a66473
MD5 97b59e6fa8080ed4819e675aa98d5302
BLAKE2b-256 211316c3540d98bc4b71672293ab799e3ab339e796209da6fae39f141fe64d94

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page