Module inputs¶
Functions for terminal input.
New in version 0.3.0.
Changed in version 0.16.0: The exc_on_cancel
parameters of the functions read
, select
,
yesno
, and menu
now default to None
. This means that the value
of the new module level attribute exception_on_cancel
will be used. As
this defaults to True
the default behavior of the functions has changed.
To restore the old behavior set exception_on_cancel
to False
.
The terminal must support ANSI escape sequences.
Overview¶
Default value for |
|
Return a check function for floats. |
|
Return a check function for integers. |
|
Return a check function for strings. |
|
Show a simple menu. |
|
Read a line of input and check if it is allowed. |
|
Show an input with selectable options. |
|
Show yes/no input prompt. |
-
salmagundi.inputs.
exception_on_cancel
= True¶ Default value for
exc_on_cancel
parameters.
-
salmagundi.inputs.
check_float
(predicate)[source]¶ Return a check function for floats.
The returned function can be used as the
check
argument in theread()
function.- Parameters
predicate (callable(float)) – predicate function
New in version 0.6.0.
-
salmagundi.inputs.
check_int
(predicate)[source]¶ Return a check function for integers.
The returned function can be used as the
check
argument in theread()
function.- Parameters
predicate (callable(int)) – predicate function
New in version 0.6.0.
-
salmagundi.inputs.
check_str
(min_len=0, max_len=None, chars=None, negate=False)[source]¶ Return a check function for strings.
The returned function can be used as the
check
argument in theread()
function.- Parameters
New in version 0.6.0.
Show a simple menu.
If the input is not allowed the prompt will be shown again. The input can be cancelled with EOF (
^D
).The caller has to take care that the menu will fit in the terminal.
def update(): ... def sort(desc=True, duration=True): ... items = ( ('Update', update), ('Sort duration desc', sort), ('Sort duration asc', sort, False), ('Sort size desc', sort, True, False), ('Sort size asc', sort, False, False), ) i = menu('> ', tuple(x[0] for x in items)) print() if i is not None: items[i][1](*items[i][2:])
[1] Update [2] Sort duration desc [3] Sort duration asc [4] Sort size desc [5] Sort size asc >
- Parameters
prompt (str) – the prompt
titles (tuple) – the titles of the menu options
cols (int) – number of columns
col_by_col (bool) – if
True
the menu will be filled column-by-column, otherwise row-by-rowexc_on_cancel (bool) – if
True
an EOF will cause an Exception; ifNone
the value ofexception_on_cancel
will be usedcaption (str) – caption for the menu
default (int) – number of the default menu option
- Returns
index of the selected option in
titles
or None if cancelled andexc_on_cancel=False
- Return type
- Raises
New in version 0.4.0.
Changed in version 0.6.0: Add parameter
caption
Changed in version 0.17.0: Add parameter
default
-
salmagundi.inputs.
read
(prompt='', default=None, check=None, exc_on_cancel=None, *, noecho=False)[source]¶ Read a line of input and check if it is allowed.
If the input is not allowed the prompt will be shown again. The input can be cancelled with EOF (
^D
).If the
check
parameter is set toNone
any input is allowed, else it must be acallable
that takes a string as a parameter and returns the (converted) input value or raisesValueError
if the input is not allowed.There are 3 predefined check functions in this module:
check_str()
,check_int()
andcheck_float()
.>>> read('Number: ', default='42') Number: 21 '21' >>> read('Number: ', default='42', check=int) Number: 42
- Parameters
prompt (str) – the prompt
default (str or None) – default value that will be used if no input is provided
check (callable(str) or None) – the check parameter (see above)
exc_on_cancel (bool) – if
True
an EOF will cause an Exception; ifNone
the value ofexception_on_cancel
will be usednoecho (bool) – if set to
True
getpass()
will be used instead ofinput()
- Returns
(converted) input value or
None
if input was cancelled andexc_on_cancel=False
- Return type
str or return-type of the
check
callable or None- Raises
Changed in version 0.15.0: Add parameter
noecho
-
salmagundi.inputs.
select
(prompt, options, default=None, case_sensitive=False, exc_on_cancel=None)[source]¶ Show an input with selectable options.
If the input is not allowed the prompt will be shown again. The input can be cancelled with EOF (
^D
).>>> select('Select: [T]op, [B]ottom, [L]eft, [R]ight > ', 'TBLR') Select: [T]op, [B]ottom, [L]eft, [R]ight > b 1
- Parameters
prompt (str) – the prompt
options (str or tuple) – the options; if all options are only 1 character a string can be used, else a tuple of strings
default (str or None) – default value that will be used if no input is provided
case_sensitive (bool) – if
False
case of typed characters will be ignoredexc_on_cancel (bool) – if
True
an EOF will cause an Exception; ifNone
the value ofexception_on_cancel
will be used
- Returns
index of the selected option in
options
or None if cancelled andexc_on_cancel=False
- Return type
- Raises
New in version 0.4.0.
-
salmagundi.inputs.
yesno
(prompt, yesno, exc_on_cancel=None)[source]¶ Show yes/no input prompt.
If the typed character (case-insensitive) is not allowed (i.e. not in
yesno
) the prompt will be shown again. The input can be cancelled with EOF (^D
).>>> yesno('Exit program?', 'yN') Exit program? [yN] Y True
- Parameters
- Returns
True
foryes
,False
forno
,None
if cancelled andexc_on_cancel=False
- Return type
- Raises
EOFError – if input was cancelled and
exc_on_cancel=True
TypeError – if
yesno
is not of typestr
ValueError – if
yesno
is not of length 2