Options Module¶
This section documents the options components of the Nextmv Python SDK.
options
¶
Configuration management for application runs.
This module provides classes for handling configuration options for
applications. It supports reading options from command-line arguments,
environment variables, and default values in a prioritized manner. The module
includes classes for defining individual options (Option
) and managing
collections of options (Options
).
CLASS | DESCRIPTION |
---|---|
Option |
Class for defining individual options for configuration. |
Options |
Class for managing collections of options. |
Option
dataclass
¶
Option(
name: str,
option_type: type,
default: Optional[Any] = None,
description: Optional[str] = None,
required: bool = False,
choices: Optional[list[Any]] = None,
additional_attributes: Optional[dict[str, Any]] = None,
)
An option that is used in Options
.
You can import the Option
class directly from nextmv
:
Options provide a way to configure application behavior. When an Option
is required, it is a good practice to provide a default value for it. This
is because the Options
will raise an error if a required Option
is not
provided through a command-line argument, an environment variable or a
default value.
PARAMETER | DESCRIPTION |
---|---|
|
TYPE:
|
|
The type of the option.
TYPE:
|
|
The default value of the option. Even though this is optional, it is recommended to provide a default value for all options.
TYPE:
|
|
An optional description of the option. This is useful for generating
help messages for the
TYPE:
|
|
Whether the option is required. If an option is required, it will be an error to not provide a value for it, either through a command-line argument, an environment variable or a default value.
TYPE:
|
|
Limits values to a specific set of choices.
TYPE:
|
|
Optional additional attributes for the option. The Nextmv Cloud may
perform validation on these attributes. For example, the maximum length
of a string or the maximum value of an integer. These additional
attributes will be shown in the help message of the
TYPE:
|
Examples:
from nextmv.options import Option
opt = Option("duration", str, "30s", description="solver duration", required=False)
opt.name
opt.default
additional_attributes
class-attribute
instance-attribute
¶
Optional additional attributes for the option. The Nextmv Cloud may
perform validation on these attributes. For example, the maximum length of
a string or the maximum value of an integer. These additional attributes
will be shown in the help message of the Options
.
choices
class-attribute
instance-attribute
¶
Limits values to a specific set of choices.
default
class-attribute
instance-attribute
¶
The default value of the option. Even though this is optional, it is recommended to provide a default value for all options.
description
class-attribute
instance-attribute
¶
An optional description of the option. This is useful for generating help
messages for the Options
.
from_dict
classmethod
¶
Creates an instance of Option
from a dictionary.
PARAMETER | DESCRIPTION |
---|---|
|
The dictionary representation of an option.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Option
|
An instance of |
Examples:
>>> opt_dict = {"name": "timeout", "option_type": "<class 'int'>", "default": 60}
>>> option = Option.from_dict(opt_dict)
>>> option.name
'timeout'
>>> option.default
60
Source code in nextmv/nextmv/options.py
required
class-attribute
instance-attribute
¶
Whether the option is required. If a option is required, it will be an error to not provide a value for it, either trough a command-line argument, an environment variable or a default value.
to_dict
¶
Converts the option to a dict.
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
The option as a dict with all its attributes. |
Examples:
>>> opt = Option("duration", str, "30s", description="solver duration")
>>> opt_dict = opt.to_dict()
>>> opt_dict["name"]
'duration'
>>> opt_dict["default"]
'30s'
Source code in nextmv/nextmv/options.py
Options
¶
Options container for application configuration.
You can import the Options
class directly from nextmv
:
To initialize options, pass in one or more Option
objects. The options
will look for the values of the given parameters in the following order:
command-line arguments, environment variables, default values.
Once the Options
are initialized, you can access the underlying options as
attributes of the Options
object. For example, if you have an
Option
object with the name "duration", you can access it as
options.duration
.
If an option is required and not provided through a command-line argument, an environment variable or a default value, an error will be raised.
Options works as a Namespace, so you can assign new attributes to it. For
example, you can do options.foo = "bar"
.
Options are parsed from the given sources when an attribute is accessed.
Alternatively, you can call the parse
method to parse the options
manually. Options that are not parsed may be merged with other unparsed
options, by using the merge
method. Once options are parsed, they cannot
be merged with other options. After options are parsed, you may get the
help message by running the script with the -h/--help
flag.
PARAMETER | DESCRIPTION |
---|---|
|
The list of
TYPE:
|
Examples:
>>> import nextmv
>>>
>>> options = nextmv.Options(
... nextmv.Option("duration", str, "30s", description="solver duration", required=False),
... nextmv.Option("threads", int, 4, description="computer threads", required=False),
... )
>>>
>>> print(options.duration, options.threads, options.to_dict())
30s 4 {"duration": "30s", "threads": 4}
RAISES | DESCRIPTION |
---|---|
ValueError
|
If a required option is not provided through a command-line argument, an environment variable or a default value. |
TypeError
|
If an option is not either an |
ValueError
|
If an environment variable is not of the type of the corresponding parameter. |
Initialize an Options instance with the provided option objects.
PARAMETER | DESCRIPTION |
---|---|
|
The option objects to include in this Options instance.
TYPE:
|
Source code in nextmv/nextmv/options.py
from_dict
classmethod
¶
Creates an instance of Options
from a dictionary.
The dictionary should have the following structure:
PARAMETER | DESCRIPTION |
---|---|
|
The dictionary representation of the options.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Options
|
An instance of |
Examples:
>>> data = {"duration": "30s", "threads": 4}
>>> options = Options.from_dict(data)
>>> options.duration
'30s'
>>> options.threads
4
Source code in nextmv/nextmv/options.py
from_options_dict
classmethod
¶
from_options_dict(
options_dict: list[dict[str, Any]],
) -> Options
Creates an instance of Options
from a list of Option
objects in
dict form. Each entry is the dict representation of an Option
.
PARAMETER | DESCRIPTION |
---|---|
|
The list of dictionaries (
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Options
|
An instance of |
Examples:
>>> options_dict = [
... {"name": "duration", "option_type": "<class 'str'>", "default": "30s"},
... {"name": "threads", "option_type": "<class 'int'>", "default": 4}
... ]
>>> options = Options.from_options_dict(options_dict)
>>> options.duration
'30s'
>>> options.threads
4
Source code in nextmv/nextmv/options.py
from_parameters_dict
classmethod
¶
from_parameters_dict(
parameters_dict: list[dict[str, Any]],
) -> Options
Warning
Parameter
is deprecated, use Option
instead.
Options.from_parameters_dict
-> Options.from_options_dict
Creates an instance of Options
from parameters in dict form. Each
entry is the dict representation of a Parameter
.
PARAMETER | DESCRIPTION |
---|---|
|
The list of dictionaries (parameter entries).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Options
|
An instance of |
Source code in nextmv/nextmv/options.py
merge
¶
merge(*new: Options, skip_parse: bool = False) -> Options
Merges the current options with the new options.
This method cannot be used if any of the options have been parsed. When options are parsed, values are read from the command-line arguments, environment variables and default values. Merging options after parsing would result in unpredictable behavior.
PARAMETER | DESCRIPTION |
---|---|
|
The new options to merge with the current options. At least one new option set
is required to merge. Multiple
TYPE:
|
|
If True, the merged options will not be parsed after merging. This is useful if you want to merge further options after this merge. The default is False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Options
|
The merged options object (self). |
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If the current options have already been parsed. |
RuntimeError
|
If the new options have already been parsed. |
Examples:
>>> opt1 = Options(Option("duration", str, "30s"))
>>> opt2 = Options(Option("threads", int, 4))
>>> opt3 = Options(Option("verbose", bool, False))
>>> merged = opt1.merge(opt2, opt3)
>>> merged.duration
'30s'
>>> merged.threads
4
>>> merged.verbose
False
Source code in nextmv/nextmv/options.py
options_dict
¶
Converts the Options
to a list of dicts. Each dict is the dict
representation of an Option
.
RETURNS | DESCRIPTION |
---|---|
list[dict[str, Any]]
|
The list of dictionaries ( |
Examples:
>>> options = Options(Option("duration", str, "30s"), Option("threads", int, 4))
>>> opt_dicts = options.options_dict()
>>> opt_dicts[0]["name"]
'duration'
>>> opt_dicts[1]["name"]
'threads'
Source code in nextmv/nextmv/options.py
parameters_dict
¶
Warning
Parameter
is deprecated, use Option
instead. Options.parameters_dict
-> Options.options_dict
Converts the options to a list of dicts. Each dict is the dict
representation of a Parameter
.
RETURNS | DESCRIPTION |
---|---|
list[dict[str, Any]]
|
The list of dictionaries (parameter entries). |
Source code in nextmv/nextmv/options.py
parse
¶
Parses the options using command-line arguments, environment variables
and default values, in that order. Under the hood, the argparse
library is used. When command-line arguments are parsed, the help menu
is created, thus parsing Options more than once may result in
unexpected behavior.
This method is called automatically when an attribute is accessed. If you want to parse the options manually, you can call this method.
After Options have been parsed, they cannot be merged with other Options. If you need to merge Options, do so before parsing them.
Examples:
>>> import nextmv
>>>
>>> options = nextmv.Options(
... nextmv.Option("duration", str, "30s", description="solver duration", required=False),
... nextmv.Option("threads", int, 4, description="computer threads", required=False),
... )
>>> options.parse() # Does not raise an exception.
>>> import nextmv
>>>
>>> options = nextmv.Options(
... nextmv.Option("duration", str, "30s", description="solver duration", required=False),
... nextmv.Option("threads", int, 4, description="computer threads", required=False),
... )
>>> print(options.duration) # Parses the options.
>>> options.parse() # Raises an exception because the options have already been parsed.
RAISES | DESCRIPTION |
---|---|
RuntimeError
|
If the options have already been parsed. |
ValueError
|
If a required option is not provided through a command-line argument, an environment variable or a default value. |
TypeError
|
If an option is not an |
ValueError
|
If an environment variable is not of the type of the corresponding parameter. |
Source code in nextmv/nextmv/options.py
to_dict
¶
Converts the options to a dict. As a side effect, this method parses
the options if they have not been parsed yet. See the parse
method
for more information.
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
The options as a dict where keys are option names and values are the corresponding option values. |
Examples:
>>> options = Options(Option("duration", str, "30s"), Option("threads", int, 4))
>>> options_dict = options.to_dict()
>>> options_dict["duration"]
'30s'
>>> options_dict["threads"]
4
Source code in nextmv/nextmv/options.py
to_dict_cloud
¶
Converts the options to a dict that can be used in the Nextmv Cloud.
Cloud has a hard requirement that options are passed as strings. This method converts the options to a dict with string values. This is useful for passing options to the Nextmv Cloud.
As a side effect, this method parses the options if they have not been
parsed yet. See the parse
method for more information.
RETURNS | DESCRIPTION |
---|---|
dict[str, str]
|
The options as a dict with string values where non-string values are JSON-encoded. |
Examples:
>>> options = Options(Option("duration", str, "30s"), Option("threads", int, 4))
>>> cloud_dict = options.to_dict_cloud()
>>> cloud_dict["duration"]
'30s'
>>> cloud_dict["threads"]
'4'
Source code in nextmv/nextmv/options.py
Parameter
dataclass
¶
Parameter(
name: str,
param_type: type,
default: Optional[Any] = None,
description: Optional[str] = None,
required: bool = False,
choices: list[Optional[Any]] = None,
)
Warning
Parameter
is deprecated, use Option
instead.
Parameter that is used in a Configuration
. When a parameter is required,
it is a good practice to provide a default value for it. This is because
the configuration will raise an error if a required parameter is not
provided through a command-line argument, an environment variable or a
default value.
PARAMETER | DESCRIPTION |
---|---|
|
The name of the parameter.
TYPE:
|
|
The type of the parameter.
TYPE:
|
|
The default value of the parameter. Even though this is optional, it is recommended to provide a default value for all parameters.
TYPE:
|
|
An optional description of the parameter. This is useful for generating help messages for the configuration.
TYPE:
|
|
Whether the parameter is required. If a parameter is required, it will be an error to not provide a value for it, either through a command-line argument, an environment variable or a default value.
TYPE:
|
|
Limits values to a specific set of choices.
TYPE:
|
Examples:
>>> from nextmv.options import Parameter
>>> parameter = Parameter("timeout", int, 60, "The maximum timeout in seconds", required=True)
choices
class-attribute
instance-attribute
¶
Limits values to a specific set of choices.
default
class-attribute
instance-attribute
¶
The default value of the parameter. Even though this is optional, it is recommended to provide a default value for all parameters.
description
class-attribute
instance-attribute
¶
An optional description of the parameter. This is useful for generating help messages for the configuration.
from_dict
classmethod
¶
Warning
Parameter
is deprecated, use Option
instead.
Parameter.from_dict
-> Option.from_dict
Creates an instance of Parameter
from a dictionary.
PARAMETER | DESCRIPTION |
---|---|
|
The dictionary representation of a parameter.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Parameter
|
An instance of |
Source code in nextmv/nextmv/options.py
required
class-attribute
instance-attribute
¶
Whether the parameter is required. If a parameter is required, it will be an error to not provide a value for it, either trough a command-line argument, an environment variable or a default value.
to_dict
¶
Warning
Parameter
is deprecated, use Option
instead.
Parameter.to_dict
-> Option.to_dict
Converts the parameter to a dict.
RETURNS | DESCRIPTION |
---|---|
dict[str, Any]
|
The parameter as a dict with its name, type, default value, description, required flag, and choices. |
Examples:
>>> param = Parameter("timeout", int, 60, "Maximum time in seconds", True)
>>> param_dict = param.to_dict()
>>> param_dict["name"]
'timeout'
>>> param_dict["default"]
60