Parsing from Configuration File¶
-
YAML File Example¶
Example of content of your /path/to/config.yml
example_string: "from_config_file_value"
example_bool: True
example_int: 24
example_float: 2.4
example_list: [4, 5, 6]
example_dict:
key_1: config_val_1
key_2: config_val_2
-
JSON File Example¶
Example of content of your /path/to/config.toml
{
"example_string": "from_config_file_value",
"example_bool": true,
"example_int": 24,
"example_float": 2.4,
"example_list": [4, 5, 6],
"example_dict": {
"key_1": "config_val_1",
"key_2": "config_val_2"
}
}
-
TOML File Example¶
Example of content of your /path/to/config.json
example_string = "from_config_file_value"
example_bool = true
example_int = 24
example_float = 2.4
example_list = [ 4, 5, 6 ]
[example_dict]
key_1 = "config_val_1"
key_2 = "config_val_2"
-
INI File Example¶
Example of content of your /path/to/config.ini
[default]
example_string = "from_config_file_value"
example_bool = True
example_int = 24
example_float = 2.4
example_list = 4, 5, 6 ; This also works[4, 5, 6]
example_dict = {
"key_1": "config_val_1",
"key_2": "config_val_2"
}
-
Using the configuration file¶
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"
},
{
"name": "example_bool",
"type": "bool",
"default": False
},
{
"name": "example_int",
"type": "int",
"default": 42
},
{
"name": "example_float",
"type": "float",
"default": 4.2
},
{
"name": "example_list",
"type": "list",
"default": [1, 2, 3]
},
{
"name": "example_dict",
"type": "dict",
"default": {
"key_1": "dict_default_1",
"key_2": "dict_default_2"
}
},
]
config = my_config(
config=config,
config_file_path="/path/to/config.yml" # "/path/to/config.json" | "/path/to/config.toml" | "/path/to/config.ini"
)
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_config_file_value"
example_bool: True
example_int: 24
example_float: 2.4
example_list: [4, 5, 6]
example_dict: {"key_1": "dict_default_1", "key_2": "dict_default_2"}