Input Helpers. There are a number of common character-mode input operations
that can benefit from using exceptions to simplify error handling.
All of these input operations are based around a loop that
examines the results of raw_input and converts this to expected
Python data.
All of these functions should accept a prompt, a default value
and a help text. Some of these have additional parameters to qualify
the list of valid responses.
All of these functions construct a prompt of the form:
your prompt [valid input hints,?,q]:
If the user enters a ?, the help text is displayed. If the
user enters a q, an exception is raised that indicates that the user
quit. Similarly, if the
KeyboardInterrupt or any end-of-file
exception is received, a user quit exception is raised from the
exception handler.
Most of these functions have a similar algorithm.
Procedure 17.1. User Input Function
Construct Prompt. Construct the prompt with the hints for valid values,
plus ? and q.
While Not Valid Input. Loop until the user enters valid input.
Try the following suite of operations.
Prompt and Read. Use raw_input to prompt and read a reply from the
user.
Help? If the user entered "?", provide the help
message.
Quit? If the user entered "q" or "Q", raise a UserQuit
exception.
Try the following suite of operations
Convert. Attempt any conversion.
Range Check. If necessary, do any range checks. For some
prompts, there will be a fixed list of valid answers.
For other prompts, there is no checking
required.
If the input is valid, break out of the
loop.
In the event of an exception, the user input was
invalid.
Nothing? If the user entered nothing, and there is a default
value, return the default value.
In the event of an exception, this function should
generally raise a UserQuit
exception.
Result. Return the validated user input.
ckdate
Prompts for and validates a date. The basic version
would require dates have a specific format, for example
mm/dd/yy. A more advanced version would accept a
string to specify the format for the
input. Much of this date validation is available in the
time module, which will be covered in
Chapter 32, Dates and Times: the time and
datetime Modules. This function not
reaturn bad dates or other invalid input.
ckint
Display a prompt; verify and return an integer
value
ckitem
Build a menu; prompt for and return a menu item. A
menu is a numbered list of alternative values, the user
selects a value by entering the number. The function should
accept a sequence of valid values, generate the numbers and
return the actual menu item string. An
additional help prompt of "??" should be
accepted, this writes the help message and redisplays the
menu.
ckkeywd
Prompts for and validates a keyword from a list of
keywords. This is similar to the menu, but the prompt is
simply the list of keywords without numbers being
added.
ckpath
Display a prompt; verify and return a pathname. This
can use the os.path module for information on construction of
valid paths. This should use fstat to check the user input to
confirm that it actually exists.
ckrange
Prompts for and validates an integer in a given
range. The range is given as separate values for the lowest
allowed and highest allowed value. If either is not given,
then that limit doesn't apply. For instance, if only a lowest
value is given, the valid input is greater than or equal to
the lowest value. If only a highest value is given, the input
must be less than or equal to the highest value.
ckstr
Display a prompt; verify and return a
string answer. This is similar to the
basic raw_input, except that it provides a simple help feature
and raises exceptions when the user wants to quit.
cktime
Display a prompt; verify and return a time of day.
This is similar to ckdate; a more advanced version would use
the time module to validate inputs. The basic version can
simply accept a
hh:mm:ss
time string and validate it as a legal
time.
ckyorn
Prompts for and validates yes/no. This is similar to
ckkeywd, except that it tolerates a number of variations on
yes (YES, y, Y) and a number of variations on no (NO, n, N).
It returns the canonical forms: Y or N irrespective of the
input actually given.
Published under the terms of the Open Publication License