Package: inputvalidation
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...InputValidationA python module to validate input.Supported Operating SystemsThe inputvalidation module should be supported on Windows, Linux and Mac (althought it has not been tested on Mac).InstallationThe inputvalidation module can be install using pip.pip install inputvalidationUsageimportinputvalidationasivAfter you imported the inputvalidation module you're ready to go.# Simple int validatorintValidator=iv.Validator(type=int)intNumber=intValidator.input("Enter an integer: ")# Enter an integer: 15print("\nNumber:",intNumber,type(number))# Number: 15 <class 'int'>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 numbersphoneNumber=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-8901You 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"),# TrueintValidator.validate("0x5"),# FalseintValidator.validate("Hello, world!"),# Falsesep="\n")The inputvalidation module also supports default values for when the user leaves the input field empty.# Validator using the default parameternameValidator=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 DoeIf you want to ask the user a multiple choice question you can use the MultipleChoice validator.# Multiple choice inputmodeValidator=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): Yesprint("\nAnswer:",answer)# Answer: yesYou 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 integersnumberValidator=iv.Validator(type=lambdainp:int(inp.strip().lstrip("0x"),16),# Converts strings to integers using base 16pattern=r"[0x]?[0-9]+",# Regex pattern for hex numberspreCustom=lambdainp:inp.strip().lstrip("0x").isnumeric()# Checks if the input is numeric)number=numberValidator.input("Enter a hex number: ")# Enter a hex number: 0x52print("\nEntered:",number,type(number))# Entered: 82 <class 'int'>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=lambdainp: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"