Using this python package¶
-
Install¶
For installing the package, please run the command:
bash
pip install fejao_config_parser
-
Importing it to your code¶
For importing the package after that you installed, add this line to your code:
python
from fejao_config_parser import my_config
-
Precedence¶
The values parsed have a precedence for been set. This precendence is:- 1 - Terminal arguments (highest precedence, if set and not the default)
- 2 - Environment variables (if set)
- 3 - Configuration file values (if a config file is provided and values are set)
- 4 - Default values (lowest precedence)
-
Simple Example¶
Here is an example of using the package:
```python from fejao_config_parser import my_config
config = [ { "name": "var_1", "type": bool, "default": False }, { "name": "var_2", "type": int, "default": 42 }, ] config = my_config( config=config, debug=True ) ```
And for accessing the added variables is easy as:
python
print(f"var_2: {config.var_2}") # This should print: 42
For further functionalities, please read the main.py source code.
-
Parameters¶
The possible parameters to parse are:
-
config: list[{dict_1}, {dict_2}, ...]¶
This is the list of dictioneries that should contain the information from the variables to be added.
The dictionary keys to besed:
-
name: str (REQUIRED)¶
The name of the variable that you want to add.
-
type: (str | bool | int | float | list | dict) (REQUIRED)¶
The type of the variable that you want to add. The accepted types that can be parsed are:
-
default: (str | bool | int | float | list | dict) (REQUIRED)¶
The default value of the variable to be used if no other method was used. It will raise error if you parse this value as not the same type as you set at the type parameter
-
env_name: str¶
The environmental variable name that you want to be read. Please read the Environment Variables Example for more infos.
-
terminal_help: str¶
This is the help string to be displayed at the terminal when using the --help after calling your project. Please read the Terminal Arguments Example for more infos.
-
terminal_name: str¶
The string to be read from the terminal for setting your variable. For example, for setting the var_1 variable, you could set this also to var_1 and it will be searched as --var_1 parsed from therminal. Please read the Terminal Arguments Example for more infos.
-
terminal_name_short: {char | str}¶
The character or short string to be read from the terminal input for setting your variable. For example, for setting the var_1 variable, you could set this to v1 and it will be searched as -v1 parsed from terminal. Please read the Terminal Arguments Example for more infos.
-
-
config_file_path: str¶
If you are developing or debugging, and parsing the variables all the time is tiresome. That's why you can parse a configuration file path to be read for parsing the values of your variables.
The accepted file types to be parsed are:
- yaml
- json
- toml
- ini Over the .ini files, you should set your variables over the default section
Please read the Configuration File Example for more infos.
-
description: str¶
This is the description string that should be printed at the terminal when using your program.
If you set this parameters as: description=This is awesome description that you added.
And call your project with:
bash python your_program_name.py --helpIt should output like this: ```bash usage: your_program_name.py [-h]
This is awesome description that you added.
options: -h, --help show this help message and exit ```
Please read the Terminal Arguments for more infos.
-
debug: bool¶
When this is set to true, it will print at the terminal all the values from the variables that was parsed.