UserInputParser Package
Suggested import method:
from inputparser import *
InputParser
- exception inputparser.inputparser.UserInputError(message: str = '')
Error that gets raised when a user input is invalid
- __init__(message: str = '')
Create an instance of the error
- Parameters
message – message describing the error
- class inputparser.inputparser.InputParser(default_val: Any, allowable_types: Union[type, Tuple], constraint_func: Optional[Callable] = None, constraint_args=None)
InputParser class enables the checking of user’s inputs while constructing helpful error messages in the case the input is invalid.
- __init__(default_val: Any, allowable_types: Union[type, Tuple], constraint_func: Optional[Callable] = None, constraint_args=None)
Create a new User Input Checker to ensure inputs from the user are valid.
- Parameters
default_val – if there is an error with the user’s input, this is the value the value field will default to
allowable_types – the expected type(s) of the user’s input
constraint_func – function handle that will be used to validate the user’s input
constraint_args –
dictionary of arguments to be passed to the constraint_func function
dictionary keys must match constraint_func function keyword arguments.
- is_valid(user_arg: Any, supress_error: bool = False) bool
Validate the user’s argument. If invalid create the error string and optionally raise an error.
- Parameters
user_arg – argument from the user
supress_error – boolean flag to supress the raising of an error on invalid input
- property default: Any
Get the default argument provided
- Returns
specified default argument
- property error_str: str
Get the error string that is constructed when a user’s input is invalid
- Returns
error string
- property user_arg: Any
Get the original argument provided by the user
- Returns
user’s input argument
- property valid: bool
Get the boolean flag indicating if the user’s input was valid.
Note
This field is None until is_valid() is called
- Returns
boolean flag indicating valid user argument
- property value: Any
When the user’s input is valid, this field returns the user’s argument. When the user’s input is invalid, it returns the default argument
- Returns
user’s input if valid, default value if invalid
Validators
- inputparser.validators.are_valid_elements(vals: list, element_constraint: Callable, args=[], constraint_args={}) bool
Check if the elements of a list conform ot the provided constraint function
- Parameters
vals – list of values to check against the constraint
element_constraint – function handle used to check each element
args – positional arguments to pass into the element_constraint function
constraint_args – keyword arguments to pass into the element_constraint function
- Returns
boolean flag indicating each element in the list meets the constraint
- inputparser.validators.in_list(val: list, allowable: list, case_sensitive: bool = False) bool
Check if provided value is in the allowable list
- Parameters
val – value to check
allowable – list of arguments that are allowable
case_sensitive – flag to enforce case sensitivity when the val is a string
- Returns
boolean indicating if the val was in the allowable list
- inputparser.validators.in_range(val: (<class 'int'>, <class 'float'>), allowable_range: typing.Tuple, inclusive: bool = True) bool
Check if the provided value is within the allowable range
- Parameters
val – value to check
allowable_range – iterator specifying the min and max allowable values
inclusive – flag to specify if the value can be equal to the provided min and max range. Default of True means if the value is equal to the min or max, the function returns true
- Returns
boolean flag indicating the value is within the range
- inputparser.validators.is_positive(val: (<class 'int'>, <class 'float'>)) bool
Check if the provided argument is real and positive
- Parameters
val – value to check
- Returns
boolean indicating value is positive
- inputparser.validators.is_real(val: (<class 'int'>, <class 'float'>)) bool
Check if the provided argument is real
- Parameters
val – value to check
- Returns
boolean indicating value is real
- inputparser.validators.matches_reg_ex(val: str, reg_ex: str) bool
Check if the provided argument matches the provided regular expression
- Parameters
val – value to check
reg_ex – regular expression string to compare the value to
- Returns
boolean flag indicating a match