DictMenuPrint#

class pybear.base.DictMenuPrint(MENU_DICT, *, disp_width=80, fixed_col_width=None, allowed=None, disallowed=None)#

Bases: object

Manage the display and operation of an interactive user menu.

Display the allowed sub-menu options from a full menu to the screen.

The choose() method displays the allowed menu options and offers a validated prompt to select one of the options.

Parameters:
MENU_DICTdict[str, str]

Required. A Dictionary of unit-length alpha characters as keys and the descriptions of their associated actions as values. Keys are case-sensitive.

disp_widthint, default=80

The maximum number of characters to display per line.

fixed_col_widthint | None, default=None

Set a fixed width for each column of menu items in the display. DMP will determine a number of columns that causes the overall width of the display to be less than or equal to disp_width.

allowedstr | None, default=None

Can only enter this if disallowed is not entered, cannot enter both. The action keys that are allowed to be selected from the full selection available in MENU_DICT. case-sensitive. Enter as a contiguous sequence of characters.

disallowedstr | None, default=None

Can only enter this if allowed is not entered, cannot enter both. The action keys that are not allowed to be selected from MENU_DICT. allowed becomes the space of action keys that are not disallowed. Case-sensitive. Enter as a contiguous sequence of characters.

Attributes:
allowedstr

The positive space out of MENU_DICT that the user is allowed to select from. This attribute is always the set of allowed options determined at instantiation. The instance attribute IS NOT overwritten by any allowed or disallowed passed to the methods. The allowed passed to or determined by the methods is used only temporarily in place of the permanent allowed attribute. The menu associated with the stored allowed attribute is always available as the default menu.

all_allowed_strstr

The full set of possible options taken from the keys of MENU_DICT.

disp_widthint

The maximum character display width passed at instantiation or the default if not passed.

fixed_col_widthint

The fixed column width within the full display width passed at instantiation or the default if not passed.

MENU_DICTdict[str, str]

The MENU_DICT passed at instantiation.

Methods

choose(prompt, *[, allowed, disallowed])

Displays the allowed menu options to the screen.

Examples

>>> MENU_DICT = {
...    'a': 'Apply Option 1', 'b': 'Apply Option 2',
...    'c': 'Apply Option 3', 'd': 'Apply Option 4'
... }
>>> DMP = DictMenuPrint(
...    MENU_DICT,
...    disp_width = 80,
...    fixed_col_width = None,
...    allowed = 'abcd',
...    disallowed = None
... )
>>> DMP.choose(allowed='ab', prompt='pick one')
a) Apply Option 1     b) Apply Option 2
pick one >
choose(prompt, *, allowed=None, disallowed=None)#

Displays the allowed menu options to the screen.

Prompts the user for a case-sensitive selection. Returns the single-character action command selected by the user. The default menu associated with the allowed action keys that were passed at instantiation is available by passing no kwargs, or pass custom allowed or disallowed to generate a different menu for the one time. The custom allowed or disallowed that are passed here DO NOT overwrite the allowed attribute of the instance, that will always be available as the default menu.

Parameters:
allowedstr | None, default=None

Can only enter this if disallowed is not entered, cannot enter both. The action keys that are allowed to be selected from the full section available in MENU_DICT. Case-sensitive. Enter as a contiguous sequence of characters.

disallowedstr | None, default=None

Can only enter this if allowed is not entered, cannot enter both. The action keys that are not allowed to be selected from MENU_DICT. allowed becomes the space of action keys that are not disallowed. Case-sensitive. Enter as a contiguous sequence of characters.

Returns:
charstr

The value of the menu item selected by the user.