Petty little joy # 5: Dynaconf - managing settings in the project
Some questions in the world of pit-house development have the magical power to raise entire armies of people, direct them to a holy war and force them to throw into each other with whole heaps of arguments, calculations and pieces of code. Sometimes, when the arguments end, the warring armies quickly move to warfare by throwing piles of verbal excrement.
Popular topics include:
- Should I even write this project in Python?
- What framework to take for development?
- Who is faster, library X or library Y?
Today I’ll try to gently pick just one of these magical topics - the question "How to manage configs in a project?".
Obviously, there are plenty of ways to configure a live program.
- Environment variables (together with .env files to the heap).
- All kinds of specialized files in yaml, json, xml, ini formats.
- * .py files with classes or "constants".
- configparser from the Python standard library.
- Self-written solutions based on classes, modules, files, telepathy, black magic (or all together).
- Config managers built into the framework (as, for example, in Flask).
There are enough ways, you can choose anything and use it. But progress would stop if people did not try to improve and optimize everything in a row (including something that has been working for a long time and with acceptable quality). The hands of enthusiasts finally reached the configs of Python applications.
So there was dynaconf
The developers were not particularly worried with the invention of their cunning approaches and gathered all the popular methods of working with settings under the roof of one.
- You can read files (.json, .ini, yaml and the newfangled .toml, which is also worth getting to know)
- You can pick up environment variables and .env files
- You can use external storage and services (from simple Redis to special secure storage with passwords)
- All this successfully merges itself with the configs of the Flask and Django frameworks.
And, of course, on top we added a set of buns that makes life easier in any project
- Easy switching environments dev / staging / testing / production.
- The default settings.
- Turning features on and off in the code (marketers, product managers and testers will be happy).
- A console utility that allows you to change project settings on the fly without touching the code.