A simple lightweight library to avoid writting common input checks
Project description
valid_input
valid_input is a lightweight Python library for performing commonly used input checks like make the user inputs a valid int or float or an especific option
Index
Installation
You can install input_tools using pip:
pip install valid_input
Usage
You can pass to all the examples below an additional parameter error_msg to override the default error message of the input function
input_int
from valid_input.tools import input_int
age = input_int("Introduce your age: ") # Returns an integer
input_float
from valid_input.tools import input_float
height = input_float("What's your height? (in meters): ") # Returns a float
input_option
from valid_input.tools import input_option
# Returns the element chosen from the list
sex = input_option("What's your sex? (male/female/other): ", ["male", "female", "other"])
# The returned value will be the original element in the list
# For example, if we have this list:
options = [1, 2, "3", 4]
# The user can choose between 1, 2, 3 or 4
option = input_option("Choose a number between 1 and 4: ", options)
# If the user chooses 3, the returned value will be "3" as string, else it will be the chosen number as integer
input_yes_no
from valid_input.tools import input_yes_no
# Returns True if the user inputs "yes" or "y" or False if the user inputs "no" or "n" (case insensitive)
is_student = input_yes_no("Are you a student? (yes/no): ")
input_date
from valid_input.tools import input_date
# Returns a datetime object (by default expects the date in the format YYYY-MM-DD")
birthday = input_date("Introduce your birthday (YYYY-MM-DD): ")
# You can specify the date format
birthday = input_date("Introduce your birthday (DD/MM/YYYY): ", date_format="%d/%m/%Y")
input_with_length
from valid_input.tools import input_with_length
# Returns a string with defined minimum and maximum length
name = input_with_length("Introduce your name: ", min_length=3, max_length=50)
# You can define only the minimum or maximum length
name = input_with_length("Introduce your name: ", min_length=3) # Returns a string with at least 3 characters
name = input_with_length("Introduce your name: ", max_length=50) # Returns a string with at most 50 characters
input_list
from valid_input.tools import input_list
# Returns a list of elements that match the validation function
numbers = input_list("Introduce a list of numbers separated by commas: ", item_validation_func=int)
# You can define a custom separator
numbers = input_list("Introduce a list of numbers separated by semicolons: ", item_validation_func=int, separator=";")
# Also you can allow an empty list (by default is not allowed)
numbers = input_list("Introduce a list of numbers separated by commas: ", item_validation_func=int, allow_empty=True)
input_num_in_range
from valid_input.tools import input_num_in_range
# Returns a number in the specified range
age = input_num_in_range("Introduce your age: ", 0, 120) # Returns a number between 0 and 120
# The range is inclusive
# You can define only the lower or upper limit
age = input_num_in_range("Introduce your age: ", min_value=18) # Returns a number greater or equal to 18
age = input_num_in_range("Introduce your age: ", max_value=120) # Returns a number less or equal to 120
# You can also disallow the user to input a float number
age = input_num_in_range("Introduce your age: ", 0, 120, allow_float=False) # Returns an integer between 0 and 120
input_with_regex
from valid_input.tools import input_with_regex
# Returns a string that matches the regex
isbn = input_with_regex("Introduce the ISBN of the book: ", r"\d{9}[\d|X]")
validated_input
from valid_input.tools import validated_input
# Returns a string that passes the validation function
def validation_func(value: str):
value = int(value)
if value % 2 != 0:
raise ValueError("The number must be even")
return value
number = validated_input("Introduce an even number: ", validation_func)
License
valid_input is distributed under the MIT License.
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 valid_input-1.0.0.tar.gz.
File metadata
- Download URL: valid_input-1.0.0.tar.gz
- Upload date:
- Size: 5.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05aca3ad8ab0e53d015670f42d6fad0341c9bbcc581db5f7f9c210e3f0aeb29a
|
|
| MD5 |
5d1896727493da90b580385c6839f2bf
|
|
| BLAKE2b-256 |
5d7eab7211d52f8e7788e2dd0099a1370c79c1115211dceaa55757d527e50e19
|
File details
Details for the file valid_input-1.0.0-py3-none-any.whl.
File metadata
- Download URL: valid_input-1.0.0-py3-none-any.whl
- Upload date:
- Size: 6.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
951936a4b36163bc3a768bedcd6719993f82061cf6304ad925dd08e890c3d39e
|
|
| MD5 |
a62e8c859c1e193f52a1351580fa5a2a
|
|
| BLAKE2b-256 |
f9de3b016d883278af4c9f36003e1d4f968f975092b9ec083b237447604f84fb
|