Skip to main content

Type-safe configuration management with support for nested dataclasses

Project description

NestConfig

NestConfig is a flexible configuration management system designed to simplify the handling of configurations in Python applications. It leverages Python's type hints and dataclasses to offer type-safe configuration updates, including support for nested configurations and direct application of command-line options.

This class is similar to fvcore.common.config.CfgNode, but it offers the added benefit of AutoCompletion.

Features

  • Type-safe configuration management using dataclasses.
  • Support for nested configurations.
  • Easy integration of command-line options into the configuration.
  • Simple and intuitive usage.

Installation

To install NestConfig, simply use pip:

pip install nestconfig

It can also be built from source with pip install . from this repository.

How to use

Minimal Example

from dataclasses import dataclass
from nestconfig import NestConfig

@dataclass
class ModelConfig:
    ENC_NUM_HEADS: int = 8

@dataclass
class Config(NestConfig):
    MODEL: ModelConfig = ModelConfig()
    SOME_OTHER_CONFIG: int = 10

# Instantiate your configuration
config = Config()

# Update the configuration with new values.
# You can also use a yaml file: updates = yaml.safe_load(yaml_file)
updates = {'MODEL': {'ENC_NUM_HEADS': 12}, 'SOME_OTHER_CONFIG': 15}
config.merge_updates(updates)
print(config.MODEL.ENC_NUM_HEADS)  # Outputs: 12
print(config.SOME_OTHER_CONFIG)  # Outputs: 15

# Apply command-line options directly.
# You can use argparse here and set opts to args.opts
opts = ['MODEL.ENC_NUM_HEADS', '16', 'SOME_OTHER_CONFIG', '20']
config.merge_opts(opts)
print(config.MODEL.ENC_NUM_HEADS)  # Outputs: 16
print(config.SOME_OTHER_CONFIG)  # Outputs: 20

More examples

More examples can be found in test folder.

Blog post

A blog post can be found here.

If you find this code useful, please consider giving it a star (☆) :).

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

nestconfig-0.2.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

nestconfig-0.2.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file nestconfig-0.2.0.tar.gz.

File metadata

  • Download URL: nestconfig-0.2.0.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for nestconfig-0.2.0.tar.gz
Algorithm Hash digest
SHA256 01c785cc280fb38c345667e91c48e7478e092f61468e5ef53ad4af708e65e882
MD5 6c5da382eb2c61b071885caca740518c
BLAKE2b-256 28045d4ce72dbc8b98495aff06415dfedf974b76ce8302504ff09d387568c400

See more details on using hashes here.

File details

Details for the file nestconfig-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: nestconfig-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for nestconfig-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fef9dacee65c612fc3647728608803c61eada96e16e436d4e34f03949fb8776d
MD5 8279c61bd7c377143252ec69d91b409d
BLAKE2b-256 da6e2649b266e2189c4e357aee3efe200381db70d197e4eddd479a01754ca407

See more details on using hashes here.

Supported by

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