Skip to main content

Framework for asking questions

Project description

Question Framework helps you to ask questions and get answers in a declarative way!

Question Framework

Basic Usage

from question_framework.question import Question, RepeatedQuestion, BranchedQuestion
from question_framework.user_input import ask

questions = [Question("Name", "Your name:")]
answers = ask(questions)
print(answers)

Output:

Your name:
foobar
{'Name': 'foobar'}

Question Types

Question

Question is basically a question with an answer.

questions = [Question("Name", "Your name:")]
answers = ask(questions)
print(answers)

Output:

Your name:
John Doe
{'Name': 'John Doe'}

Repeated Question

RepeatedQuestion can be used to ask same question consecutively.

questions = [RepeatedQuestion("Password", "Your password:", 2)]
answers = ask(questions)
print(answers)

Output:

Your password:
123
Your password:
321
Your password:
765
{'Password': ['123', '321', '765']}

Branched Question

BranchedQuestion can be used to create one way adventures.

game = [BranchedQuestion("Main", "Where to go? [N | E | S | W]", [
    Question("N", "North is cold. You died! (type anything to exit)"),
    Question("E", "You trigerred the trap. (type anything to exit)"),
    BranchedQuestion("S", "You found a tresure chest! [open | leave]", [
        Question("open", "It was a trap! (type anything to exit)"),
        Question("leave", "You leave the cave.. (type anything to exit)"),
    ]),
    Question("W", "West is wild, you died! (type anything to exit)"),
])]
answers = ask(game)

Static Answers

"StaticAnswer" can be used to provide a default value.

from question_framework.question import BranchedQuestion, StaticAnswer, Question
questions = [BranchedQuestion("password", "Do you want to enter a password? [y|n]", [
    Question("y", "What is your password?"),
    StaticAnswer("n", "No password.")
])]
answers = ask(questions)

Output:

Do you want to enter a password? [y|n]
n
{'password': {'n': 'No password.', '__answer': 'n'}}

Validations

A validation function can be specified to validate answers. If validation fails, user will be asked to enter the input again.

Question("Password", "Enter password:", validation=lambda x: len(x) > 5)

Validation Error Messages

When a user provides input that does not satify a validation function, it may be desireable to give them a message. The ValidationError exception allows this.

To use, raise the ValidationError exception from your validation function with your desired message.

from question_framework.question import Question
from question_framework.user_input import ask
from question_framework.validation import ValidationError

def is_not_blank(x):
    if not x:
        raise ValidationError("Your answer may not be blank.")
    return True

questions = [Question("Name", "Your name:", validation=is_not_blank)]
answers = ask(questions)

Output:

Your name:
Your answer may not be blank.
Your name:
David
{'Name': 'David'}

Post process

A post process can be specified to transform answer.

Question("Firstname", "Enter firstname:", post_process=lambda x: x.upper())

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

question_framework-0.2.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

question_framework-0.2.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file question_framework-0.2.0.tar.gz.

File metadata

  • Download URL: question_framework-0.2.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.9

File hashes

Hashes for question_framework-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a0860596bbaad55fac9bf3d524276a7ccb933e37eb602c5635a6814e22f195d3
MD5 f01cc8b8e20d58005acd803d1c6bb6de
BLAKE2b-256 d2ee71be26119ff31e232c09bb594d3e229db5b90a40b075ae890c4170b2aa2c

See more details on using hashes here.

File details

Details for the file question_framework-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: question_framework-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.1.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.9

File hashes

Hashes for question_framework-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 024968f8471e59d7e0e282e44d2a612085a04bfc8b7bcbac50891e085acdd15e
MD5 9a8f54e09c6cc67c77d8b9692ea787bb
BLAKE2b-256 cbc803dd0c5e513853cef46ad2543d514286c73fec101ca1b917a8867b7f94d4

See more details on using hashes here.

Supported by

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