nextmv-py icon indicating copy to clipboard operation
nextmv-py copied to clipboard

ENG-5400 Adds configuration

Open sebastian-quintero opened this issue 1 year ago • 0 comments

Description

Adds new Configuration and Parameter classes used for options/configurations. A parameter dictates an option that, as the bare minimum, has a name and a type. A configuration can be instantiated from a list of parameters. Parameters can be populated from command-line arguments, environment variables, or default values. A special help is provided when invoking the -h/--help flag on a script that uses the Configuration.

The Configuration is a namespace that allows someone to access parameters directly with the name, i.e.: config.param_a.

Examples

The following examples use this python script:

import nextmv

config = nextmv.Configuration(
    nextmv.Parameter("duration", str, "30s", description="solver duration", required=True),
    nextmv.Parameter("threads", int, 4, description="computer threads", required=True),
)

print(config.duration, config.threads)
  • Default values are used.
$ python main.py
30s 4
  • The duration parameter is taken from a command-line argument
$ python main.py -duration 40s
40s 4
  • The duration parameter is taken from an environment variable
$ DURATION=50s python main.py
50s 4
  • The duration parameter is taken from a command-line argument (highest precedence_, even though the env var is specified.
$ DURATION=50s python main.py -duration 40s
40s 4
  • This is the help manu.
$ python main.py -h
usage: main.py [options]

Configuration for main.py. Use command-line arguments (highest precedence) or environment variables.

options:
  -h, --help            show this help message and exit
  -duration DURATION, --duration DURATION
                        [env var: DURATION] (required) (default: 30s) (type: str): solver duration
  -threads THREADS, --threads THREADS
                        [env var: THREADS] (required) (default: 4) (type: int): computer threads
  • The bare minimum that a parameter needs is a name and a type. Consider this other python script:
import nextmv

config = nextmv.Configuration(
    nextmv.Parameter("duration", str),
    nextmv.Parameter("threads", int),
)

print(config.duration, config.threads)

The help is a bit different:

$ python main.py -h
usage: main.py [options]

Configuration for main.py. Use command-line arguments (highest precedence) or environment variables.

options:
  -h, --help            show this help message and exit
  -duration DURATION, --duration DURATION
                        [env var: DURATION] (type: str)
  -threads THREADS, --threads THREADS
                        [env var: THREADS] (type: int)

But using it is straight forward:

$ python main.py -duration 30s -threads 3
30s 3

sebastian-quintero avatar Aug 23 '24 05:08 sebastian-quintero