Skip to main content

PyConfita: Confita-like for Python

Library that ease reading a value from multiple key-value stores/backends with ordered evaluation.

Disclaimer

Free implementation of the GO library Confita.

Features

  • Backends/stores supported:
    • Environment variables (EnvBackend);
    • File (YAML format) (FileBackend);
    • Python dictionary object (DictBackend);
    • Vault key-value store (VaultBackend);
    • String parsing (serialized JSON) (StringBackend);
  • Backends evaluation order: directly set by the order of backends in Confita.backends list. The last not None evaluated value is returned;
  • Explicit type conversion supported for str, bool, int, float;
  • Case sensitivity option: option to read key with casing variations (uppercased, lowercased).

Quickstart

from pyconfita import (
    LoggingInterface,
    Confita,
    EnvBackend,
    StringBackend,
    DictBackend,
    VaultBackend,
    FileBackend
)
dumb_logger = LoggingInterface()
c = Confita(
    logger=dumb_logger,
    backends=[
        FileBackend("/abs/path/vars.yaml"),
        DictBackend({"FOO": "bar"}),
        StringBackend("{'HELLO': 'WORLD'}"),
        VaultBackend(dumb_logger, default_key_path=f"path1"),
        EnvBackend(),
    ],
)

v = c.get("FOO") 
# At least DictBackend should evaluate it as not None
assert v is not None
# Type should be `str` 
assert isinstance(v, str)

Evaluation order

Evaluation is performed in order of the list of backends. Next backend has priority over previous backend to set the value.

import os
from pyconfita import (
    LoggingInterface,
    Confita,
    EnvBackend,
    DictBackend
)
dumb_logger = LoggingInterface()

os.environ.setdefault("KEY", "VALUE_FROM_ENV")

c = Confita(
    logger=dumb_logger,
    backends=[
        DictBackend({
            "KEY": "VALUE",
            "BOOL_1": "false",
            "BOOL_2": "true"
        }),
        EnvBackend(),
    ],
)

assert c.get("KEY") == "VALUE_FROM_ENV" # Environment backend overrides previous backends' values
assert c.get("BOOL_1") == "false" # No implicit type conversion 
assert c.get("BOOL_2", type=bool) # Explicit type conversion requested

# Reverse evaluation order by reversing list of backends
c = Confita(
    logger=dumb_logger,
    backends=[
        EnvBackend(),
        DictBackend({"KEY": "VALUE"}),
    ],
)

assert c.get("KEY") == "VALUE" # Dict backend overrides previous backends' values

Explicit type conversion

Type conversion must be explicit. Only str, bool, int, float types are supported. Default type is str.

from pyconfita import (
    LoggingInterface,
    Confita,
    DictBackend
)
dumb_logger = LoggingInterface()

c = Confita(
    logger=dumb_logger,
    backends=[
        DictBackend({
            "BOOL_1": "false",
            "BOOL_2": "true"
        }),
    ],
)

assert c.get("BOOL_1") == "false" # No implicit type conversion 
assert c.get("BOOL_2", type=bool) # Explicit type conversion requested

Case sensitivity

from pyconfita import (
    LoggingInterface,
    Confita,
    DictBackend
)
dumb_logger = LoggingInterface()

# Case sensitivity is enabled by default
c = Confita(
    logger=dumb_logger,
    backends=[DictBackend({"KEY": "VALUE"})],
)

assert c.get("KEY") == "VALUE" 
assert c.get("key") == None 

# Disable case sensitivity
c = Confita(
    logger=dumb_logger,
    backends=[DictBackend({"KEY": "VALUE"})],
    case_sensitive=False
)

assert c.get("KEY") == "VALUE" 
assert c.get("key") == "VALUE" 

Metadata

Release files for pyconfita 1.1.1

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

Source distribution (sdist)

Source distribution for pyconfita 1.1.1
File Size Uploaded
pyconfita-1.1.1.tar.gz 11.1 kB Details

Built distribution (wheel)

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

Total release size: 22.3 kB

Release files / pyconfita-1.1.1.tar.gz

Download URL pyconfita-1.1.1.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
cf657f387b2d19f61917eb5252c7414d3c2084186dc9dad797c4ce5a9421ea49
BLAKE2b-256 checksum
How to use checksums
8507cb47e1aaf92286824dc4132f51a9f4c7c81b90fae1aacc1cddaa7ae7aaf4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release files / pyconfita-1.1.1-py3-none-any.whl

Download URL pyconfita-1.1.1-py3-none-any.whl
Size 11.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4344b9f8aea2afe6816317dd04e4b6b523bf4f47c1e796900e4663f5e6997556
BLAKE2b-256 checksum
How to use checksums
34146f00760d06534f1daf0abe34e183053ffc0e5a1db26dd345f21fa0b40782
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.1

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1.0

2 release files

1.0.14

2 release files

1.0.12

2 release files

1.0.11

2 release files

1.0.10

2 release files

1.0.9

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

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