Skip to main content

Build pypi versions codecov license

Python configuration utilities

Implementation of key-value pair based configuration for Python applications.

Features:

  • support for most common sources of application settings
  • support for overriding settings in sequence
  • support for nested structures and lists, using attribute notation
  • strategy to use environment specific settings

This library is freely inspired by .NET Core Microsoft.Extensions.Configuration namespace and its pleasant design (ref. MSDN documentation, Microsoft Extensions Configuration Deep Dive).

The main class is influenced by Luciano Ramalho`s example of JSON structure explorer using attribute notation, in his book Fluent Python.

Supported sources:

  • yaml files
  • json files
  • ini files
  • environmental variables
  • dictionaries
  • keys and values

Installation

pip install roconfiguration

Examples

YAML file and environmental variables

In this example, configuration will be comprised of anything inside a file settings.yaml and environmental variables. Settings are applied in order, so environmental variables with matching name override values from the yaml file.

from roconfiguration import Configuration

config = Configuration()

config.add_yaml_file("settings.yaml")

config.add_environmental_variables()

YAML file, optional file by environment

In this example, if an environmental variable with name APP_ENVIRONMENT and value dev exists, and a configuration file with name settings.dev.yaml is present, it is read to override values configured in settings.yaml file.

import os
from roconfiguration import Configuration

environment_name = os.environ["APP_ENVIRONMENT"]

config = Configuration()

config.add_yaml_file("settings.yaml")
config.add_yaml_file(f"settings.{environment_name}.yaml", optional=True)

config.add_environmental_variables()

Filtering environmental variables by prefix

import os
from roconfiguration import Configuration

config = Configuration()

# will read only environmental variables
# starting with "APP_", case insensitively
config.add_environmental_variables("APP_")

Ini files

Ini files are parsed using the built-in configparser module, therefore support [DEFAULT] section; all values are kept as strings.

from roconfiguration import Configuration

config = Configuration()

config.add_ini_file("settings.ini")

JSON files

JSON files are parsed using the built-in json module.

from roconfiguration import Configuration

config = Configuration()

config.add_json_file("settings.json")

Dictionaries

from roconfiguration import Configuration

config = Configuration({"host": "localhost", "port": 8080})

config.add_map({"hello": "world", "example": [{"id": 1}, {"id": 2}]})

assert config.host == "localhost"
assert config.port == 8080
assert config.hello == "world"
assert config.example[0].id == 1
assert config.example[1].id == 2

Keys and values

from roconfiguration import Configuration

config = Configuration({"host": "localhost", "port": 8080})

config.add_value("port", 44555)

assert config.host == "localhost"
assert config.port == 44555

Overriding nested values

config = Configuration(
    {
        "a": {
            "b": 1,
            "c": 2,
            "d": {
                "e": 3,
                "f": 4,
            },
        }
    }
)

assert config.a.b == 1
assert config.a.d.e == 3
assert config.a.d.f == 4

config.add_value("a:d:e", 5)

assert config.a.d.e == 5
assert config.a.d.f == 4

Overriding nested values using env variables

config = Configuration(
    {
        "a": {
            "b": 1,
            "c": 2,
            "d": {
                "e": 3,
                "f": 4,
            },
        }
    }
)

assert config.a.b == 1
assert config.a.d.e == 3
assert config.a.d.f == 4

# NB: if an env variable such as:
# a:d:e=5
# or...
# a__d__e=5
#
# is defined, it overrides the value  from the dictionary

config.add_environmental_variables()

assert config.a.d.e == 5

Overriding values in list items using env variables

config = Configuration(
    {
        "b2c": [
            {"tenant": "1"},
            {"tenant": "2"},
            {"tenant": "3"},
        ]
    }
)

config.add_value("b2c:1:tenant", "4")

assert config.b2c[0].tenant == "1"
assert config.b2c[1].tenant == "4"
assert config.b2c[2].tenant == "3"

Develop and run tests locally

pip install -r requirements.txt

# run tests using automatic discovery:
pytest

Metadata

Release files for roconfiguration 1.0.9

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

Source distribution (sdist)

Source distribution for roconfiguration 1.0.9
File Size Uploaded
roconfiguration-1.0.9.tar.gz 6.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for roconfiguration 1.0.9
File Interpreter ABI Platform
roconfiguration-1.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 12.3 kB

Release files / roconfiguration-1.0.9.tar.gz

Download URL roconfiguration-1.0.9.tar.gz
Size 6.1 kB
Tags Source
SHA-256 checksum
How to use checksums
14c12a4ef90710d6b19bd13d79dab6f32516479380246469548f3eb297970136
BLAKE2b-256 checksum
How to use checksums
f8f7e313b8b19061b8f2932a774a1b83252631e998080ad222c545e8afa75226
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release files / roconfiguration-1.0.9-py3-none-any.whl

Download URL roconfiguration-1.0.9-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8831f983437def35ecf0ee8c865c5822e7c980687043c6fc4ade896611539df4
BLAKE2b-256 checksum
How to use checksums
38de647adcf75a1b57290d02d204306a4a6e7f7209edfc752e50df4d73927b00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.9.5

Release history Release notifications | RSS feed

This release

1.0.9 This release

2 release files

1.0.8

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

1 release file

1.0.0

1 release file

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