In this answer, I was told that the logger needed to only be declared once globally.
On similar lines, how are program configuration settings loaded and used? Whether I use Dynaconf or simply use yaml.safe_load
, what is the best practice for storing and loading configuration data?
Example folder structure:
main.py | |--diskOperations | __init__.py | fileAndFolderOps.py | |--timer __init__.py timers.py stopwatch.py
Functionality needed:
Some program settings need to be loaded in
main.py
. The classes infileAndFolderOps.py
of thediskOperations
package need to loadsome settings. The classes intimers.py
andstopwatch.py
of thetimer
package need to load some settings.The settings need to be segregated into settings that are loadedduring
development
,testing
,production
etc.
Questions:
What are the best practices for creating and storing the configfiles? Are separate config files to be created and stored in eachpackage's folder, and do each of the
.py
files load theconfiguration settings so that it becomes accessible to all theclasses in the.py
file?If there is any setting to be used globally across the entireprogram, is it best to load it into
main.py
and then put it intosomeConfigurationHandler
class and pass the class instance to allinstances of classes of every package file?How best to switch between
development
,test
andproduction
modes? At least inDynaconf
, there's an environment parameter.What about other config libraries? I'm asking about the bestpractice when needing to use settings across multiple packages.To ensure that the variables in the classes match the configurationsin the config files, is it recommended to have a "generator"function in each class, that auto-generates configuration files withdefault values (if the config file does not exist)?
Perhaps it'd also help to point us to any open source project that uses config files and settings in an ideal way (it helps to know about the 12 factor app guide).