IO Base Class#

Warning

This class is an abstract implementation using ABC. It serves as an interface for other IO classes to build upon. You should never import BaseIo to use as IO for your pipeline steps.

class expectmine.io.base_io.BaseIo(**kwargs: Dict[Any, Any])[source]#

Bases: ABC

abstract __init__(**kwargs: Dict[Any, Any])[source]#

Creates an instance of the scoped io. Each io instance is scoped to an individual step.

abstract 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 }
abstract 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.

abstract filepath(key: str, message: str, validate: ~typing.Callable[[~pathlib.Path], bool] = <function BaseIo.<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.

abstract filepaths(key: str, message: str, file_validate: ~typing.Callable[[~pathlib.Path], bool] = <function BaseIo.<lambda>>, list_validate: ~typing.Callable[[list[~pathlib.Path] | None], bool] = <function BaseIo.<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.

abstract 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.

abstract number(key: str, message: str, validate: ~typing.Callable[[int | float], bool] = <function BaseIo.<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.

abstract 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.

abstract string(key: str, message: str, validate: ~typing.Callable[[str], bool] = <function BaseIo.<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.