Categories
Python

Validating Python Inputs with PyInputPlus

Spread the love

Python is a convenient language that’s often used for scripting, data science, and web development.

In this article, we’ll look at how to validating Python inputs with the PyInputPlus.

Validation Inputs

We use the PyInputPlus package to validate inputs from retrieved from the command line.

To use it, we have to install it. We run pip install pyinputplus to install the package.

Then we can use the following functions in the module to validate inputs:

  • inputStr — it’s like input but we can validate custom validations into it.
  • inputNum — ensures user enters a number and returns an int or float depending on if the number has a decimal point or not
  • inputChoice — ensures user enters of one the provided choices
  • inputMenu — it’s like inputChoice but provides a menu with number or lettered options
  • inputDatetime — ensures user enter a date and time
  • inputYesNo — ensures user enters yes or no
  • inputBool — ensures user enters True or False
  • inputEmail — ensures user enters email address
  • inputFilePath — checks that a user enters a file path
  • inputPassword — like input , but displays * in place of whatever is entered

We can use the module as follows:

import pyinputplus  
print('What is your age?')  
age = pyinputplus.inputNum()  
print('Your age is', age)

The code above asks user to enter their age. If they enter their age, then we display the last line.

Otherwise, we display an error message until they enter a number.

We can pass in the prompt text to the function. For instance, we can write:

import pyinputplus  
age = pyinputplus.inputNum(prompt='What is your age?')  
print('Your age is', age)

It works the same way as before except the 'What is your age?’ message doesn’t add a new line.

The min, max, greaterThan, and lessThan Keyword Arguments

We can check if the number entered is in the range we want with min , max , greaterThan , and lessThan keywords.

They do as their names suggest. For instance:

import pyinputplus  
age = pyinputplus.inputNum(prompt='What is your age?',  min=0)  
print('Your age is', age)

The code above will check if a number 0 or bigger is entered. If we enter an invalid number, we’ll get an error message until we enter a valid number.

The blank Keyword Argument

We can disallow blank inputs with by passing in a boolean argument for the blank parameter.

For example, we can write:

import pyinputplus  
age = pyinputplus.inputNum(prompt='What is your age?',  min=0, blank=False)  
print('Your age is', age)

Then we get Blank values are not allowed. error if we entered a blank value. We can’t proceed until we enter a valid number.

The limit, timeout, and default Keyword Arguments

PyInputPlus functions will continue to ask the same question until we enter a valid value.

To change this, we can use pass in an argument for the limit parameter to limit the number of tries allowed.

For instance, we can write:

import pyinputplus  
age = pyinputplus.inputNum(prompt='What is your age?',  min=0, limit=2)  
print('Your age is', age)

to limit the number of tries for answer the 'What is your age?' question to 2.

When we don’t enter a valid number after 2 tries, we get an error.

To set a default value, we can pass in an argument to the default parameter. For instance, we can write:

import pyinputplus  
age = pyinputplus.inputNum(prompt='What is your age?',  min=0, default='0', limit=2)  
print('Your age is', age)

When running the code above, if we didn’t enter a valid number after 2 tries, then it’ll print 'Your age is 0' on the screen since we set the default value to 0.

We can set the timeout parameter to limit the time that our program waits for an input to be entered.

For example, we can write:

import pyinputplus  
age = pyinputplus.inputNum(prompt='What is your age?',  timeout=1)  
print('Your age is', age)

After waiting for a second, we’ll get a TimeoutException thrown if nothing is entered.

The allowRegexes and blockRegexes Keyword Arguments

We can set a list of regex strings to the allowRegexes parameter to the PyInputPlus functions.

For example, if we want to make sure the user enters a phone number, we can write:

import pyinputplus  
phone = pyinputplus.inputNum(prompt='What is your phone?',  allowRegexes=[r'\d{3}-\d{3}-\d{4}', r'None'])  
print('Your phone is', phone)

In the code above, we allow phone numbers to be entered as the input value by setting a list of regex with the phone number regex to it.

Then we have to enter a phone number or 'None' before we can proceed.

Conclusion

We can use the PyInputPlus package to validate command line input values.

It has functions to checking various kinds of inputs like numbers, emails, date and time, yes or no, and so on.

Also, we can limit the range of the values that are entered for numbers and so check against regexes so that anything can be checked for the given format.

Leave a Reply

Your email address will not be published. Required fields are marked *