Skip to content

Specify a dynamic set of questions to ask a user and get their answers.

License

Notifications You must be signed in to change notification settings

plannigan/columbo

Repository files navigation

CI pipeline status PyPI PyPI - Python Version codecov Checked with mypy Code style: black

Columbo

Specify a dynamic set of questions to ask a user and get their answers.

columbo's feature set allows a program to:

  • Ask multiple types of questions:
    • Yes or No
    • Multiple choice
    • Open-ended
  • Validate the response provided by the user.
  • Use answers from earlier questions:
    • As part of the text of a question
    • As part of the text of a default value
    • To decide if a question should be skipped
  • Accept answers from the command line in addition to prompting the user.

Example

User Prompts

The primary use of columbo is to define a sequence of interactions that are used to prompt a user to provide answers using a terminal. Below is a sample which shows some ways this can be used.

import columbo

interactions = [
    columbo.Echo("Welcome to the Columbo example"),
    columbo.Acknowledge(
        "Press enter to start"
    ),
    columbo.BasicQuestion(
        "user",
        "What is your name?",
        default="Patrick",
    ),
    columbo.BasicQuestion(
        "user_email",
        lambda answers: f"""What email address should be used to contact {answers["user"]}?""",
        default="me@example.com"
    ),
    columbo.Choice(
        "mood",
        "How are you feeling today?",
        options={
            "happy": "😀",
            "sad": "😢",
            "sleepy": "🥱",
            "confused": "🤔",
        },
        default="happy",
    ),
    columbo.Confirm("likes_dogs", "Do you like dogs?", default=True),
]

answers = columbo.get_answers(interactions)
print(answers)

Below shows the output when the user accepts the default values for most of the questions. The user provides a different value for the email and explicitly confirms that they like dogs.

Welcome to the Columbo example
Press enter to start
 
What is your name? [Patrick]:

What email address should be used to contact Patrick? [me@example.com]: patrick@example.com

How are you feeling today?
1 - 😀
2 - 😢
3 - 🥱
4 - 🤔
Enter the number of your choice [1]:

Do you like dogs? (Y/n): y

{'user': 'Patrick', 'user_email': 'patrick@example.com', 'mood': 'happy', 'likes_dogs': True}

Command Line Answers

In addition to the interactive prompts, columbo can also parse command line arguments for interactions. This is done by changing columbo.get_answers() to columbo.parse_args(). Below shows the output when using the same interactions from above.

$ python columbo_example.py --user-email patrick@example.com --likes-dogs
{'user': 'Patrick', 'user_email': 'patrick@example.com', 'mood': 'happy', 'likes_dogs': True}
The full example
import columbo

interactions = [
    columbo.Echo("Welcome to the Columbo example"),
    columbo.Acknowledge(
        "Press enter to start"
    ),
    columbo.BasicQuestion(
        "user",
        "What is your name?",
        default="Patrick",
    ),
    columbo.BasicQuestion(
        "user_email",
        lambda answers: f"""What email address should be used to contact {answers["user"]}?""",
        default="me@example.com"
    ),
    columbo.Choice(
        "mood",
        "How are you feeling today?",
        options=["happy", "sad", "sleepy", "confused"],
        default="happy",
    ),
    columbo.Confirm("likes_dogs", "Do you like dogs?", default=True),
]

answers = columbo.parse_args(interactions)
print(answers)

Documentation

Check out the project documentation.

For an overview on how repository structure and how to work with the code base, read the Development Guide.

Credits

The development of this project was originally funded and incubated by Wayfair.