Skip to main content

A minimal, flexible Python configuration loader with environment variable support

Project description

liteconfig_py

CI codecov Python Version PyPI version License: MIT Code style: black

A minimal, flexible Python configuration loader with environment variable support.

Overview

liteconfig_py is a simple Python library designed to streamline configuration management for Python applications, CLI tools, scripts, and services. It allows loading configuration from YAML, JSON, or TOML files, and automatically integrates environment variables to override configuration values, offering flexibility across different deployment environments.

Features

  • Flexible Format Support: YAML, JSON, TOML.
  • Environment Overrides: Automatic merging of environment variables.
  • Nested Configurations: Supports nested configuration retrieval via dot notation.
  • Schema Validation: Optional Pydantic integration for type-safe configuration.
  • Type Hints: Full type annotation support for better IDE experience.
  • Minimal Dependencies: Lightweight and efficient.
  • Simple API: Easy-to-use interface with intuitive methods.
  • 95%+ Test Coverage: Comprehensive test suite ensuring reliability.

Installation

pip install liteconfig_py
  • Optional dependencies:
pip install PyYAML toml

Usage

Basic Example

Configuration file: config.yml

app:
  debug: true
  port: 8080
database:
  host: localhost
  user: user123
  password: pass123

Python script: main.py

from liteconfig_py import Config

config = Config('config.yml')

print(config.get('app.debug'))  # True
print(config.get('database.host'))  # 'localhost'

Overriding Values with Environment Variables

Environment variables automatically override file values if they match the configuration keys:

export DATABASE_HOST='db.production.com'
export APP_PORT=80

Python script:

from liteconfig_py import Config

config = Config('config.yml')

print(config.get('database.host'))  # 'db.production.com'
print(config.get('app.port'))  # 80

Advanced Usage

Default Values

config.get('nonexistent.key', default='default_value')

Loading Different Formats

  • JSON:
config = Config('config.json')
  • TOML:
config = Config('config.toml')

Using Environment Variable Prefix

If you want to limit which environment variables can override your configuration, you can specify a prefix:

config = Config('config.yml', env_prefix='MYAPP_')

This will only consider environment variables that start with MYAPP_, like MYAPP_DATABASE_HOST.

Loading from .env Files

Load environment variables from .env files for local development:

from liteconfig_py import Config

# Load config and .env file
config = Config('config.yml', load_dotenv=True, dotenv_path='.env')

# Or use default .env location
config = Config('config.yml', load_dotenv=True)

Example .env file:

# Database configuration
DATABASE_HOST=localhost
DATABASE_PORT=5432
DATABASE_USER=dev_user
DATABASE_PASSWORD=dev_pass123

# Application settings
APP_DEBUG=true
APP_PORT=8080

Note: Existing environment variables take precedence over .env file values.

Schema Validation with Pydantic

For type-safe configuration with validation, you can use Pydantic models:

from pydantic import BaseModel, Field
from liteconfig_py import Config

class DatabaseConfig(BaseModel):
    host: str
    port: int = Field(ge=1, le=65535)
    user: str
    password: str

class AppConfig(BaseModel):
    debug: bool = False
    port: int = 8080

config = Config('config.yml')

# Validate entire config
app_config = config.validate_section('app', AppConfig)
print(app_config.debug)  # Type-safe access with validation

# Validate specific section
db_config = config.validate_section('database', DatabaseConfig)
print(db_config.host)  # Guaranteed to be a string

Install with validation support:

pip install liteconfig_py[validation]

Examples

Check out the examples directory for comprehensive demonstrations:

See the examples README for detailed information.

Potential Enhancements

  • Auto-reload configuration upon file changes
  • Config file watching and hot-reloading
  • Config encryption/decryption support

Why liteconfig_py?

  • Simplifies common configuration tasks
  • Enhances portability and adaptability between environments
  • Easy integration into existing projects
  • Lightweight with minimal dependencies

Changelog

See CHANGELOG.md for a detailed history of changes and releases.

Contributing

Contributions are welcome! Please submit issues, improvements, or pull requests on GitHub.

License

MIT License. See LICENSE for details.

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

liteconfig_py-0.2.0.tar.gz (17.5 kB view details)

Uploaded Source

Built Distribution

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

liteconfig_py-0.2.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: liteconfig_py-0.2.0.tar.gz
  • Upload date:
  • Size: 17.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for liteconfig_py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 1330e88139efe8d747c5acc7d38213ad682145a3844b13e3dd17446caa8bbb71
MD5 413f837a01280999dbd31a247424ac25
BLAKE2b-256 f2e735bc7c2e12711127c31f83ba72b2e00bcc80eacc4712094b801e09fb27c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: liteconfig_py-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.3

File hashes

Hashes for liteconfig_py-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 564c1ffabe90334a2a8d09d94d5628f7fbda8dae856dad4f4f32aea4d034bd34
MD5 f3ce20b5bd5002841129e1defcb8f70c
BLAKE2b-256 57120f1d78bf806714d5cf66108a898a2fabc8154816e7d244d912bb7c8484da

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