simpleinput implements functions that retrieve user inputs of some determined type.
The functions defined in this module are designed to be a more robust alternative to Python’s input and raw_input built-ins. All functions expect a certain type of input and keeps asking the user for a input of the correct type if it is not given.
A program that asks for two numbers and prints their sum could be implemented naively as
>>> print('Result: ', input('Two numbers:\n<--') + input())
Two numbers:
<-- 1
<-- 2
Result: 3
This is a poor implementation for two reasons: The input() built-in executes arbitrary Python code that can be exploited by malicious users. If the user types some bad numeric value it will fail by printing a nasty traceback and shutting down the program.
The float_input() function in this library works similarly, but is safer and more user friendly:
>>> print('Result: ', float_input('Two numbers:\n<--') + float_input())
Two numbers:
<-- 1
<-- 2
Result: 3.0
If the user types an invalid numeric string, the new implementation complains and asks for a new valid value:
>>> print('Result: ', float_input('Two numbers:\n<--') + float_input())
Two numbers:
<-- 1_2
error: invalid float!
<-- 1
<-- 2
Result: 3
There are other functions specialized in different types of arguments. See the section API Documentation for extra help.
TODO: document me!
Takes a yes/no boolean input
Parameters : | text : str
yes, no : str
yn : tuple (yes, no)
case : bool
first_letter : bool
error_msg : str
getinput : list or callable
printline : callable
error : callable
|
---|
Examples
>>> value = yn_input('value: ', getinput=['bar', 'y'])
value: bar
error: type "yes" or "no".
value: y
>>> value
True
Similar to yn_input, but the default values are ‘True’ and ‘False’.
Parameters : | Similar to `yn_input`, but the parameters ``yes``, ``no`` are renamed : respectively as ``true``, ``false`` and changing ``yn`` is not : supported. : getinput : list or callable
printline : callable
error : callable
|
---|
Examples
>>> value = tf_input('value: ', getinput=['bar', 'true'])
value: bar
error: type "true" or "false".
value: true
>>> value
True
Takes an integer number as input.
An alias to numeric_input with the parameter readnum set to the builtin function int.
Examples
>>> value = int_input('value: ', getinput=['0.1', '1'])
value: 0.1
error: must be an integer!
value: 1
>>> value
1
Takes an float number as input.
An alias to numeric_input with the parameter readnum set to the builtin function float.
Examples
>>> value = float_input('value: ', getinput=['None', '1'])
value: None
error: must be a float!
value: 1
>>> value
1.0
Expects a numeric input. Numbers can be integers, floating point, complex or fractions.
Parameters : | text : str
start, end : int
error_range : str
error_msg : str
shift : int
readnum : callable
getinput : list or callable
printline : callable
error : callable
|
---|
Examples
>>> value = numeric_input('value: ', 1, 10, getinput=['0.1', '5/2'])
value: 0.1
error: must in range [1..10]
value: 5/2
>>> value
Fraction(5, 2)
Asks user to choose between a set of options.
Parameters : | text : str
choices : iterable
enum : iterable
prompt : bool
case : bool
sep : str
ret_choice : bool or iterable
error_msg : str
getinput : list or callable
printline : callable
error : callable
|
---|
Examples
>>> value = choice_input('Options:', ['foo', 'bar', 'foobar'], getinput=['0', '1'])
Options:
1) foo
2) bar
3) foobar
<-- 0
invalid choice!
<-- 1