Skip to main content

Universal config resolver with precedence: direct → config → env → default

Project description

Priority Config

Universal configuration resolver with clean precedence hierarchy: direct → config → env → default

Python 3.8+ PyPI License: MIT

Installation

pip install priority-config

Quick Start

from priority_config import PriorityConfig

# Create config resolver
config = PriorityConfig(
    config_dict={"port": 3000, "debug": True},
    env_prefix="MYAPP_"
)

# Resolve with precedence
port = config.resolve("port", direct_value=None, default=8000, type=int)
# Returns: 3000 (from config_dict)

debug = config.resolve("debug", direct_value=False, default=True, type=bool)
# Returns: False (direct_value takes precedence)

# Environment variables (MYAPP_HOST) override config
host = config.resolve("host", default="localhost")
# Returns: value from MYAPP_HOST env var, or "localhost"

Features

  • Clean Precedence: direct → config → env → default
  • Automatic Type Conversion: str, int, float, bool, list
  • Sensitive Data Masking: Auto-detects and masks passwords, keys, tokens
  • Resolution Logging: Track where each value came from
  • Zero Dependencies: Pure Python, works with 3.8+

Usage Examples

Django-style Configuration

django_config = PriorityConfig({}, env_prefix="DJANGO_", auto_uppercase=True)
debug = django_config.resolve("debug", default=False, type=bool)
secret = django_config.resolve("secret_key", default="", mask=True)

Custom Application

import yaml

with open("config.yaml") as f:
    config_data = yaml.safe_load(f)

app_config = PriorityConfig(config_data, env_prefix="MYAPP_")
port = app_config.resolve("port", default=8000, type=int)
workers = app_config.resolve("workers", default=4, type=int)

Resolution Logging

config = PriorityConfig({"api_url": "https://api.dev"}, "APP_")

# Resolve multiple values
api_url = config.resolve("api_url", default="https://api.prod")
timeout = config.resolve("timeout", default=30, type=int)
api_key = config.resolve("api_key", default="", mask=True)

# See resolution log
config.print_resolutions()

Output:

Configuration Resolution Log:
--------------------------------------------------
api_url              = https://api.dev      (config)
timeout              = 30                  (default)
api_key              = te**key             (env:APP_API_KEY)

API Reference

PriorityConfig(config_dict=None, env_prefix="", auto_uppercase=True)

  • config_dict: Dictionary with configuration values
  • env_prefix: Prefix for environment variables (e.g., "MYAPP_")
  • auto_uppercase: Whether to uppercase keys for env lookup

resolve(key, direct_val=None, default=None, type=str, mask=None)

  • key: Configuration key to resolve
  • direct_val: Direct value (highest precedence)
  • default: Default value if not found elsewhere
  • type: Type conversion (str, int, float, bool, list)
  • mask: Override automatic masking of sensitive values

print_resolutions()

Print resolution log showing where each value came from.

clear_log()

Clear the resolution log.

License

MIT

Contact

Yusuke.Watanabe@scitex.ai

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

priority_config-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

priority_config-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file priority_config-0.1.0.tar.gz.

File metadata

  • Download URL: priority_config-0.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.0rc1

File hashes

Hashes for priority_config-0.1.0.tar.gz
Algorithm Hash digest
SHA256 18deff07c341052fd55037f0402ad45989b7328570ec9ef537ba9cbb72c06958
MD5 57c0aee5ceba42be22218b1f0f66ad6a
BLAKE2b-256 dfbc8b683132459a58e036f7be3ecacab72e2417c5eff8f7cd5dceecc5ae09cc

See more details on using hashes here.

File details

Details for the file priority_config-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: priority_config-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.0rc1

File hashes

Hashes for priority_config-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 331fd9229b3b461b1177b6f0490b95c07c8167902211921aac4491f2ad6ad852
MD5 9977685b41ed384c749604eb10027908
BLAKE2b-256 82207ff9e60f100a415f9aacb04643780168af7a5454236f40061db8d8298144

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