Dynaconf - Let your settings to be Dynamic

Dynaconf

dynaconf - The dynamic configurator for your Python Project

dynaconf is an OSM (Object Settings Mapper) it can read settings variables from a set of different data stores such as python settings files, environment variables, redis, memcached, ini files, json files, yaml files and you can customize dynaconf loaders to read from wherever you want. (maybe you really want to read from xml files ughh?)

GITHUB REPO: https://github.com/rochacbruno/dynaconf







What is Dynaconf?

Dynaconf is a common point of access to settings variables, you import only one object in your project and from that object you can access settings variables from Python settings file, from environment variables, from parsed yaml, ini, json or xml files, from datastores as Redis and MongoDB or from wherever your need if you write a simple dynaconf loader.

How it works

Install it

pip install dynaconf

Use it

from dynaconf import settings print settings.SOME_VARIABLE or print settings.get('SOME_VARIABLE')

By default Dynaconf will try to use a file called settings.py on the root of your project, if you place that file there all upper case variables will be read

You can also replace the file exporting an environment variable pointing to the module or location for the settings file.

# using module name export DYNACONF_SETTINGS=myproject.production_settings # or using location path export DYNACONF_SETTINGS=/etc/myprogram/settings.py

Doing that when you use from dynaconf import settings the variables will be read from that file.

So how it is Dynamic?

Now think you have your program done and you want to deploy to a certain infrastructure for testing or maybe different deployment, you don't need to rewrite the settings file. Just export some variables to your environment.

export DYNACONF_MYSQL_HOST=myserver.com

Now in your project you can do:

from dynaconf import settings print settings.MYSQL_HOST myserver.com

The default prefix for exported envvars is by default DYNACONF_ but you also can change it if needed.

But what if I have some typed values to export?

You can also define type casting when exporting and those types will be used to parse the values.

export DYNACONF_NUMBER='@int 123' export DYNACONF_FLOAT='@float 12.2' export DYNACONF_FLAG='@bool yes' export DYNACONF_FLAG2='@bool disabled' export DYNACONF_LIST='@json [1, 2, 3, 4]' export DYNACONF_DICT='@json {"name": "Bruno"}'

Now you can read all those values from your project and it will be loaded with correct type casting.

from dynaconf import settings type(settings.NUMBER) int type(settings.FLOAT) float type(settings.FLAG) bool print settings.FLAG2 == False True print settings.LIST[1] 2 print settings.DICT['name'] Bruno

Nice! But I don't want to use envvars because I use autoscaling and I want my machines to share a settings environment how to do it?

Redis

Go to your settings file (default settings.py) and put

# connection REDIS_FOR_DYNACONF = { 'host': 'localhost', 'port': 6379, 'db': 0 } # and loader LOADERS_FOR_DYNACONF = [ 'dynaconf.loaders.env_loader', 'dynaconf.loaders.redis_loader' ]

Now you can store settings variables directly in Redis using a hash named by default DYNACONF_DYNACONF

If you don't want want to write directly you can use the Redis writer helper in a python REPL. (ipython as example)

from dynaconf.utils import redis_writer from dynaconf import settings redis_writer.write(settings, name='Bruno', mysql_host='localhost', MYSQL_PORT=1234)

And the above will be store in Redis as a hash int the form.

DYNACONF_DYNACONF: NAME='Bruno' MYSQL_HOST='localhost' PORT='@int 1234'

And of course you can now read those variables in the project, all the casting wildcards also works on Redis but if you want to skip type casting, write as string intead of PORT=1234 use PORT='1234' as redis stores everything as string anyway.

There is more

Dynaconf has support for using different namespaces in the same project, you can also write your own loaders, you can find more information on the repository https://github.com/rochacbruno/dynaconf

Contribute

All contributions are very welcome!!

Acknowledgements

Dynaconf was inspired by Flask app config and also by Django settings module.

Please enable JavaScript to view the comments powered by Disqus.

Disqus