Skip to main content

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

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

columbo-0.14.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

columbo-0.14.0-py3-none-any.whl (13.6 kB view details)

Uploaded Python 3

File details

Details for the file columbo-0.14.0.tar.gz.

File metadata

  • Download URL: columbo-0.14.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for columbo-0.14.0.tar.gz
Algorithm Hash digest
SHA256 8cb3d14c5269c6d6f207e83c0c3f347cdfd133fb52536f52c775cffd4f29a7ef
MD5 0461cdfb8f48b1cbae4e150f57263c5e
BLAKE2b-256 cfca03e42aa3140568ac080cd2443d2f1789d1423f61e599808ab42dd6eb3b26

See more details on using hashes here.

File details

Details for the file columbo-0.14.0-py3-none-any.whl.

File metadata

  • Download URL: columbo-0.14.0-py3-none-any.whl
  • Upload date:
  • Size: 13.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for columbo-0.14.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8cf13a09d14849fc1adac9e97d6b2bc0e8a928dfa3d9a3848d94e2d05c9636f8
MD5 22566f0574ea26ab060edb400bd72d5e
BLAKE2b-256 0a526630765a83d30d873f1da7a3c3f106604cfbc992d22405e6a7638859932a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page