mlrl.util.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.util.options.BooleanOption(*values)¶
Bases:
StrEnumSpecifies all valid textual representations of boolean values.
- FALSE = 'false'¶
- TRUE = 'true'¶
- class mlrl.util.options.Options(dictionary: dict[str, Any] | None = None)¶
Bases:
objectStores 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'¶
- static create(string: str, allowed_keys: set[str]) Options¶
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 | None = None) str | None¶
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.util.options.parse_enum(name: str, value: str | None, enum: type[Enum], default: Enum | None = None) Enum | None¶
Parses and returns an enum value. If the given value is invalid, a ValueError is raised.
- Parameters:
name – The name of the argument or parameter, the value corresponds to
value – The value to be parsed
enum – The enum
default – The default value to be returned if value is None
- Returns:
The value that has been parsed or default, if the given value is None
- mlrl.util.options.parse_param(name: str, value: str, allowed_values: set[str]) str¶
Parses and returns an argument or parameter value. If the given value is invalid, a ValueError is raised.
- Parameters:
name – The name of the argument or parameter, the value corresponds to
value – The value to be parsed
allowed_values – A set that contains all valid values
- Returns:
The value that has been parsed
- mlrl.util.options.parse_param_and_options(name: str, value: str, allowed_values_and_options: dict[str, set[str]]) tuple[str, Options]¶
Parses and returns an argument or parameter value, as well as additional Options that may be associated with it. If the given value is invalid, a ValueError is raised.
- Parameters:
name – The name of the argument or parameter, the value corresponds to
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.