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

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.2.tar.gz (6.4 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.2-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

Hashes for ccfg-0.0.2.tar.gz
Algorithm Hash digest
SHA256 d14ff30f8688e0725dbdd17a89c9fe9add5f33fb1978f6308e44c2fe6325dd81
MD5 e3f503ccca7db597b508cf15d6254f76
BLAKE2b-256 a3c10ce553eb0843f4b54c280df66d613cfda4619f0e01161eaaa3c4883fd26c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ccfg-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.3 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 575ef913f1e061214c2cbd9f23a90c331dfe641ac89747ab7598f5f0a7f116ca
MD5 279ae7062beca20325f46ecbd0b7220c
BLAKE2b-256 dbd10320bcd4267c29f2924e4cc298b933b151b6b3426395dcff67dbeca95532

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