YAML Config

0.1.5 · active · verified Sat Apr 11

Python client for reading YAML based config files. It provides a `Config` class for retrieving configuration variables from YAML files. It allows configuration of root and directory paths via environment variables. The library is currently at version 0.1.5 and has a low-cadence release cycle, with the last PyPI release in June 2020.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `Config` subclass to load a YAML file and access its properties. It shows how to define a `default_file` and access nested configurations.

import os
from yaml_config import Config

# Create a dummy config file for the example
config_content = """
database:
  host: localhost
  port: 5432
logging:
  level: INFO
"""
with open("my_app_config.yaml", "w") as f:
    f.write(config_content)

# You can optionally set environment variables to define config root/dir
# For example, in your shell: export MYAPP_CONFIG_ROOT=/tmp
# This example assumes the file is in the current working directory.

class MyAppConfig(Config):
    # Define the default config file name to be loaded
    default_file = 'my_app_config.yaml'
    # If using environment variables for root/dir, define a prefix:
    # default_env_prefix = 'MYAPP' # Would look for MYAPP_CONFIG_ROOT / MYAPP_CONFIG_DIR

# Load the configuration
config = MyAppConfig()

# Access configuration values
print(f"Database Host: {config.database.host}")
print(f"Database Port: {config.database.port}")
print(f"Logging Level: {config.logging.level}")

# Clean up dummy config file
os.remove("my_app_config.yaml")

view raw JSON →