Skip to main content

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_value and max_value constraints.
  • 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

  1. Prompts the user with a custom message (input_message).
  2. Attempts to convert user input to the specified value_type.
  3. Validates the converted input against optional min_value and max_value.
  4. If the input is invalid:
    • Displays the error_message or a specific constraint violation message.
    • Re-prompts the user until valid input is received.
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

value_exception_handler-0.1.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

value_exception_handler-0.1.0-py3-none-any.whl (15.5 kB view details)

Uploaded Python 3

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

Hashes for value_exception_handler-0.1.0.tar.gz
Algorithm Hash digest
SHA256 821da33d86ec0bdad7d830ace539b196919af7df1a76e98f26813d2e1162762f
MD5 f746c8d25046e0351dab315858aa8bc3
BLAKE2b-256 252e983966450746e6f58e8c1f1b42e3d2f38305f1a2fc56d5402046086fcc96

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for value_exception_handler-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 10dba0b889faf7f2dc1382c4a2cc2d8b59b836700ad59537943cb38e65a95da9
MD5 1ec550e800c305def11f94f1673be4ad
BLAKE2b-256 5d00525cef0e3e3dd6068613a3cb1ec9e70e69cdd3daa889476277443054368f

See more details on using hashes here.

Supported by

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