Skip to main content

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.4.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ccfg-0.0.4-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file ccfg-0.0.4.tar.gz.

File metadata

  • Download URL: ccfg-0.0.4.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.4

File hashes

Hashes for ccfg-0.0.4.tar.gz
Algorithm Hash digest
SHA256 0691e291de850e227a17d4bd44e384870c5cb509b9a4a8aec197e036c6e1db45
MD5 8bc6fdf7f8c549f8dcf6774d89d2b8f7
BLAKE2b-256 73fd186004b439cfc78d4c8cadb5ad705c2b9999e4e7e648da7cb2de9e073f66

See more details on using hashes here.

File details

Details for the file ccfg-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: ccfg-0.0.4-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

Hashes for ccfg-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 00869d7df81728697bbfa76fc66d1e2ab12d3d4532b75ca88865dc918feacc92
MD5 88c911b72c66133a37120a861655ebb9
BLAKE2b-256 71ef318d23f5c96b94363a9a6ed1ce734cf8ac3f2e4271920422abbf639f9190

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page