python library for user input
Asked Answered
C

5

6

I am implementing a small command line tool in python that needs to ask the user a couple of questions. I use

raw_input('Are you male or female?')

all the time. Now I would like to be able to deal with dumb users (or those too lazy to read/remember the documentation), so I need to check whether the answer makes sense.

gender = ''
while gender not in ['male', 'female']:
    gender = raw_input('Are you male or female?')

I am wondering whether there exists something like argparse that would automate this problem, something like

import inputparse 
gender = inputparse.get_input(prompt='Are you male or female?', type=str, possible_input=['male', 'female'])

and would take care of automatic checking etc.?

Cadena answered 2/6, 2013 at 16:14 Comment(2)
Given what you've shown, it's easy enough to write your own. Your question is whether there's a standard or common library to do it?Carrington
You're right, it's simple to do, but just like argparse takes care of much more than I would implement myself (automatically generated --help etc), I would have thought there is something comparable for this problemCadena
E
2

Necromanting again...

Please have a look at click if you need a simple helper library for questions. Its main focus are command line options, but it’s a good fit for your use case, I think.

EDIT 3 years later: I’m using prompt toolkit nowadays.

Elephus answered 18/10, 2017 at 8:44 Comment(1)
Isn't prompt toolkit if you are using (shudders) Vi or EMacs for your programming? lolRyeland
K
4

This question is quite old, but I'm researching it today. The library pyinputplus is recommended by Al Swigert in Automate the Boring Stuff With Python

Kindrakindred answered 24/6, 2020 at 11:18 Comment(0)
C
3

From the accepted answer to this question: the cmd library might be of interest to you.

"The Cmd class provides a simple framework for writing line-oriented command interpreters."

This Python Module of the Week page features it, and it has some examples and explanations.

Cribbing answered 20/10, 2015 at 20:18 Comment(0)
E
2

Necromanting again...

Please have a look at click if you need a simple helper library for questions. Its main focus are command line options, but it’s a good fit for your use case, I think.

EDIT 3 years later: I’m using prompt toolkit nowadays.

Elephus answered 18/10, 2017 at 8:44 Comment(1)
Isn't prompt toolkit if you are using (shudders) Vi or EMacs for your programming? lolRyeland
C
1

I don't know if such a library exists, but you could write an high-order function like this:

def check_input(predicate, msg, error_string="Illegal Input"):
    while True:
        result = input(msg).strip()
        if predicate(result):
            return result
        print(error_string)

result = check_input(lambda x: x in ['male', 'female'],
                                   'Are you male or female? ')
print(result)

Output:

Are you male or female? foo
Illegal Input
Are you male or female? bar
Illegal Input
Are you male or female? Male
Illegal Input
Are you male or female? male
male
Ceylon answered 2/6, 2013 at 16:20 Comment(0)
I
1

I stumbled in this thread looking for a similar library and i was quite disappointed for the fact that there isn't one, so i wrote one. I will work a lot on this in the next days because i need a lot more features for what i'm writing.

pickone

Incredulous answered 24/10, 2013 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.