A simple configuration manager for Python applications.
Project description
ModuleConfig
Overview
ModuleConfig is a Python class designed to facilitate the loading and management of configuration settings for applications. It supports both global and local configurations, allowing for a flexible and organized approach to handling application settings.
Features
- Load configurations from JSON and YAML files.
- Support for both global and module-specific local configurations.
- Ability to retrieve and set configuration values easily.
- Default values can be specified for configuration retrieval.
Requirements
- Python 3.x
PyYAMLlibrary for handling YAML files (install usingpip install PyYAML).
Usage
Importing the Module
To use the ModuleConfig class, import it into your Python script:
from module_config import ModuleConfig
Loading Configuration Files
Load Global Configuration
To load a global configuration file (either JSON or YAML):
ModuleConfig.load_global_config('path/to/global_config.json')
Load Local Configuration for a Module
To load a local configuration for a specific module:
ModuleConfig.load_local_config('module_name', 'path/to/local_config.yaml')
Retrieving Configuration Values
To get a configuration value, use the get_config method:
# Retrieve a global config value
value = ModuleConfig.get_config('some_key', default='default_value')
# Retrieve a local config value
value = ModuleConfig.get_config('some_key', module_name='module_name', default='default_value')
Setting Configuration Values
You can set configuration values using the set_config method:
# Set a global config value
ModuleConfig.set_config('some_key', 'new_value')
# Set a local config value for a specific module
ModuleConfig.set_config('some_key', 'new_value', module_name='module_name')
Error Handling
- FileNotFoundError: Raised when the specified configuration file does not exist.
- ValueError: Raised when an unsupported configuration format is provided.
Example
Here's a simple example demonstrating how to use ModuleConfig:
# Load global config
ModuleConfig.load_global_config('config/global_config.yaml')
# Load local config for a module
ModuleConfig.load_local_config('module1', 'config/module1_config.json')
# Get configuration values
database_url = ModuleConfig.get_config('database_url')
module_specific_setting = ModuleConfig.get_config('setting_key', module_name='module1', default='default_value')
# Set a configuration value
ModuleConfig.set_config('api_key', 'your_api_key_here')
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file module_config-0.0.1.tar.gz.
File metadata
- Download URL: module_config-0.0.1.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d66165f4cfce018690a976f638e7e7541ae157cd4328aa3c013d8421a07da4c3
|
|
| MD5 |
6befbfc07949a8587c7a44fd5aa67f9a
|
|
| BLAKE2b-256 |
082d4a5ef217fa8b5b93650b52c6a699f57798e411c7f731a6af64fc8e0c5021
|
File details
Details for the file module_config-0.0.1-py3-none-any.whl.
File metadata
- Download URL: module_config-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c3f1e8281f5f41bf9e325a0ffef8cadb1f4153d2d783e0c9a0de65e8e3d1e45
|
|
| MD5 |
652ad34ca7009bfaada02a0a883f5b2a
|
|
| BLAKE2b-256 |
2a6d5bb1dcb1c99699de34cfcc9cdf1f5f2809e7225ef22229d64b0031d5b8a7
|