Skip to content

Configuration

Configuration management for LavenderTown using environment variables and .env files.

Overview

LavenderTown automatically loads configuration from .env files when the package is imported. Configuration is searched in the following order:

  1. Current directory (.env)
  2. Parent directories (up to project root)
  3. Home directory (.lavendertown.env)

Functions

get_config

get_config(key, default=None)

Get configuration value from environment variable.

Parameters:

Name Type Description Default
key str

Environment variable name to retrieve

required
default str | None

Default value to return if key is not set

None

Returns:

Type Description
str | None

Configuration value as string, or default if not set

Example

Get log level with default::

log_level = get_config("LAVENDERTOWN_LOG_LEVEL", "WARNING")
Source code in lavendertown/config.py
def get_config(key: str, default: str | None = None) -> str | None:
    """Get configuration value from environment variable.

    Args:
        key: Environment variable name to retrieve
        default: Default value to return if key is not set

    Returns:
        Configuration value as string, or default if not set

    Example:
        Get log level with default::

            log_level = get_config("LAVENDERTOWN_LOG_LEVEL", "WARNING")
    """
    return os.getenv(key, default)

get_config_bool

get_config_bool(key, default=False)

Get boolean configuration value from environment variable.

Converts common truthy values (1, true, yes, on) to True, everything else to False.

Parameters:

Name Type Description Default
key str

Environment variable name to retrieve

required
default bool

Default value to return if key is not set

False

Returns:

Type Description
bool

Boolean configuration value

Source code in lavendertown/config.py
def get_config_bool(key: str, default: bool = False) -> bool:
    """Get boolean configuration value from environment variable.

    Converts common truthy values (1, true, yes, on) to True,
    everything else to False.

    Args:
        key: Environment variable name to retrieve
        default: Default value to return if key is not set

    Returns:
        Boolean configuration value
    """
    value = os.getenv(key)
    if value is None:
        return default

    return value.lower() in ("1", "true", "yes", "on")

get_config_int

get_config_int(key, default=None)

Get integer configuration value from environment variable.

Parameters:

Name Type Description Default
key str

Environment variable name to retrieve

required
default int | None

Default value to return if key is not set or invalid

None

Returns:

Type Description
int | None

Integer configuration value, or default if not set or invalid

Source code in lavendertown/config.py
def get_config_int(key: str, default: int | None = None) -> int | None:
    """Get integer configuration value from environment variable.

    Args:
        key: Environment variable name to retrieve
        default: Default value to return if key is not set or invalid

    Returns:
        Integer configuration value, or default if not set or invalid
    """
    value = os.getenv(key)
    if value is None:
        return default

    try:
        return int(value)
    except ValueError:
        return default

Usage Example

from lavendertown.config import get_config, get_config_bool, get_config_int

# Get string configuration
log_level = get_config("LAVENDERTOWN_LOG_LEVEL", "WARNING")

# Get boolean configuration
debug_mode = get_config_bool("LAVENDERTOWN_DEBUG", False)

# Get integer configuration
max_rows = get_config_int("LAVENDERTOWN_MAX_ROWS", 1000000)

Environment Variables

Common environment variables:

  • LAVENDERTOWN_LOG_LEVEL: Logging level (e.g., "INFO", "WARNING", "DEBUG")
  • LAVENDERTOWN_DEBUG: Enable debug mode (boolean)
  • LAVENDERTOWN_OUTPUT_DIR: Default output directory for exports

Configuration is automatically loaded when you import LavenderTown:

import lavendertown  # Configuration is loaded automatically

# Your code here