4 ms·
At my current job I wrote a simple Python class to abstract settings being read from either command line args, environment variables or a TOML-formatted config
by reassembled 4y ago
At my current job I wrote a simple Python class to abstract settings being read from either command line args, environment variables or a TOML-formatted config file. The class first reads settings from the config file if it is available, then environment variables if found, and finally from the command line args if set by the user. The last context to provide a setting value is treated as the actual setting to propagate to the app. If desired the app can then save its current settings state back to a file, either the original config or to a new config file.
I arrived at this solution after getting tired of both having to track down where the value for a particular setting originated at runtime, as well as having multiple implementations of the same kinds of argparse/os.environ/file-based settings systems across our internal tools and scripts.
- smitty1e 4y agoI just put configuration settings in config.py and import them. Problem solved. Plus I have the full programming language available, not some textual subset with Yet Another Dependency to read it.