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"
Owner
Matthias
I'm a self-taught python developer that's always eager to learn!
Matthias
This script allows you to retrieve all functions / variables names of a Python code, and the variables values.

Memory Extractor This script allows you to retrieve all functions / variables names of a Python code, and the variables values. How to use it ? The si

Venax 2 Dec 26, 2021
Simple RGB to HEX game made in python

Simple RGB to HEX game made in python

5 Aug 26, 2022
DiddiParser 2: The DiddiScript parser.

DiddiParser 2 The DiddiScript parser, written in Python. Installation DiddiParser2 can be installed via pip: pip install diddiparser2 Usage DiddiPars

Diego Ramirez 3 Dec 28, 2022
A small python library that helps you to generate localization strings for your mobile projects.

LocalizationUtiltiy A small python library that helps you to generate localization strings for your mobile projects. This small script aims to help yo

1 Nov 12, 2021
A simple example for calling C++ functions in Python by `ctypes`.

ctypes-example A simple example for calling C++ functions in Python by ctypes. Features call C++ function int bar(int* value, char* msg) with argumene

Yusu Pan 3 Nov 23, 2022
Simple integer-valued time series bit packing

Smahat allows to encode a sequence of integer values using a fixed (for all values) number of bits but minimal with regards to the data range. For example: for a series of boolean values only one bit

Ghiles Meddour 7 Aug 27, 2021
RapidFuzz is a fast string matching library for Python and C++

RapidFuzz is a fast string matching library for Python and C++, which is using the string similarity calculations from FuzzyWuzzy

Max Bachmann 1.7k Jan 04, 2023
Enable ++x and --x expressions in Python

By default, Python supports neither pre-increments (like ++x) nor post-increments (like x++). However, the first ones are syntactically correct since Python parses them as two subsequent +x operation

Alexander Borzunov 85 Dec 29, 2022
Python tool to check a web applications compliance with OWASP HTTP response headers best practices

Check Your Head A quick and easy way to check a web applications response headers!

Zak 6 Nov 09, 2021
Entropy-controlled contexts in Python

Python module ordered ordered module is the opposite to random - it maintains order in the program. import random x = 5 def increase(): global x

HyperC 36 Nov 03, 2022
Dill_tils is a package that has my commonly used functions inside it for ease of use.

DilllonB07 Utilities Dill_tils is a package that has my commonly used functions inside it for ease of use. Installation Anyone can use this package by

Dillon Barnes 2 Dec 05, 2021
Go through a random file in your favourite open source projects!

Random Source Codes Never be bored again! Staring at your screen and just scrolling the great world wide web? Would you rather read through some code

Mridul Seth 1 Nov 03, 2022
Patch the pclntable from Go binaries

Pretrain and Fine-tune a T5 model with Flax on GCP This tutorial details how pretrain and fine-tune a FlaxT5 model from HuggingFace using a TPU VM ava

6 Oct 05, 2022
This repository contains scripts that help you validate QR codes.

Validation tools This repository contains scripts that help you validate QR codes. It's hacky, and a warning for Apple Silicon users: the dependencies

Ryan Barrett 8 Mar 01, 2022
These scripts look for non-printable unicode characters in all text files in a source tree

find-unicode-control These scripts look for non-printable unicode characters in all text files in a source tree. find_unicode_control.py should work w

Siddhesh Poyarekar 25 Aug 30, 2022
Local backup made easy, with Python and shutil

KTBackup BETA Local backup made easy, with Python and shutil Features One-command backup and restore Minimalistic (only using stdlib) Convenient direc

kelptaken 1 Dec 27, 2021
Link-tree - Script that iterate over the links found in each page

link-tree Script that iterate over the links found in each page, recursively fin

Rodrigo Stramantinoli 2 Jan 05, 2022
An okayish python script to generate a random Euler circuit with given number of vertices and edges.

Euler-Circuit-Test-Case-Generator An okayish python script to generate a random Euler circuit with given number of vertices and edges. Executing the S

Alen Antony 1 Nov 13, 2021
DUQ is a python package for working with physical Dimensions, Units, and Quantities.

DUQ is a python package for working with physical Dimensions, Units, and Quantities.

2 Nov 02, 2022
async parser for JET

This project is mainly aims to provide an async parsing option for NTDS.dit database file for obtaining user secrets.

15 Mar 08, 2022