Repository navigation
Expand file tree
/
Copy pathquestion.py
More file actions
134 lines (116 loc) · 4.24 KB
/
Copy pathquestion.py
File metadata and controls
134 lines (116 loc) · 4.24 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
"""
Helper class for running scenarios at a command prompt. Asks questions, validates
and converts input, and returns answers.
"""
# snippet-start:[python.example_code.glue.helper.Question]
class Question:
"""
A helper class to ask questions at a command prompt and validate and convert
the answers.
"""
def __init__(self, key, question, *validators):
"""
:param key: The key that is used for storing the answer in a dict, when
multiple questions are asked in a set.
:param question: The question to ask.
:param validators: The answer is passed through the list of validators until
one fails or they all pass. Validators may also convert the
answer to another form, such as from a str to an int.
"""
self.key = key
self.question = question
self.validators = Question.non_empty, *validators
@staticmethod
def ask_questions(questions):
"""
Asks a set of questions and stores the answers in a dict.
:param questions: The list of questions to ask.
:return: A dict of answers.
"""
answers = {}
for question in questions:
answers[question.key] = Question.ask_question(
question.question, *question.validators
)
return answers
@staticmethod
def ask_question(question, *validators):
"""
Asks a single question and validates it against a list of validators.
When an answer fails validation, the complaint is printed and the question
is asked again.
:param question: The question to ask.
:param validators: The list of validators that the answer must pass.
:return: The answer, converted to its final form by the validators.
"""
answer = None
while answer is None:
answer = input(question)
for validator in validators:
answer, complaint = validator(answer)
if answer is None:
print(complaint)
break
return answer
@staticmethod
def non_empty(answer):
"""
Validates that the answer is not empty.
:return: The non-empty answer, or None.
"""
return answer if answer != "" else None, "I need an answer. Please?"
@staticmethod
def is_yesno(answer):
"""
Validates a yes/no answer.
:return: True when the answer is 'y'; otherwise, False.
"""
return answer.lower() == "y", ""
@staticmethod
def is_int(answer):
"""
Validates that the answer can be converted to an int.
:return: The int answer; otherwise, None.
"""
try:
int_answer = int(answer)
except ValueError:
int_answer = None
return int_answer, f"{answer} must be a valid integer."
@staticmethod
def is_letter(answer):
"""
Validates that the answer is a letter.
:return The letter answer, converted to uppercase; otherwise, None.
"""
return (
answer.upper() if answer.isalpha() else None,
f"{answer} must be a single letter.",
)
@staticmethod
def is_float(answer):
"""
Validate that the answer can be converted to a float.
:return The float answer; otherwise, None.
"""
try:
float_answer = float(answer)
except ValueError:
float_answer = None
return float_answer, f"{answer} must be a valid float."
@staticmethod
def in_range(lower, upper):
"""
Validate that the answer is within a range. The answer must be of a type that can
be compared to the lower and upper bounds.
:return: The answer, if it is within the range; otherwise, None.
"""
def _validate(answer):
return (
answer if lower <= answer <= upper else None,
f"{answer} must be between {lower} and {upper}.",
)
return _validate
# snippet-end:[python.example_code.glue.helper.Question]