A utility for handling user input validation and exceptions in Python.
Project description
Value Error Exception Handler
A Python utility function designed to simplify input validation by handling ValueError exceptions and ensuring user input meets specified criteria. This function is ideal for interactive CLI applications where user input needs to be validated against type, minimum, and maximum value constraints.
Features
- Supports dynamic input type conversion (
int,float,str, etc.). - Validates input against optional
min_valueandmax_valueconstraints. - Provides customizable input and error messages.
- Loops until the user provides valid input.
Installation
pip install value-error-exception-handler
Usage
from value_error_exception_handler import value_error_exception_handler
# Example: Asking for an integer between 1 and 10
user_input = value_error_exception_handler(
value_type=int,
input_message="Enter a number between 1 and 10: ",
error_message="Invalid input. Please enter a valid number.",
min_value=1,
max_value=10
)
print(f"You entered: {user_input}")
Function Signature
def value_error_exception_handler(
value_type=int,
input_message="Please enter a number: ",
error_message="Invalid input. Please enter a number.",
max_value=None,
min_value=None
):
"""
Validates and handles user input, ensuring it meets specified criteria.
Parameters:
value_type (type): The type to which user input should be converted (default: int).
input_message (str): Message displayed to prompt the user (default: "Please enter a number: ").
error_message (str): Message displayed on invalid input (default: "Invalid input. Please enter a number.").
max_value (Optional[int/float]): The maximum allowable value for input (default: None).
min_value (Optional[int/float]): The minimum allowable value for input (default: None).
Returns:
user_input: Validated and converted user input.
"""
How It Works
- Prompts the user with a custom message (
input_message). - Attempts to convert user input to the specified
value_type. - Validates the converted input against optional
min_valueandmax_value. - If the input is invalid:
- Displays the
error_messageor a specific constraint violation message. - Re-prompts the user until valid input is received.
- Displays the
- Returns the validated input.
Examples
Basic Example
age = value_error_exception_handler(value_type=int, input_message="Enter your age: ")
print(f"Your age is {age}")
Using Minimum and Maximum Values
score = value_error_exception_handler(
value_type=float,
input_message="Enter your score (0.0 - 100.0): ",
error_message="Please enter a valid score.",
min_value=0.0,
max_value=100.0
)
print(f"Your score is {score}")
Non-Numeric Input
name = value_error_exception_handler(
value_type=str,
input_message="Enter your name: ",
error_message="Please enter a valid name."
)
print(f"Hello, {name}!")
Contribution
Contributions are welcome! If you'd like to contribute to this project, please fork the repository and submit a pull request.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Elevate your CLI application's user experience with robust input validation using value_error_exception_handler!
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 value_exception_handler-0.1.0.tar.gz.
File metadata
- Download URL: value_exception_handler-0.1.0.tar.gz
- Upload date:
- Size: 14.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
821da33d86ec0bdad7d830ace539b196919af7df1a76e98f26813d2e1162762f
|
|
| MD5 |
f746c8d25046e0351dab315858aa8bc3
|
|
| BLAKE2b-256 |
252e983966450746e6f58e8c1f1b42e3d2f38305f1a2fc56d5402046086fcc96
|
File details
Details for the file value_exception_handler-0.1.0-py3-none-any.whl.
File metadata
- Download URL: value_exception_handler-0.1.0-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10dba0b889faf7f2dc1382c4a2cc2d8b59b836700ad59537943cb38e65a95da9
|
|
| MD5 |
1ec550e800c305def11f94f1673be4ad
|
|
| BLAKE2b-256 |
5d00525cef0e3e3dd6068613a3cb1ec9e70e69cdd3daa889476277443054368f
|