-
Notifications
You must be signed in to change notification settings - Fork 236
/
pizza.py
111 lines (99 loc) · 2.95 KB
/
pizza.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# -*- 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 prompt_toolkit.validation import Validator, ValidationError
from PyInquirer import prompt
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.prompt(questions, style=custom_style_3)
print('Order receipt:')
pprint(answers)