usage
Usage¶
To use Python 2NMS config manager in a project
1 2 3 4 5 6 7 | |
Config.create() object¶
A configuration object is created by calling the Config.create() method. This actually a wrapper around the OmegaConf.create()method.
The method takes the following parameters:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
Defining parameters¶
Parameters can be defined in different ways and in a predefined order. Multiple ways can be used at the same time, for example a yaml config file exists together with environment variables and cli arguments.
In the end a single OmegaConf object will be created combining all parameters.
Import order:
- programmatically defined default values
- environment variables
- dotenv files
- configuration files in YAML format
- cli arguments
programatically defined parameters¶
It's easy to define defaults for your application by defining a dictionary or list or yaml string. Check the OmegaConf documentation for more details.
1 2 3 4 5 6 7 8 9 10 | |
configuration files defined in yaml format¶
It is possible to store all your application parameters in yaml files. You can specifically refer to a file or you can use the default filenames and then the environment parameter will define which config file will be loaded (if it exists).
If no filename is specified and no environment parameter is defined then the filename ./conf/producation.yaml will be loaded automatically if it exists.
Default config folder = ./conf
Default config filenames and matching environment variable:
| environment | config file |
|---|---|
| PROD | production.yaml |
| DEV | development.yaml |
| TEST | test.yaml |
| DEBUG | debug.yaml |
Example:¶
1 2 3 4 5 | |
Usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
environment variables¶
Environment variables can be defined via system environment variables or via .env files which will be loaded automatically if it exists.
To prevent all existing environment variables to be loaded into your configuration object, you'll have to specify which variables are allowed by specifying the allowed_envvarsparameter in the create() method.
Example:¶
1 2 3 4 5 6 7 8 9 10 | |
dotenv variables¶
Similar as with system environment variables you can also define envvars in a .env file, locally to the main script. The difference with system environment variables is that there is no need to define the allowed parameters, all parameters defined in the .env file will be loaded.
Example:¶
.env file
1 2 | |
1 2 3 4 5 | |
cli arguments¶
The last method to load parameters is to use cli script arguments. All arguments will be loaded.
Example:¶
myscript.py
1 2 3 4 5 | |
1 2 3 | |