Parsing from Environment Variables¶
-
Recomendation¶
The environment variables are normally set using uppercase delimiter-separeted words with "-" or "_" for spaces.
This parser don't care about it and will try to parse to any name you use on the env_name. If you want to set it as CamelCase variable, you can do it. I don't recommend, but you can.
-
Using the Environmental Variables¶
Here is how you can add your configuration file to the package:
from fejao_config_parser import my_config
config = [
{
"name": "example_string",
"type": "string",
"default": "default_string_value",
"env_name": "EXAMPLE_STRING"
},
{
"name": "example_bool",
"type": "bool",
"default": False,
"env_name": "EXAMPLE_BOOL"
},
{
"name": "example_int",
"type": "int",
"default": 42,
"env_name": "EXAMPLE_INT"
},
{
"name": "example_float",
"type": "float",
"default": 4.2,
"env_name": "EXAMPLE_FLOAT"
},
{
"name": "example_list",
"type": "list",
"default": [1, 2, 3],
"env_name": "EXAMPLE_LIST"
},
{
"name": "example_dict",
"type": "dict",
"default": {
"key_1": "dict_deafult_1",
"key_2": "dict_deafult_2",
},
"env_name": "EXAMPLE_DICT"
},
]
config = my_config(config=config)
Here is an example of starting your project from the terminal using the environmental variables:
EXAMPLE_STRING="from_env_value" \
EXAMPLE_BOOL=true \
EXAMPLE_INT=24 \
EXAMPLE_FLOAT=2.4 \
EXAMPLE_LIST=[4, 5, 6] \
EXAMPLE_DICT='{"key_1":"env_val_1", "key_2":"env_val_2"}' \
python your_project_file.py
You can access the values as:
print(f"example_string: {config.example_string}")
print(f"example_bool: {config.example_bool}")
print(f"example_int: {config.example_int}")
print(f"example_float: {config.example_float}")
print(f"example_list: {config.example_list}")
print(f"example_dict: {config.example_dict}")
The ouput will be:
example_string: "from_env_value"
example_bool: True
example_int: 24
example_float: 2.4
example_list: [4, 5, 6]
example_dict: {"key_1": "env_val_1", "key_2": "env_val_2"}