Magic Config library for Python
A simple library for easy handling of environment variables
I published the library as an open source project, maybe it will be useful for someone.
I needed a singleton object that stores all my settings from the .env file, as well as environment variables.
I also needed to be able to set the type of environment variables, so I could operate with logical values or numbers, not just strings.
I also want to have a universal object, where I can access variables in any case. It would also be convenient for me to access variables not only as object properties, but also as dictionary keys (for example, when I need a programmatically generated key name).
Installation
The library published in pypi.org
pip install magic-config
Examples
DEBUG=1 myapp.py
In my app script:
Configure custom variables
You can add variables to the object:
Prepared autogenerators for DB URIs
For example in .env file you can write only this data:
MONGO_HOST="127.0.0.1"
MONGO_USER="user"
MONGO_PWD="*****"
MONGO_DB="test"
MONGO_PORT=27017
and in code you can call the MONGO_URL
output:
mongodb://user:passwd@127.0.0.1:27017/test?authSource=admin&tls=false
Set type casting for environment variables
For example if you create in source root magic.config file and write:
DEBUG="bool"
DEBUG_STEP="bool"
DEBUG_USER_ID="int"
then you run
DEBUG=1 DEBUG_STEP=1 DEBUG_USER_ID=10011888 python server.py
Source code
How I publish it in PyPi
A little note on how I did it. When I did everything according to the instructions and articles from other authors — nothing worked for me. I had to find my own way.