Skip to main content

No project description provided

Project description

PatInput

This package provides functions and tools to get input from standard input, like the input function. Furthermore, this package allows you to define a pattern and force user to enter the input in a defined pattern and restrict entering illegal characters as the input.

Define Input Pattern

A pattern is defined as a function with three arguments which returns boolean. This function determines which character is allowed in defined position and situation. The first argument is the input character's ASCII code. The second one is the position of the cursor and the last one is the input string until that moment. Finally, returns True value if the input character is legal in this state and otherwise returns False value. For example, if you want to get an positive only integer you can do it like this:

digits = b"0123456789"
pattern = lambda char, pos, inp: char in list(digits)

Or,

def pattern(char: int, pos: int, inp: str):
    return char in b"0123456789"

Or if you want your users can enter negative integers also, you can do it like this:

def pattern(char: int, pos: int, inp: str):
    if pos == 0 and char == b'-'[0]:
        return True
    return char in b"0123456789"

Now, if you want your users to enter float numbers, you can do it as mentioned below:

def pattern(char: int, pos: int, inp: str):
    if pos == 0 and char == b'-'[0]:
        return True
    if char == b'.'[0] and '.' not in inp:
        return True
    return char in b"0123456789"

This pattern is like the previous one but it allows the user to enter a maximum of one dot (.) at the input.

Pattern tools

In addition, you can use the pre-defined functions and byte strings that are defined in patinput.input_pattern to make patterns in an easier way.

Pre-defined byte strings

There are some pre-defined byte strings that represent useful sets. These byte strings are listed below.

ASCII_EXTENDED = bytes(range(128, 255))
LOWER_ALPHABETS = b"abcdefghijklmnopqrstuvwxyz"
UPPER_ALPHABETS = b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
ALPHABETS = LOWER_ALPHABETS + UPPER_ALPHABETS
DIGITS = b"0123456789"
LEGAL_SIGNS = b"~!@#$%^&()_+=-`;'.,}{[]"
ILLEGAL_SIGNS = b"\\/:*?\"<>|"
SIGNS = LEGAL_SIGNS + ILLEGAL_SIGNS
SPACE = b" "

Pre-defined patterns

There are some pre-defined patterns (functions) that represent useful patterns. These patterns are listed below:

  • patinput.input_pattern.ALOW_NUMBERS is the pattern of digit-only inputs.
  • patinput.input_pattern.ALOW_ALPHABETS is the pattern of alphabet-only inputs.
  • patinput.input_pattern.ALOW_NOSPACE is the pattern of strings that does not include spaces and illegal signs which are useful for file names.

Getting Input From STDIN

After defining pattern you should get input from STDIN. The function which get this input is patinput.patinput and accepts these arguments:

Argument Name Type Default Description
allowness Callable(int, int, str) -> bool the function returns True always. A function that determines which character is allowed in defined position and situation (Pattern function).
prompt String "" If the prompt argument is present, it is written to standard output without a trailing newline.
default String "" The default value that is pre-typed in input field.
interrupt Callable(int, int, str) -> bool the function returns False always. A function that determines when the input getting procedure must interrupt.
ends_nl Boolean True After receiving input from the user, print a new line, if this argument is True.

For example, if want to use the pattern that we defined before to get float numbers from the user, we can do something like this:

from patinput import patinput

def pattern(char: int, pos: int, inp: str)
    if pos == 0 and char == b'-'[0]:
        return True
    if char == b'.'[0] and '.' not in inp:
        return True
    return char in b"0123456789"


user_input = patinput(pattern, "Please enter a float number: ")

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

patinput-0.1.0.tar.gz (7.4 kB view details)

Uploaded Source

Built Distribution

patinput-0.1.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file patinput-0.1.0.tar.gz.

File metadata

  • Download URL: patinput-0.1.0.tar.gz
  • Upload date:
  • Size: 7.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for patinput-0.1.0.tar.gz
Algorithm Hash digest
SHA256 37570d9131f1af8ac34bcbb055e8033355f5da32c00463116b92f3282739634c
MD5 52d51ddb62d86f2cc310651d3bc966fe
BLAKE2b-256 0e87b17d9ba7bcaed75fccf2e231555044b7bdbef6fe4d770bd8a7408f1ac307

See more details on using hashes here.

File details

Details for the file patinput-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: patinput-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for patinput-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bdbe90bb36a13539e61c0507b4ce55d54549bd47446ca758bb5067e244b77ac5
MD5 09986e72f9f3dc34af8f6a4b30e636cb
BLAKE2b-256 d76f90c90eec6542771305c4689f527be3e8b7bd60b203132253c4590b9c656e

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