Skip to main content

config2 PyPI version Build Status Coverage Status

Python environment configuration simplified.

Introduction

Config2 (for Python) - which is highly inspired by node-config - organizes hierarchical configurations for your app deployments.

It lets you define a set of default parameters, and extend them for different deployment environments (development, qa, staging, production, etc.).

Configurations are stored in configuration files within your application, and can be overridden and extended by environment variables, command line parameters, or external sources.

This gives your application a consistent configuration interface shared among a growing list of npm modules also using node-config.

NOTE: This project is more or less in pair with node-config implementation, with exception for some fluff that could be considered too much magic such as deployment specific multi-instance deployments which I so far haven't found any good motivation for, and some other questionable advanced features mentioned in the wiki pages.

Project Guidelines

...based on node-config project guidelines:

  • Simple - Get started fast
  • Powerful - For multi-node enterprise deployment - excluded because with power comes responsability
  • Flexible - Supporting multiple config file formats
  • Lightweight - Small file and memory footprint
  • Predictable - Well tested foundation for module and app developers

Install

Install using pip:

$ pip install config2

Use

1. Assuming we have a python application project...

some_project
└── app.py

app.py - some app making serious $$$

# business logic
print('$$$')

2. Let's add some environment specific config files...

some_project
└── config
    ├── default.yml
    ├── development.yml
    ├── foo.yml
    └── production.yml
└── app.py

default.yml - with some bogus nested settings shared for all environments (defaults)

a1: DEFAULT 1
a2:
    b1: [1, 2, 3]
    b2:
        -   foo
        -   bar
    b3:
        c1: 1
        c2: "DEFAULT 2"

development.yml - with some bogus nested settings overriden for development environment (overriden)

a2:
    b2:
        -   DEV 1
    b3:
        c2: "DEV 2"
some_key_only_for_dev: true

foo.yml - with some bogus nested settings overriden for foo environment (overriden)

a2:
    b2:
        -   FOO 1
    b3:
        c2: "FOO 2"
some_key_only_for_foo: true

production.yml - with some bogus nested settings overriden for production environment (overriden)

a2:
    b2:
        -   PROD 1
    b3:
        c2: "PROD 2"
some_key_only_for_prod: true

3. Let's now run the app using various environments...

$ python app.py

from config2.config import config

config.get_env() # => None
config.get() # => {'a1': 'DEFAULT 1', 'a2': {'b1': [1, 2, 3], 'b2': ['foo', 'bar'], 'b3': {'c1': 1, 'c2': 'DEFAULT 2'}}}

config.a1 # => 'DEFAULT 1'
config.a2 # => {'b1': [1, 2, 3], 'b2': ['foo', 'bar'], 'b3': {'c1': 1, 'c2': 'DEFAULT 2'}}
config.a2.b3.c2 # => 'DEFAULT 2'

print('$$$')

$ ENV=development python app.py

from config2.config import config

config.get_env() # => 'development'
config.get() # => {'a1': 'DEFAULT 1', 'a2': {'b1': [1, 2, 3], 'b2': ['DEV 1'], 'b3': {'c1': 1, 'c2': 'DEV 2'}}, 'some_key_only_for_dev': True}

config.a1 # => 'DEFAULT 1'
config.a2 # => {'b1': [1, 2, 3], 'b2': ['DEV 1'], 'b3': {'c1': 1, 'c2': 'DEV 2'}}
config.a2.b3.c2 # => 'DEV was here 2'

config.some_key_only_for_dev # => True

config.some_key_only_for_foo # => AttributeError
config.some_key_only_for_prod # => AttributeError

print('$$$')

$ ENV=foo python app.py

from config2.config import config

config.get_env() # => 'foo'
config.get() # => {'a1': 'DEFAULT 1', 'a2': {'b1': [1, 2, 3], 'b2': ['FOO 1'], 'b3': {'c1': 1, 'c2': 'FOO 2'}}, 'some_key_only_for_foo': True}

config.a1 # => 'DEFAULT 1'
config.a2 # => {'b1': [1, 2, 3], 'b2': ['FOO 1'], 'b3': {'c1': 1, 'c2': 'FOO 2'}}
config.a2.b3.c2 # => 'FOO was here 2'

config.key_only_for_foo # => True

config.some_key_only_for_dev # => AttributeError
config.some_key_only_for_prod # => AttributeError

print('$$$')

$ ENV=production python app.py

from config2.config import config

config.get_env() # => 'production'
config.get() # => {'a1': 'DEFAULT 1', 'a2': {'b1': [1, 2, 3], 'b2': ['PROD 1'], 'b3': {'c1': 1, 'c2': 'PROD 2'}}, 'some_key_only_for_foo': True}

config.a1 # => 'DEFAULT 1'
config.a2 # => {'b1': [1, 2, 3], 'b2': ['PROD 1'], 'b3': {'c1': 1, 'c2': 'PROD 2'}}
config.a2.b3.c2 # => 'PROD was here 2'

config.some_key_only_for_prod # => True

config.some_key_only_for_dev # => AttributeError
config.some_key_only_for_foo # => AttributeError

print('$$$')

etc.

4. Optionally, let's now introduce custom config environment variables...

some_project
└── config
    ├── custom-environment-variables.yml
    ├── default.yml
    ├── development.yml
    ├── foo.yml
    └── production.yml
└── app.py

custom-environment-variables.yml - with mappings of config keys to environment variables

a1: A1
a2:
    b3:
        c2: C2

5. Let's now run the app using custom environment variables to override config...

$ A1=x C2=y python app.py

from config2.config import config

config.get_env() # => None
config.get() # => {'a1': 'x', 'a2': {'b1': [1, 2, 3], 'b2': ['foo', 'bar'], 'b3': {'c1': 1, 'c2': 'y'}}}

config.a1 # => 'x'
config.a2 # => {'b1': [1, 2, 3], 'b2': ['foo', 'bar'], 'b3': {'c1': 1, 'c2': 'y'}}
config.a2.b3.c2 # => 'y'

print('$$$')

Test

Clone down source code:

$ make install

Run colorful tests, with only native environment (dependency sandboxing up to you):

$ make test

Run less colorful tests, with multi-environment (using tox):

$ make test-tox

About

This project was mainly initiated - in lack of existing alternatives - to be used at our work at Markable.ai to have common code conventions between various programming environments where Python (research, CV, AI) and Node.js (I/O, APIs, UIs, scripts) currently are most used.

License

Released under the MIT license.

Release files for config2 0.3.2

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

Source distribution (sdist)

Source distribution for config2 0.3.2
File Size Uploaded
config2-0.3.2.tar.gz 22.4 kB Details

Built distribution (wheel)

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

Total release size: 60.9 kB

Release files / config2-0.3.2.tar.gz

Download URL config2-0.3.2.tar.gz
Size 22.4 kB
Tags Source
SHA-256 checksum
How to use checksums
6b3406b45a8aa86f25225f880dfdff523b01203e867501ed6c772b736a9a74c7
BLAKE2b-256 checksum
How to use checksums
dfeec0548078db70217616ac355b938a1be0a9d8c5427844ad719eb407229237
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.20.1 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

Release files / config2-0.3.2-py3-none-any.whl

Download URL config2-0.3.2-py3-none-any.whl
Size 38.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
52fa6f70f16df59087e0bdf210fb2fd251452f5701e4668b34528e20388cc840
BLAKE2b-256 checksum
How to use checksums
f4a392a2693d7def37f0785b91fa8a5fa80023419c23e927db80e1f36d82d439
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.4.2 requests/2.20.1 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.5

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

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