1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
# -*- coding: utf-8 -*-
"""Example for a autocomplete question type.
Run example by typing `python -m examples.autocomplete` in your console."""
from pprint import pprint
import questionary
from examples import custom_style_fancy
from questionary import ValidationError
from questionary import Validator
from questionary import prompt
class PolyergusValidator(Validator):
def validate(self, document):
ok = "Polyergus" in document.text
if not ok:
raise ValidationError(
message="Please choose a Polyergus Ant",
cursor_position=len(document.text),
) # Move cursor to end
meta_information = {
"Camponotus pennsylvanicus": "This is an important, destructive pest that"
" attacks fences, poles and buildings",
"Linepithema humile": "It is an invasive species that has been established"
" in many Mediterranean climate areas",
"Eciton burchellii": "Known as army ants, moves almost incessantly"
" over the time it exists",
"Atta colombica": "They are known for cutting grasses and leaves, carrying"
" them to their colonies' nests, and growing fungi on"
" them which they later feed on",
"Polyergus lucidus": "It is an obligatory social parasite, unable to feed"
" itself or look after its brood and reliant on ants"
" of another species of the genus Formica to undertake"
" these tasks.",
"Polyergus rufescens": "Is another specie of slave-making ant.",
}
def ask_pystyle(**kwargs):
# create the question object
question = questionary.autocomplete(
"Choose ant species",
validate=PolyergusValidator,
meta_information=meta_information,
choices=[
"Camponotus pennsylvanicus",
"Linepithema humile",
"Eciton burchellii",
"Atta colombica",
"Polyergus lucidus",
"Polyergus rufescens",
],
ignore_case=False,
style=custom_style_fancy,
**kwargs,
)
# prompt the user for an answer
return question.ask()
def ask_dictstyle(**kwargs):
questions = [
{
"type": "autocomplete",
"name": "ants",
"choices": [
"Camponotus pennsylvanicus",
"Linepithema humile",
"Eciton burchellii",
"Atta colombica",
"Polyergus lucidus",
"Polyergus rufescens",
],
"meta_information": meta_information,
"message": "Choose ant species",
"validate": PolyergusValidator,
}
]
return prompt(questions, style=custom_style_fancy, **kwargs)
if __name__ == "__main__":
pprint(ask_pystyle())
|