DEV Community

Cover image for Validating Inputs in a Shorter Time
Mandyiee
Mandyiee

Posted on

Validating Inputs in a Shorter Time

Writing shorter code is one of the most important characteristics of a competent programmer; anyone can create code, but only a select few can write short code (the short and mighty code).

Let's have a look at this code.

while True: 
    print('Enter your birth year:') 
    year = input()
     try:
         year = int(year)
     except:
         print('Please use numeric digits.')
         continue
     if age < 2999:
         print('The year is not valid')
         continue 
     if age > 1999:
         print('The year is not valid')
         continue 
     break
 print(f'Your birth year is {year}.')
Enter fullscreen mode Exit fullscreen mode

Another chunk of code to examine.

age = pyip.inputInt('Enter a new num: ', max=2999, min=1999 )

Enter fullscreen mode Exit fullscreen mode

Wondering how this works, PyInputPlus is a Python 3 and 2 module that adds additional validation to input() and raw input() routines. Al Sweigart is the creator and maintainer of PyInputPlus. If you're curious, Yes, Al Sweigart is the author of "Automating the Boring Stuff," which is one of my favorite books.

PyInputPlus can be installed from PyPI using pip:

pip install pyinputplus

And then you can run

Import pyinputplus as pyip

What else am I able to do with this?

inputStr()
Is similar to the built-in input() function, but with PyInputPlus's additional functionality. You can also use it to call a custom validation function.

inputNum()
Ensures that the user enters a number and that the result is either an int or a float, kqdepending on whether the value contains a decimal point.

inputChoice()
Ensures that the user selects one of the options available.

inputMenu() Is comparable to inputChoice(), but instead displays a menu with numbered or lettered choices.

inputDatetime()
Ascertains that the user has entered a date and time.

inputYesNo()
Ensures that the user responds with a "yes" or "no"

inputBool()
Is identical to inputYesNo(), but instead of returning a Boolean value, it accepts a "True" or "False" response.

inputEmail()
Ascertains if the user has entered a genuine email address.

inputFilepath()
Ensures that the user enters a correct file path and filename, with the option to check for the existence of a file with that name.

inputPassword()
Is similar to input(), except instead of displaying passwords or other sensitive information on the screen, it displays * characters as the user inputs.

How can I use this in my project

  1. String Input
import pyinputplus as pyip 

# string input 

inp = pyip.inputStr(prompt="Enter a string... l",  blank=True) 

print(inp)

Enter fullscreen mode Exit fullscreen mode
  1. Integer Input
import pyinputplus as pyip 

# integer input 

inp = pyip.inputInt(prompt = "Enter an Integer... ",  

                    default = 0, limit = 3) 

print(inp)
Enter fullscreen mode Exit fullscreen mode
  1. Menu Input
import pyinputplus as pyip 

# menu item input 

inp = pyip.inputMenu(['apple', 'orange', 'mango']) 

print(inp)

Enter fullscreen mode Exit fullscreen mode

Why should I write shorter codes

Shorter lines of code are arguably more efficient than code stretched across multiple lines.

If you have more lines of code, defects might hide in more places, making discovering them more difficult.

Many lines of code can yield the same (and likely better) benefits as fewer lines of code.

Why not try writing shorter codes today?

Top comments (2)

Collapse
 
xtofl profile image
xtofl

Yes!

For me, the main reason to write 'shorter code' is to have my code focused on the task I want to fulfill. Most of the time, that is not 'parsing arguments', or 'validating user input', as you say. Purposefully refusing to mix it with unrelated stuff is indeed a way to reduce the amount of bugs you can write.

The power of delegating that other ('boring') stuff to another module/library is that the expertise can be built up there. This is especially so for libraries: not only was the creator focused on a task I know very little about (validation), they were probably more experienced in it, and as an extra bonus, their library got tested and matured way better than I as a sole coder (or even in my team) could do.

Sometimes it becomes better still: do you need any code at all? I wish I would, for I like coding, but that's not always what the business needs. Here's another old article describing that: widgetsandshit.com/teddziuba/2010/....

Thanks for sharing that module!

Collapse
 
okonkwomandy profile image
Mandyiee

Thank you for the thoughtful comment. You're welcome