Skip to main content

Layered configuration loader merging env vars, files, and defaults

Project description

philiprehberger-config-kit

Tests PyPI version GitHub release Last updated License Bug Reports Feature Requests Sponsor

Layered configuration loader merging env vars, files, and defaults.

Installation

pip install philiprehberger-config-kit

Usage

Basic Setup

from philiprehberger_config_kit import Config

config = Config(
    sources=[
        Config.defaults({"port": 3000, "debug": False, "log_level": "info"}),
        Config.json_file("config.json", optional=True),
        Config.env_file(".env", optional=True),
        Config.env(prefix="APP_"),
    ]
)

# Typed access
port = config.get_int("port")
debug = config.get_bool("debug")
db_url = config.get_str("database_url")
timeout = config.get_float("timeout", default=5.0)
hosts = config.get_list("allowed_hosts")

# Nested access (dot notation)
redis_host = config.get_str("redis.host")

# Validation
config.require("database_url", "secret_key")  # raises ConfigError if missing

# Check existence
if config.has("cache_ttl"):
    ttl = config.get_int("cache_ttl")

Source Priority

Sources are applied in order -- later sources override earlier ones:

Config(sources=[
    Config.defaults({...}),       # lowest priority
    Config.json_file("..."),      # overrides defaults
    Config.env_file(".env"),      # overrides JSON
    Config.env(prefix="APP_"),    # highest priority
])

Typed List Getters

Parse comma-separated values into typed lists:

config = Config(sources=[
    Config.defaults({"ports": "8080,8081,8082", "rates": "1.5,2.0,3.7"}),
])

ports = config.get_int_list("ports")      # [8080, 8081, 8082]
rates = config.get_float_list("rates")    # [1.5, 2.0, 3.7]

# Custom separator
config2 = Config(sources=[Config.defaults({"ids": "1|2|3"})])
config2.get_int_list("ids", sep="|")      # [1, 2, 3]

Config Flattening

Export nested config as a flat dictionary with dot-notation keys:

config = Config(sources=[
    Config.defaults({"db": {"host": "localhost", "port": 5432}, "debug": True}),
])

flat = config.flatten()
# {"db.host": "localhost", "db.port": "5432", "debug": "True"}

# With a prefix
flat = config.flatten(prefix="app")
# {"app.db.host": "localhost", "app.db.port": "5432", "app.debug": "True"}

Environment Variables

With prefix="APP_", env vars are mapped:

  • APP_PORT -> port
  • APP_DATABASE__HOST -> database.host (double underscore = nested)

.env Files

DATABASE_URL=postgresql://localhost/mydb
SECRET_KEY="my-secret"
DEBUG=true

Bool Coercion

get_bool() accepts: true/false, 1/0, yes/no, on/off

API

Function / Class Description
Config(sources) Layered configuration with typed access via get(), get_str(), get_int(), get_float(), get_bool(), get_list()
Config.get_int_list(key, sep) Split a string value and convert each element to int
Config.get_float_list(key, sep) Split a string value and convert each element to float
Config.flatten(prefix) Export nested config as flat dict with dot-notation keys
ConfigError(missing) Raised when required config keys are missing

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this package useful, consider giving it a star on GitHub — it helps motivate continued maintenance and development.

LinkedIn More packages

License

MIT

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

philiprehberger_config_kit-0.2.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_config_kit-0.2.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_config_kit-0.2.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_config_kit-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a63988249862a07c7988640a694abce3d48c7c104a0a1db76b55c3fd917e7e6a
MD5 286dc9e4518f1ddebb9148d33f434420
BLAKE2b-256 bfffa42e05db8e60bd78554a71fde82b9522abcf51175733cd6ccd1822ffd005

See more details on using hashes here.

File details

Details for the file philiprehberger_config_kit-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_config_kit-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c807ed19de7512a4c5b6d69e5737dc411d1cf51485968402509700a766dadea1
MD5 e6b7e59e3f956a5d4e7b82f0c1384d47
BLAKE2b-256 578765bdc6e513aae4deb88420d3484aa9151484a90c5c2dc4191a04b88924fd

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