modelrunner.model.parameters module

Infrastructure for managing classes with parameters.

One aim is to allow easy management of inheritance of parameters.

Parameter

class representing a single parameter

DeprecatedParameter

a parameter that can still be used normally but is deprecated

HideParameter

a helper class that allows hiding parameters of the parent classes

Parameterized

a mixin that manages the parameters of a class

get_all_parameters

get a dictionary with all parameters of all registered classes

class DeprecatedParameter(name, default_value=None, cls=<class 'object'>, description='', choices=None, required=False, hidden=False, extra=<factory>)[source]

Bases: Parameter

a parameter that can still be used normally but is deprecated

Parameters:
  • name (str) –

  • default_value (Any) –

  • cls (type | Callable) –

  • description (str) –

  • choices (Container | None) –

  • required (bool) –

  • hidden (bool) –

  • extra (dict[str, Any]) –

extra: dict[str, Any]
name: str
class HideParameter(name)[source]

Bases: object

a helper class that allows hiding parameters of the parent classes

This parameter will still appear in the parameters dictionary, but it will typically not be visible to the user, e.g., when calling show_parameters().

Parameters:

name (str) – The name of the parameter

class NoValueType[source]

Bases: object

special value to indicate no value for a parameter

class Parameter(name, default_value=None, cls=<class 'object'>, description='', choices=None, required=False, hidden=False, extra=<factory>)[source]

Bases: object

class representing a single parameter

Parameters:
  • name (str) – The name of the parameter

  • default_value (Any) – The default value of the parameter

  • cls (type | Callable) – The type of the parameter, which is used for conversion. The conversion and parsing of values can be disabled by using the default class object.

  • description (str) – A string describing the impact of this parameter. This description appears in the parameter help.

  • choices (container) – A list or set of values that the parameter can take. Values (including the default value) that are not in this list will be rejected. Note that values are check after they have been converted by cls, so specifying cls is particularly important to convert command line parameters from strings.

  • required (bool) – Whether the parameter is required

  • hidden (bool) – Whether the parameter is hidden in the description summary

  • extra (dict) – Extra arguments that are stored with the parameter

choices: Container | None = None
cls

alias of object

convert(value=NoValue, *, strict=True)[source]

converts a value into the correct type for this parameter. If value is not given, the default value is converted.

Note that this does not make a copy of the values, which could lead to unexpected effects where the default value is changed by an instance.

Parameters:
  • value – The value to convert

  • strict (bool) – Flag indicating whether conversion to the type indicated by cls is enforced. If False, the original value is returned when conversion fails.

Returns:

The converted value, which is of type self.cls

default_value: Any = None
description: str = ''
extra: dict[str, Any]
hidden: bool = False
name: str
required: bool = False
property short_description: str

return only the first sentence of the description

class Parameterized(parameters=None, *, strict=True)[source]

Bases: object

a mixin that manages the parameters of a class

initialize the parameters of the object

Parameters:
  • parameters (dict) – A dictionary of parameters to change the defaults. The allowed parameters can be obtained from get_parameters() or displayed by calling show_parameters().

  • strict (bool) – Flag indicating whether parameters are strictly interpreted. If True, only parameters listed in parameters_default can be set and their type will be enforced.

classmethod get_parameter_default(name)[source]

return the default value for the parameter with name

Parameters:

name (str) – The parameter name

classmethod get_parameters(include_hidden=False, include_deprecated=False, sort=True)[source]

return a dictionary of parameters that the class supports

Parameters:
  • include_hidden (bool) – Include hidden parameters

  • include_deprecated (bool) – Include deprecated parameters

  • sort (bool) – Return ordered dictionary with sorted keys

Returns:

a dictionary mapping names to instances of Parameter

Return type:

dict

parameters_default: ParameterListType = []

parameters (with default values) of this subclass

Type:

list

show_parameters(description=False, sort=False, show_hidden=False, show_deprecated=False)[source]

show all parameters in human readable format

Parameters:
  • description (bool) – Flag determining whether the parameter description is shown.

  • sort (bool) – Flag determining whether the parameters are sorted

  • show_hidden (bool) – Flag determining whether hidden parameters are shown

  • show_deprecated (bool) – Flag determining whether deprecated parameters are shown

Return type:

None

All flags default to False.

auto_type(value)[source]

convert value to float or int if reasonable

get_all_parameters(data='name')[source]

get a dictionary with all parameters of all registered classes

Parameters:

data (str) – Determines what data is returned. Possible values are ‘name’, ‘value’, or ‘description’, to return the respective information about the parameters.

Return type:

dict[str, Any]