Parse user input to ensure it meets the specified constraints
Project description
UserInputParser
Usage
Installation
pip install UserInputParser
Then import the package
from inputparser import *
This will make InputParser avalaible as well as all the natively supported validator functions.
Basic Example
from inputparser import *
parser = InputParser("", str, in_list, {"allowable" : ["a", "b", "c", "d"]})
valid = parser.is_valid(input("Select an option: a, b, c, d"))
and on error...
inputparser.inputparser.UserInputError: Provided value of 'e' did not meet the constraints enforced by: in_list().
Arguments passed to constraint function:
- allowable : ['a', 'b', 'c', 'd']
see more example use-cases
Features
Extensible
Type checking is included by default. If you need something more complex, that's supported too.
Evaluate elements of a list to ensure they meet your specific conditions:
from inputparser import *
InputParser(default_val=[],
allowable_types=(int, float),
constraint_func=are_valid_elements,
constraint_args={"element_constraint": in_range,
"constraint_args":
{"allowable_range": (0.0, 0.1)}
})
or perhaps you want something custom. Provide your own constraint function:
from inputparser import *
# Define your own constraint function
def custom_constraint(val, str_starts_with: str):
return True if val[0:len(str_starts_with)] == str_starts_with else False
# Create the object
parser = InputParser(default_val="$0",
allowable_types=str,
constraint_func=custom_constraint,
constraint_args={"str_starts_with": "$"})
Helpful Error Messages
As a user, not knowing where or why your input was wrong is aweful. Or worse, having the program produce wrong results because the user entered a percentage instead of a decimal - but didn't error.
User value provided for interest_rate in ./examples/user_input_example.json is invalid:
Provided value of '0.2' did not meet the constraints enforced by: in_range().
Arguments passed to constraint function:
- allowable_range : (0.0, 0.1)
- inclusive : True
Using default value of 0.0
This even works for custom functions.
Lightweight
UserInputParser has no dependencies to keep your programs fast.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file UserInputParser-0.1.0.tar.gz.
File metadata
- Download URL: UserInputParser-0.1.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f9cd22a0bf767da5242bd4dcdbb8999c0814eb674ecd06177c2fec79d90ec87c
|
|
| MD5 |
a6a2cb65a3aafb245c757c4dd3d584a0
|
|
| BLAKE2b-256 |
6d8f128d15925f31a16c4c23a8d625958feeceddcff7c4ddc9f34ce9cf421c79
|
File details
Details for the file UserInputParser-0.1.0-py3-none-any.whl.
File metadata
- Download URL: UserInputParser-0.1.0-py3-none-any.whl
- Upload date:
- Size: 6.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fae1742f9afae5871a5ca3c8bdf5fe10dc323effbb22e8affca2016b29e32335
|
|
| MD5 |
29e5d558f835795e514cdfc3384e7dae
|
|
| BLAKE2b-256 |
960d548594cc91e89868ad87b6bb6262c62099b6b888036538ac03b0103f5b93
|