A Python library for managing hierarchical configurations using a class-based approach.
Project description
ccfg
A Python library for managing hierarchical configurations using a class-based approach.
Features
- Define configuration hierarchies using Python classes
- Access configuration hierarchically using dot notation, enabling IDE autocompletion
- Access to non-existent paths returns None instead of raising errors
- Automatically convert between configuration classes and dictionaries
- Support for multiple serialization formats (JSON, TOML, YAML)
- Simple file-based storage and retrieval
Installation
pip install ccfg
Optional Dependencies
For additional serialization format support:
- For TOML support:
pip install toml - For YAML support:
pip install pyyaml
Usage Example
Below is a comprehensive example demonstrating the main features:
from ccfg import CCFG
class ApplicationConfig(CCFG):
# Custom path for file storage
path = "configs/app_settings.json"
# No explicit name, will use class name "ApplicationConfig" as default
class Database:
# Custom name for this section
name = "DatabaseSettings"
class Connection:
# No explicit name, will use "Connection" by default
value = "postgresql://user:password@localhost:5432/mydb"
class Pooling:
name = "ConnectionPool"
value = {"max_connections": 10, "timeout": 30}
class Logging:
# Using default name "Logging"
class Level:
value = "INFO"
class Output:
name = "OutputPath"
value = "/var/log/app.log"
class Features:
# Using a list for complex values
value = ["authentication", "api", "admin_panel"]
# Convert to dictionary
config_dict = ApplicationConfig.to_dict()
print(config_dict)
# Output:
# {
# "ApplicationConfig": {
# "DatabaseSettings": {
# "Connection": "postgresql://user:password@localhost:5432/mydb",
# "ConnectionPool": {"max_connections": 10, "timeout": 30}
# },
# "Logging": {
# "Level": "INFO",
# "OutputPath": "/var/log/app.log"
# },
# "Features": ["authentication", "api", "admin_panel"]
# }
# }
# Save to file using the configured path
ApplicationConfig.dump() # Saves to configs/app_settings.json
# Save to a different file in YAML format
ApplicationConfig.dump(form="yaml", path="configs/app_config.yaml")
# Load configuration
ApplicationConfig.load() # Loads from configs/app_settings.json
# Access configuration values
db_connection = ApplicationConfig.Database.Connection.value
log_level = ApplicationConfig.Logging.Level.value
features = ApplicationConfig.Features.value
print(f"Database connection: {db_connection}")
print(f"Log level: {log_level}")
print(f"Enabled features: {', '.join(features)}")
Supported Serialization Formats
- JSON (default)
- TOML
- YAML
License
MIT
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
ccfg-0.0.3.tar.gz
(6.4 kB
view details)
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
ccfg-0.0.3-py3-none-any.whl
(5.4 kB
view details)
File details
Details for the file ccfg-0.0.3.tar.gz.
File metadata
- Download URL: ccfg-0.0.3.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ea26ae13bb57d36a37bb0cd333c8200afc731ab3682c56c663400ce78f1ece2
|
|
| MD5 |
fbb39c2a70917f57c21df097c28ddf69
|
|
| BLAKE2b-256 |
a3880b68fa28096e0696e96ea45e798a46db084ba2d2ccb50ee457a2de613bf4
|
File details
Details for the file ccfg-0.0.3-py3-none-any.whl.
File metadata
- Download URL: ccfg-0.0.3-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7ca0d4539d93d7c36be975792200db57ea28cd436a610adeac4849ed0d8397e3
|
|
| MD5 |
1cb9723ba9816a28226980b37520e71e
|
|
| BLAKE2b-256 |
849971b17467e5b11c95cdda9b80518c04042bbbf9117d2b6a7ca31c9941caf6
|