Skip to main content

Lazy environment variable configuration of declarative style.

Project description


Decoupled and lazy library to get settings from an environment variables.


Environment variables are the most common way to configure projects, based on Docker, hosted on clouds e.t.c. In the end it is secure and simple way. So grab variables from an environment is the only feature of the package.

First issue which becomes obvious at the simplest approach for getting environment variables is that attempt to get the environment variables too early. For example an app is initialized by tests and you need different values for the settings at different tests. But if the values obtained in this way:

import os

VAR_A = os.environ['VAR_A']

from . import *

def a():

from . import module_a

class Test(unittest.TestCase):
    @mock.patch('os', 'environ', {"VAR_A": 1})
    def test_a(self, env):

the values already loaded to the memory. Therefor mock will not work. And the most sad part that this issue is not solved in the most of advanced configuration libraries. So this is the first issue which solved here by Buddy Config.


pip install buddy_config

Getting started

Buddy Config aims to be as declarative as possible.

import buddy_config

class MyConf(metaclass=buddy_config.Config):

This is minimum of declaration that you need to have a setting. Let's pale it out. The name of a class attribute is the name of the setting, exactly how you suppose to call it. A string at position 0 of the fallowing tuple is a name of an environment variable that you are going to retrieve from the environment. And the type class at the position 1 of the tuple is the type value after it retrieved from the environment. Both values are required.

To use the config you need to instantiate it:

my_conf = MyConfig()

The value is not going to be retrieved until you actually get the attribute from the config.


Also it is not cached.


Defaults are flexible and could be different per instance of the config.

my_conf_a = MyConfig(NAME_OF_A_SETTING_A=1)
my_conf_b = MyConfig(NAME_OF_A_SETTING_A=2)


Moreover, you can add pre-processing of retrieved values.

class MyConf(metaclass=Config):

The optional items of the tuple are pre-processors. Pre-processor is a callable which must return a value of int, float, string or bytes value.

If you need to create a combined setting using values from environment variables, no problem, buddy config can do that:

class MyConf(metaclass=Config):
        lambda self: int(self.NAME_OF_A_SETTING_A) + self.NAME_OF_A_SETTING_B

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for buddy-config, version 0.1.6
Filename, size File type Python version Upload date Hashes
Filename, size buddy-config-0.1.6.tar.gz (4.2 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page