Configuration Library Wrappers
This library, referred to as 'configs' on GitHub, provides wrappers for handling configuration through JSON, YAML, environment variables, and command-line arguments. It aims to simplify the process of loading and accessing hierarchical configuration settings. It is currently at version 0.0.1, indicating an early development stage with an infrequent release cadence.
Warnings
- gotcha The PyPI slug 'config-parser' (as specified in the prompt) does not directly correspond to the GitHub repository 'mattpaletta/configs' (which has version 0.0.1). The `pip install config-parser` command will install a different, unrelated library. To install the library described here, it must be installed directly from its GitHub repository.
- gotcha The library is in a very early development stage (version 0.0.1) and lacks comprehensive documentation. Its API, features, and stability may change frequently without explicit breaking change notices.
- gotcha By default, the `Config` class attempts to load configuration from `config.json` or `config.yaml` files located in the current working directory. If these files are not present or explicitly specified, configuration might be empty or incomplete.
- gotcha Accessing nested configuration values (e.g., `app_config.database.host`) relies on the underlying structure. Attempting to access a non-existent key will likely result in an `AttributeError` or `KeyError` depending on the access method, without robust error handling built into the accessor.
Install
-
pip install git+https://github.com/mattpaletta/configs.git#egg=configs
Imports
- Config
from configs.config import Config
Quickstart
import os
from configs.config import Config
# Create a dummy config.json file for demonstration
with open('config.json', 'w') as f:
f.write('{"database": {"host": "localhost", "port": 5432}}')
# Set an environment variable (optional)
os.environ['APP_ENV'] = 'development'
# Initialize Config - it will look for config.json or config.yaml by default
# in the current directory, and also process environment variables.
# For demonstration, we explicitly pass a config file path or let it discover.
# In a real app, you might pass sys.argv for argparse processing.
# Example 1: Load from default config.json
app_config = Config()
print(f"DB Host: {app_config.database.host}")
print(f"DB Port: {app_config.database.port}")
print(f"App Environment (from env var): {app_config.app_env}")
# Example 2: Accessing values dynamically or with a default
port_value = app_config.get('database.port', default=5432)
print(f"DB Port (via .get() with default): {port_value}")
# Clean up dummy file and env var
os.remove('config.json')
del os.environ['APP_ENV']