Skip to main content

buddy_config

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

Motivation

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

The first issue that becomes obvious at the simplest approach for getting environment variables is attempting 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:

init.py

import os

VAR_A = os.environ['VAR_A']

module_a.py

from . import *

def a():
    b(VAR_A)

test.py

from . import module_a

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

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

Installation

pip install buddy_config

Getting started

Buddy Config aims to be as declarative as possible.

import buddy_config


class MyConf(metaclass=buddy_config.Config):
    NAME_OF_A_SETTING_A = "NAME_OF_AN_ENVIRONMENT_VARIABLE_A", str

This is a 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 following tuple is a name of an environment variable that you will retrieve from the environment. And the type class at position 1 of the tuple is the type value after it is 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.

print(my_config.NAME_OF_A_SETTING_A)

Also it is not cached.

os.environ["NAME_OF_AN_ENVIRONMENT_VARIABLE_A"] = "3"
print(my_config.NAME_OF_A_SETTING_A)

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)

print(my_conf_a.NAME_OF_A_SETTING_A)
print(my_conf_b.NAME_OF_A_SETTING_A)

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

class MyConf(metaclass=Config):
    NAME_OF_A_SETTING_C = "NAME_OF_AN_ENVIRONMENT_VARIABLE_C", int, lambda x: x ** 2

The optional items of the tuple are pre-processors. A pre-processor is a callable that 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):
    NAME_OF_A_SETTING_A = "NAME_OF_AN_ENVIRONMENT_VARIABLE_A", str
    NAME_OF_A_SETTING_B = "NAME_OF_AN_ENVIRONMENT_VARIABLE_B", int
    COMBINED_SETTING = (
        lambda self: int(self.NAME_OF_A_SETTING_A) + self.NAME_OF_A_SETTING_B
    )

Metadata

Release files for buddy-config 0.1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for buddy-config 0.1.7
File Size Uploaded Provenance Actions
buddy-config-0.1.7.tar.gz 4.5 kB Without provenance Details for buddy-config-0.1.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for buddy-config 0.1.7
File Interpreter ABI Platform Actions
buddy_config-0.1.7-py3-none-any.whl Python 3 none any Details for buddy_config-0.1.7-py3-none-any.whl

Total release size: 21.5 kB

Release files / buddy-config-0.1.7.tar.gz

Download URL buddy-config-0.1.7.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
775cc074cf6909328483bc060bc4b2b8ebc1b3daea8241a26412a8b69a72c5c0
BLAKE2b-256 checksum
How to use checksums
e947adb99a5d4ce8dac5dde284e62b135e656c61b05d75c8189994df0ea4c921
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.0

Release files / buddy_config-0.1.7-py3-none-any.whl

Download URL buddy_config-0.1.7-py3-none-any.whl
Size 16.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1bbab918b507b9499bca61dbf67d531ad3d73d1dc15774d720b1dbc3cfb08d2e
BLAKE2b-256 checksum
How to use checksums
3032c7e443dd401cd2be3a4f166560e6ac799bebc1c33285704e7e6f35d9bdbb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.0

Security for buddy-config 0.1.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page