Dict IO Class#

class expectmine.io.io.dict_io.DictIo(answers: dict[str, object], **kwargs: Dict[Any, Any])[source]#

Bases: BaseIo

Simple dict that serves as IO to configure a step directly from config dict.

Raises:

ValueError – If the key is not found in the dict, there is a type mismatch or validation fails.

__init__(answers: dict[str, object], **kwargs: Dict[Any, Any])[source]#

Foo BAR :param answers: Dict containing all necessary keys for the step you want

to configure

all_answers() dict[str, object][source]#

Returns all previous questions together with the given answers.

Returns:

All previously answered questions.

Return type:

dict[str, object]

Example:

>>> export_answers()
{ "foo" : "bar", "hello": 12, "world": True }
boolean(key: str, message: str) bool[source]#

Presents the user with a message and returns the inputted boolean. Can also validate the user input. For each key/step combination, the first entered value by the user for this question is always returned.

Parameters:
  • key (str) – Key used to identify the user input.

  • message (str) – Message displayed to the user when asked the question.

Returns:

The value entered by the user.

Return type:

bool

Example:

>>> boolean("hello", "world")
True
>>> boolean()
TypeError("Key and message should be of type string.")
Raises:
  • TypeError – If the arguments have the wrong type.

  • ValueError – If the key is empty.

filepath(key: str, message: str, validate: ~typing.Callable[[~pathlib.Path], bool] = <function DictIo.<lambda>>) Path[source]#

Presents the user with a message and returns the inputted filepath. Can also validate the user input. For each key/step combination, the first entered value by the user for this question is always returned.

Parameters:
  • key (str) – Key used to identify the user input.

  • message (str) – Message displayed to the user when asked the question.

  • validate (Callable[[Path], bool] | None) – Validator which validates the user input.

Returns:

The value entered by the user.

Return type:

Path

Example:

>>> filepath("hello", "world")
Path()
>>> filepath("hello", "world", lambda x: True)
Path()
>>> filepath()
TypeError("Key and message should be of type string.")
Raises:
  • TypeError – If the arguments have the wrong type.

  • ValueError – If the key is empty.

filepaths(key: str, message: str, file_validate: ~typing.Callable[[~pathlib.Path], bool] = <function DictIo.<lambda>>, list_validate: ~typing.Callable[[list[~pathlib.Path] | None], bool] = <function DictIo.<lambda>>) list[Path][source]#

Presents the user with a message and returns the inputted filepaths. Can also validate the user input. For each key/step combination, the first entered value by the user for this question is always returned.

Parameters:
  • key (str) – Key used to identify the user input.

  • message (str) – Message displayed to the user when asked the question.

  • file_validate (Callable[[Path], bool] | None) – Validator which validates the inputted files.

  • list_validate (Callable[[list], bool] | None) – Validator which validates the inputted list of files.

Returns:

The value entered by the user.

Return type:

list[Path]

Example:

>>> filepaths("hello", "world")
[Path(), Path()]
>>> filepaths("hello", "world", lambda x: True)
[Path(), Path()]
>>> filepath()
TypeError("Key and message should be of type string.")
Raises:
  • TypeError – If the arguments have the wrong type.

  • ValueError – If the key is empty.

multiple_choice(key: str, message: str, options: list[tuple[str, T]], allow_no_choice: bool = False) list[T] | None[source]#

Presents the user with a message and options and returns the user choice. Can also validate the user input. For each key/step combination, the first entered value by the user for this question is always returned.

Parameters:
  • key (str) – Key used to identify the user input.

  • message (str) – Message displayed to the user when asked the question.

  • options (list[tuple[str, K]]) – Options from which the user can choose.

  • allow_no_choice (bool | None) – Optional argument to allow no selection by the user.

Returns:

The value chosen by the user.

Return type:

K

Example:

>>> multiple_choice("hello", "world", [("choice a", "a"), ("choice b", "b")])
["a"]
>>> multiple_choice("hello", "world", [("choice a", "a"), ("choice b", "b")], allow_no_choice=True)
None
>>> multiple_choice("A", "B")
TypeError("Options need to be of type list.")
Raises:
  • TypeError – If the arguments have the wrong type.

  • ValueError – If the key is empty.

number(key: str, message: str, validate: ~typing.Callable[[int | float], bool] = <function DictIo.<lambda>>) int | float[source]#

Presents the user with a message and returns the inputted number. Can also validate the user input. For each key/step combination, the first entered value by the user for this question is always returned.

Parameters:
  • key (str) – Key used to identify the user input.

  • message (str) – Message displayed to the user when asked the question.

  • validate (Callable[[int | float], bool] | None) – Validator which validates the user input.

Returns:

The value entered by the user.

Return type:

int | float

Example:

>>> number("hello", "world")
123
>>> number("hello", "world", lambda x: True)
123
>>> number()
TypeError("Key and message should be of type string.")
Raises:
  • TypeError – If the arguments have the wrong type.

  • ValueError – If the key is empty.

single_choice(key: str, message: str, options: list[tuple[str, K]]) K[source]#

Presents the user with a message and options and returns the user choice. Can also validate the user input. For each key/step combination, the first entered value by the user for this question is always returned.

Parameters:
  • key (str) – Key used to identify the user input.

  • message (str) – Message displayed to the user when asked the question.

  • options (list[tuple[str, K]]) – Options from which the user can choose.

Returns:

The value chosen by the user.

Return type:

K

Example:

>>> single_choice("hello", "world", [("choice a", "a"), ("choice b", "b")])
"a"
>>> single_choice("A", "B")
TypeError("Options need to be of type list.")
Raises:
  • TypeError – If the arguments have the wrong type.

  • ValueError – If the key is empty.

string(key: str, message: str, validate: ~typing.Callable[[str], bool] = <function DictIo.<lambda>>) str[source]#

Presents the user with a message and returns the inputted string. Can also validate the user input. For each key/step combination, the first entered value by the user for this question is always returned.

Parameters:
  • key (str) – Key used to identify the user input.

  • message (str) – Message displayed to the user when asked the question.

  • validate (Callable[[str], bool] | None) – Validator which validates the user input.

Returns:

The value entered by the user.

Return type:

str

Example:

>>> string("hello", "world")
"User input"
>>> string("hello", "world", lambda x: True)
"User input"
>>> string()
TypeError("Key and message should be of type string.")
Raises:
  • TypeError – If the arguments have the wrong type.

  • ValueError – If the key is empty.