mlrl.common.options module

Author: Michael Rapp (michael.rapp.ml@gmail.com)

Provides a data structure that allows to store and parse options that are provided as key-value pairs.

class mlrl.common.options.BooleanOption(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)

Bases: Enum

Specifies all valid textual representations of boolean values.

FALSE = 'false'
TRUE = 'true'
static parse(text: str) bool

Parses a given text that represents a boolean value. If the given text does not represent a valid boolean value, a ValueError is raised.

Parameters:

text – The text to be parsed

Returns:

True or false, depending on the given text

class mlrl.common.options.Options

Bases: object

Stores key-value pairs in a dictionary and provides methods to access and validate them.

ERROR_MESSAGE_INVALID_OPTION = 'Expected comma-separated list of key-value pairs'
ERROR_MESSAGE_INVALID_SYNTAX = 'Invalid syntax used to specify additional options'
classmethod create(string: str, allowed_keys: Set[str])

Parses the options that are provided via a given string that is formatted according to the following syntax: “[key1=value1,key2=value2]”. If the given string is malformed, a ValueError will be raised.

Parameters:
  • string – The string to be parsed

  • allowed_keys – A set that contains all valid keys

Returns:

An object of type Options that stores the key-value pairs that have been parsed from the given string

get_bool(key: str, default_value: bool) bool

Returns a boolean that corresponds to a specific key.

Parameters:
  • key – The key

  • default_value – The default value to be returned, if no value is associated with the given key

Returns:

The value that is associated with the given key or the given default value

get_float(key: str, default_value: float) float

Returns a float that corresponds to a specific key.

Parameters:
  • key – The key

  • default_value – The default value to be returned, if no value is associated with the given key

Returns:

THe value that is associated with the given key or the given default value

get_int(key: str, default_value: int) int

Returns an integer that corresponds to a specific key.

Parameters:
  • key – The key

  • default_value – The default value to be returned, if no value is associated with the given key

Returns:

The value that is associated with the given key or the given default value

get_string(key: str, default_value: str) str

Returns a string that corresponds to a specific key.

Parameters:
  • key – The key

  • default_value – The default value to be returned, if no value is associated with the given key

Returns:

The value that is associated with the given key or the given default value

mlrl.common.options.parse_param(parameter_name: str, value: str, allowed_values: Set[str]) str

Parses and returns a parameter value. If the given value is invalid, a ValueError is raised.

Parameters:
  • parameter_name – The name of the parameter

  • value – The value to be parsed

Allowed_values:

A set that contains all valid values

Returns:

The value that has been parsed

mlrl.common.options.parse_param_and_options(parameter_name: str, value: str, allowed_values_and_options: Dict[str, Set[str]]) Tuple[str, Options]

Parses and returns a parameter value, as well as additional Options that may be associated with it. If the given value is invalid, a ValueError is raised.

Parameters:
  • parameter_name – The name of the parameter

  • value – The value to be parsed

  • allowed_values_and_options – A dictionary that contains all valid values, as well as their options

Returns:

A tuple that contains the value that has been parsed, as well as additional Options.