Skip to main content

PyConfita: Confita-like library for Python

Project description

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" 

Project details


Download files

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

Source Distribution

pyconfita-1.1.1.tar.gz (11.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyconfita-1.1.1-py3-none-any.whl (11.2 kB view details)

Uploaded Python 3

File details

Details for the file pyconfita-1.1.1.tar.gz.

File metadata

  • Download URL: pyconfita-1.1.1.tar.gz
  • Upload date:
  • Size: 11.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for pyconfita-1.1.1.tar.gz
Algorithm Hash digest
SHA256 cf657f387b2d19f61917eb5252c7414d3c2084186dc9dad797c4ce5a9421ea49
MD5 1a28bf9109c245c27a4cca911bcb814d
BLAKE2b-256 8507cb47e1aaf92286824dc4132f51a9f4c7c81b90fae1aacc1cddaa7ae7aaf4

See more details on using hashes here.

File details

Details for the file pyconfita-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: pyconfita-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 11.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.1

File hashes

Hashes for pyconfita-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4344b9f8aea2afe6816317dd04e4b6b523bf4f47c1e796900e4663f5e6997556
MD5 eb99556e89c6df713fa187096cedc2e0
BLAKE2b-256 34146f00760d06534f1daf0abe34e183053ffc0e5a1db26dd345f21fa0b40782

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page