pythonfunctionvalidationlambdainquirer

Python: inquirer validate function


Can I use a separate function for inquirer validate function or it has to be lambda function?

I'm trying to ask a question about shift count. So I have to check it's number or not.

I wrote a function that checks its argument number or not and returns a boolean value.

import inquirer
import string

def shiftCount(count):
    for i in count:
        if i in string.digits:
            pass
        else:
            return False
    return True

question = [
    inquirer.Text('count', message='Enter a number',validate=???)
]

answers = inquirer.prompt(questions)

I know that validate function must take two arguments. But I couldn't manage to write it down.


Solution

  • You can use lambda or a user defined function. You can find some implementations here.

    def count_validation(answers, current):
        pass #write your validation logic here. current variable hold the input value
    

    and then call

    question = [
        inquirer.Text('count', message='Enter a number',validate=count_validation)
    ]