Member-only story
Write a Better config.py
Your app deserves it. The world deserves it.
Congratulations! You’ve decided not to hard-code values like MAX_SIZE
at the top of seven different modules. You’re going to make a config.py
module to handle such things. But what will it look like? I want to tell you about some alternatives to simply dropping all those uppercase constants into a .py file. Here are the golden nuggets I plan to drop in this article:
- Move your information into a structured text file (json/yaml/etc.)
- Use properties in your config class to control mutability
- Implement runtime configurables as environmental variables
At the bottom of the post, I’ll provide link to a “Hello World” example on GitHub if you’d like to see a complete (if basic) example.
Move your information into a structured text file
It’s often not a great idea to declare all your configuration information in Python in the config.py module. But let’s walkthrough why that is, and what I’m suggesting instead.
The nature of a lot of configuration values is that they allow your application to be configurable. This implies that that the configuration could be set or changed outside of the runtime to allow the application to exhibit certain behaviors when it…