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())
Metadata
Release files for question-framework 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| question_framework-0.2.0.tar.gz | 5.5 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| question_framework-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.6 kB
Release files / question_framework-0.2.0.tar.gz
| Download URL | question_framework-0.2.0.tar.gz |
|---|---|
| Size | 5.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a0860596bbaad55fac9bf3d524276a7ccb933e37eb602c5635a6814e22f195d3
|
|
BLAKE2b-256 checksum How to use checksums |
d2ee71be26119ff31e232c09bb594d3e229db5b90a40b075ae890c4170b2aa2c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Release files / question_framework-0.2.0-py3-none-any.whl
| Download URL | question_framework-0.2.0-py3-none-any.whl |
|---|---|
| Size | 8.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
024968f8471e59d7e0e282e44d2a612085a04bfc8b7bcbac50891e085acdd15e
|
|
BLAKE2b-256 checksum How to use checksums |
cbc803dd0c5e513853cef46ad2543d514286c73fec101ca1b917a8867b7f94d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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
|
Security for question-framework 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.