2022-02-16 09:32:09 -08:00
# 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.dynamodb.helper.Question]
class Question :
"""
A helper class to ask questions at a command prompt and validate and convert
the answers.
"""
2023-10-18 10:35:05 -07:00
2022-02-16 09:32:09 -08:00
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 (
2023-10-18 10:35:05 -07:00
question . question , * question . validators
)
2022-02-16 09:32:09 -08:00
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.
2022-02-17 15:48:22 -08:00
:param validators: The list of validators that the answer must pass.
2022-02-16 09:32:09 -08:00
: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 ) :
"""
2022-02-17 15:48:22 -08:00
Validates that the answer is not empty.
2022-02-16 09:32:09 -08:00
:return: The non-empty answer, or None.
"""
2023-10-18 10:35:05 -07:00
return answer if answer != " " else None , " I need an answer. Please? "
2022-02-16 09:32:09 -08:00
@staticmethod
def is_yesno ( answer ) :
"""
Validates a yes/no answer.
:return: True when the answer is ' y ' ; otherwise, False.
"""
2023-10-18 10:35:05 -07:00
return answer . lower ( ) == " y " , " "
2022-02-16 09:32:09 -08:00
@staticmethod
def is_int ( answer ) :
"""
2022-02-17 15:48:22 -08:00
Validates that the answer can be converted to an int.
2022-02-16 09:32:09 -08:00
: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 ) :
"""
2022-02-17 15:48:22 -08:00
Validates that the answer is a letter.
:return The letter answer, converted to uppercase; otherwise, None.
2022-02-16 09:32:09 -08:00
"""
2023-10-18 10:35:05 -07:00
return (
answer . upper ( ) if answer . isalpha ( ) else None ,
f " { answer } must be a single letter. " ,
)
2022-02-16 09:32:09 -08:00
@staticmethod
def is_float ( answer ) :
"""
2022-02-17 15:48:22 -08:00
Validate that the answer can be converted to a float.
:return The float answer; otherwise, None.
2022-02-16 09:32:09 -08:00
"""
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 ) :
"""
2022-02-17 15:48:22 -08:00
Validate that the answer is within a range. The answer must be of a type that can
2022-02-16 09:32:09 -08:00
be compared to the lower and upper bounds.
:return: The answer, if it is within the range; otherwise, None.
"""
2023-10-18 10:35:05 -07:00
2022-02-16 09:32:09 -08:00
def _validate ( answer ) :
return (
answer if lower < = answer < = upper else None ,
2023-10-18 10:35:05 -07:00
f " { answer } must be between { lower } and { upper } . " ,
)
2022-02-16 09:32:09 -08:00
return _validate
2023-10-18 10:35:05 -07:00
2022-02-16 09:32:09 -08:00
# snippet-end:[python.example_code.dynamodb.helper.Question]