jztools.validation#

Functions

check_expected_kwargs(expected, received[, ...])

check_option(name, value, options[, ignore_list])

Checks that an option has a valid value, raising InvalidOptionValue otherwise.

check_unique(values[, msg])

checked_filter_single(predicate, container)

Similar to filter, but ensures that a single item in the input container satisfies the predicate and returns that item.

checked_get_single(container, *posns[, msg, ...])

Checks if a (nested) container contains a single entry with the specified index (or indices) and returns it.

checked_get_unique(container)

Ensures that the container contains a unique element, possibly repeated multiple times, and returns that element.

choices(name, values[, multi, doc])

Will only check the value if it is provided explicitly by the user - default values are not checked.

confirm_option(name, received, dangerous_values)

If the received value is in the list of dangerous values, user console input will be requested.

not_none(x)

Classes

NoItem()

Exceptions

InvalidOptionValue

NonSingle

NonUnique

OptionNotConfirmed

ParameterChoiceError(name, err_value, values)

exception jztools.validation.InvalidOptionValue#

Bases: Exception

exception jztools.validation.OptionNotConfirmed#

Bases: Exception

exception jztools.validation.NonSingle#

Bases: Exception

exception jztools.validation.NonUnique#

Bases: Exception

jztools.validation.checked_get_single(container, *posns, msg: str | Callable[[], str] = 'Expected single entry but found {count} for keys {posns}.', raise_empty=True) Any#

Checks if a (nested) container contains a single entry with the specified index (or indices) and returns it.

# Success checked_get_single((x for x in [0])) -> 0 checked_get_single([‘first’]) -> ‘first’ checked_get_single([‘f’], 0, 0) -> ‘f’ checked_get_single({‘sentence’:{‘words’:[‘My’]}}, ‘sentence’, ‘words’, 0) -> ‘My’

# Error checked_get_single((x for x in [0, 1])) checked_get_single([‘first’, ‘second’]) checked_get_single([‘fa’], 0, 0) checked_get_single({‘sentence’:{‘words’:[‘My’], ‘phrases’:[None]}}, ‘sentence’, ‘words’, 0) -> ‘My’

jztools.validation.checked_get_unique(container)#

Ensures that the container contains a unique element, possibly repeated multiple times, and returns that element.

jztools.validation.checked_filter_single(predicate, container, msg='Expected a single matching entry but found {count}.')#

Similar to filter, but ensures that a single item in the input container satisfies the predicate and returns that item. If a none or more than one found, raises an exception.

jztools.validation.check_option(name, value, options, ignore_list=[])#

Checks that an option has a valid value, raising InvalidOptionValue otherwise.

Parameters:
  • name – The name of the option to check.

  • value – The value of the option.

  • options – List of valid option values.

  • ignore_list – Do not raise an error if value is in this list.

Example

def winner(medal):
    check_option('medal', medal, [1, 2, 3], ignore_list=[-1])
    ...
jztools.validation.confirm_option(name, received, dangerous_values, message='\nPlease confirm choice {name}={received} (y/n): ')#

If the received value is in the list of dangerous values, user console input will be requested. The received value is returned if confirmed by the user, and an OptionNotConfirmed error raised otherwise.

exception jztools.validation.ParameterChoiceError(name, err_value, values)#

Bases: ValueError

jztools.validation.choices(name, values, multi=False, doc=True)#

Will only check the value if it is provided explicitly by the user - default values are not checked.

Parameters:
  • name – The parameter name.

  • values – The valid choices as an iterable.

  • multi – If True, lists, tuples or sets of the allowed values also allowed.

  • doc – If True, the choices are appended afer the :param <param name>: string (if any) in the doc string.