Created
April 21, 2019 17:33
-
-
Save amirzenoozi/ad7fe17dfd251c55806651acaa0bd9ab to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
* Pizza delivery prompt example | |
* run example by writing `python example/pizza.py` in your console | |
""" | |
from __future__ import print_function, unicode_literals | |
import regex | |
from pprint import pprint | |
from PyInquirer import style_from_dict, Token, prompt | |
from PyInquirer import Validator, ValidationError | |
from examples import custom_style_3 | |
class PhoneNumberValidator(Validator): | |
def validate(self, document): | |
ok = regex.match('^([01]{1})?[-.\s]?\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})\s?((?:#|ext\.?\s?|x\.?\s?){1}(?:\d+)?)?$', document.text) | |
if not ok: | |
raise ValidationError( | |
message='Please enter a valid phone number', | |
cursor_position=len(document.text)) # Move cursor to end | |
class NumberValidator(Validator): | |
def validate(self, document): | |
try: | |
int(document.text) | |
except ValueError: | |
raise ValidationError( | |
message='Please enter a number', | |
cursor_position=len(document.text)) # Move cursor to end | |
print('Hi, welcome to Python Pizza') | |
questions = [ | |
{ | |
'type': 'confirm', | |
'name': 'toBeDelivered', | |
'message': 'Is this for delivery?', | |
'default': False | |
}, | |
{ | |
'type': 'input', | |
'name': 'phone', | |
'message': 'What\'s your phone number?', | |
'validate': PhoneNumberValidator | |
}, | |
{ | |
'type': 'list', | |
'name': 'size', | |
'message': 'What size do you need?', | |
'choices': ['Large', 'Medium', 'Small'], | |
'filter': lambda val: val.lower() | |
}, | |
{ | |
'type': 'input', | |
'name': 'quantity', | |
'message': 'How many do you need?', | |
'validate': NumberValidator, | |
'filter': lambda val: int(val) | |
}, | |
{ | |
'type': 'expand', | |
'name': 'toppings', | |
'message': 'What about the toppings?', | |
'choices': [ | |
{ | |
'key': 'p', | |
'name': 'Pepperoni and cheese', | |
'value': 'PepperoniCheese' | |
}, | |
{ | |
'key': 'a', | |
'name': 'All dressed', | |
'value': 'alldressed' | |
}, | |
{ | |
'key': 'w', | |
'name': 'Hawaiian', | |
'value': 'hawaiian' | |
} | |
] | |
}, | |
{ | |
'type': 'rawlist', | |
'name': 'beverage', | |
'message': 'You also get a free 2L beverage', | |
'choices': ['Pepsi', '7up', 'Coke'] | |
}, | |
{ | |
'type': 'input', | |
'name': 'comments', | |
'message': 'Any comments on your purchase experience?', | |
'default': 'Nope, all good!' | |
}, | |
{ | |
'type': 'list', | |
'name': 'prize', | |
'message': 'For leaving a comment, you get a freebie', | |
'choices': ['cake', 'fries'], | |
'when': lambda answers: answers['comments'] != 'Nope, all good!' | |
} | |
] | |
answers = prompt(questions, style=custom_style_3) | |
print('Order receipt:') | |
pprint(answers) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment