A python module to validate input.

Overview

Have you ever created a program that takes input from the command line? Have you ever wanted to convert the input to an integer or any other type? If so, you'll know how annoying it can be. Try/except blocks, while loops, multiple input calls and much more... but what if I told you that you can forget about those and just use this module! Introducing...

InputValidation

A python module to validate input.

Supported Operating Systems

The inputvalidation module should be supported on Windows, Linux and Mac (althought it has not been tested on Mac).

Installation

The inputvalidation module can be install using pip.

pip install inputvalidation

Usage

import inputvalidation as iv

After you imported the inputvalidation module you're ready to use validators in your python program.

">
# Simple int validator
intValidator = iv.Validator(type=int)
intNumber = intValidator.input("Enter an integer: ") # Enter an integer: 15
print("\nNumber:", intNumber, type(number)) # Number: 15 
   

Here's a simple validator using regex to make sure the user enters a valid phone number.

phoneValidator = iv.Validator(pattern=r"^\([2-9][\d]{2}\) [\d]{3}-[\d]{4}$") # Regex pattern to match phone numbers
phoneNumber = phoneValidator.input("Enter a phone number: ")
print("\nPhone number:", phoneNumber)

Here's what happens when you run the previous code block.

Enter a phone number: 23980983
Enter a phone number: 24-42653-35
Enter a phone number: (234) 567-8901

Phone number: (234) 567-8901

You don't have to call the input method to use the validator, you can validate strings using the validate method instead.

print(
    intValidator.validate("162"), # True
    intValidator.validate("0x5"), # False
    intValidator.validate("Hello, world!"), # False
sep="\n")

The inputvalidation module also supports default values for when the user leaves the input field empty.

# Validator using the default parameter
nameValidator = iv.Validator(default="John Doe")
name = nameValidator.input(f"Enter your name (default = {nameValidator.default}): ") # Enter your name (default = John Doe): 
print("\nName:", name) # Name: John Doe

If you want to ask the user a multiple choice question you can use the MultipleChoice validator.

# Multiple choice input
modeValidator = iv.MultipleChoice(options=["yes", "no"], default="no", caseSensitive=False)
answer = modeValidator.input(f"Are you sure you want to exit? (default = {modeValidator.default}): ") # Are you sure you want to exit? (default = no): Yes
print("\nAnswer:", answer) # Answer: yes

You can make the validators as complex as you like. Converting the input to a custom type, using regex to make sure the input is valid, running custom functions/lambdas on the input to validate them, etc. (preCustom will be called before all other tests, postCustom will be called after all other tests).

">
# Overcomplicated validator to validate hex numbers and convert them to integers
numberValidator = iv.Validator(
    type=lambda inp: int(inp.strip().lstrip("0x"), 16), # Converts strings to integers using base 16
    pattern=r"[0x]?[0-9]+", # Regex pattern for hex numbers
    preCustom=lambda inp: inp.strip().lstrip("0x").isnumeric() # Checks if the input is numeric
)
number = numberValidator.input("Enter a hex number: ") # Enter a hex number: 0x52
print("\nEntered:", number, type(number)) # Entered: 82 
   

Heres a validator that turns user input into a boolean (the "type" lambda turns the input into a boolean, this lambda will be called after the input is validated, to convert the input string to a boolean, whatever the type function/lambda returns will be used as the output).

boolValidator = iv.MultipleChoice(type=lambda inp: inp == "true", options=["true", "false"], caseSensitive=False)
userInput = boolValidator.input("(true/false): ")

The previous block of code is basically a better version of this (the "type" lambda will be called everytime the input method is called, while the next block of code runs the equality check manually, you'd have to do this everytime you call the input method, this is why using the "type" keyword argument is better).

boolValidator = iv.MultipleChoice(options=["true", "false"], caseSensitive=False)
userInput = boolValidator.input("(true/false): ") == "true"
You might also like...
Python module and its web equivalent, to hide text within text by manipulating bits
Python module and its web equivalent, to hide text within text by manipulating bits

cacherdutexte.github.io This project contains : Python modules (binary and decimal system 6) with a dedicated tkinter program to use it. A web version

A python mathematics module

A python mathematics module

A hashtag from string extract python module

A hashtag from string extract python module

A random cats photos python module

A random cats photos python module

Pyfunctools is a module that provides functions, methods and classes that help in the creation of projects in python

Pyfunctools Pyfunctools is a module that provides functions, methods and classes that help in the creation of projects in python, bringing functional

glip is a module for retrieve ip address like local-ip, global-ip, external-ip as string.

gle_ip_info glip is a module for retrieve ip address like local-ip, global-ip, external-ip as string.

Import the module and create an object of the class LocalVariable.
Import the module and create an object of the class LocalVariable.

LocalVariable Import the module and create an object of the class LocalVariable. Call the save method with the name and the value of a variable as arg

Extends the pyranges module with operations on joined genomic intervals

tiedpyranges Extends the pyranges module with operations on joined genomic intervals (e.g. exons of same transcript) Install with: pip install tiedpyr

Collection of code auto-generation utility scripts for the Horizon `Boot` system module

boot-scripts This is a collection of code auto-generation utility scripts for the Horizon Boot system module, intended for use in Atmosphère. Usage Us

Owner
Matthias
I'm a self-taught python developer that's always eager to learn!
Matthias
Package that allows for validate and sanitize of string values.

py.validator A library of string validators and sanitizers Insipired by validator.js Strings only This library validates and sanitizes strings only. P

Sanel Hadzini 22 Nov 8, 2022
Rabbito is a mini tool to find serialized objects in input values

Rabbito-ObjectFinder Rabbito is a mini tool to find serialized objects in input values What does Rabbito do Rabbito has the main object finding Serial

null 7 Dec 13, 2021
Simple python module to get the information regarding battery in python.

Battery Stats A python3 module created for easily reading the current parameters of Battery in realtime. It reads battery stats from /sys/class/power_

Shreyas Ashtamkar 5 Oct 21, 2022
A python module to manipulate XCode projects

This module can read, modify, and write a .pbxproj file from an Xcode 4+ projects. The file is usually called project.pbxproj and can be found inside the .xcodeproj bundle. Because some task cannot be done by clicking on an UI or opening Xcode to do it for you, this python module lets you automate the modification process.

Ignacio Calderon 1.1k Jan 2, 2023
A python module to update the console without flashing.

A python module to update the console without flashing.

Matthias 112 Dec 19, 2022
A module for account creation with python

A module for account creation with python

Fayas Noushad 3 Dec 1, 2021
A string extractor module for python

A string extractor module for python

Fayas Noushad 4 Jul 19, 2022
A python module for extract domains

A python module for extract domains

Fayas Noushad 4 Aug 10, 2022
An URL checking python module

An URL checking python module

Fayas Noushad 6 Aug 10, 2022
A multipurpose python module

pysherlock pysherlock is a Python library for dealing with web scraping using images, it's a Python application of the rendertron headless browser API

Sachit 2 Nov 11, 2021