Config Finder
Finds a configuration file (e.g. pyproject.toml) and returns some sub-configuration with only python standard libraries.
Algorighm
- starts from the currently executed file (__main__.__file__)
- Checks if that folder contains the desired configuration file
- Goes to the parent directory and repeats at 2
Use Case
When defining machine learning projects and handling the project configuration by e.g. a pyproject.toml (e.g. with Astral UV or Poetry) you can utilize the configuration files to define and store important variables.
[tool.some_tool]
key1 = "some_value_1"
key2 = "some_value_2"
[tool.some_tool.default_config]
important_key = "some_value"
[tool.some_tool.special_config]
important_key = "another_value"
Tip
Instead of defining global variables in your code or using dotenv, a configuration file such as the pyproject.toml can be used to store configurations.
Access works via
find_configuration("pyproject.toml", ["tool", "some_tool", "default_config"])
{"important_key" : "some_value"}
This function can also be used to handle credentials.
Caution
Do not write your credentials into the pyproject.toml and ensure that you do not check your credentials into the source control.
Other Readers
Other readers can be used e.g. for YAML via PyYAML/PyPI: PyYAML works like this:
import yaml
from simpleconfigfinder import config_finder
config_finder(
"some_file.yaml", additional_readers={"yaml": yaml.safe_load}
)