Skip to main content

A config system that is lazily validated as parameters are used

Project description

lazy_config

A config system that is lazily validated as parameters are used.

Installation

pip install lazy_config

Usage

Creating a config

from lazy_config import LazyConfig

# From a YAML file (OmegaConf interpolations supported)
config = LazyConfig.from_yaml("path/to/config.yaml")

# From a JSON file
config = LazyConfig.from_json("path/to/config.json")

# From a plain Python dict
config = LazyConfig.from_dict({"num_epochs": 10, "learning_rate": 0.001})

Reading values

get(key) with no default returns a LazyConfig sub-config (real or ghost). get(key, default) returns the leaf value (type inferred from the default).

# Given config.yaml:
# model:
#   layers: 4
#   block: resnet
# dataset:
#   path: my/data/
#   augmentations: [crop]

model_config = config.get("model")    # LazyConfig
layers = model_config.get("layers", 1)  # 4

# Accessing a key that doesn't exist in the YAML is fine at this point —
# a "ghost" LazyConfig is returned and the access is recorded.
training_config = config.get("training")
learning_rate = training_config.get("learning_rate", 0.001)  # returns 0.001

Validating at setup time

Call check() once all parameters have been read. It raises MissingConfigError with a suggestion showing exactly what to add to your config file.

config.check()
# MissingConfigError:
# The following parameters were used but missing from the config.
# Since this started from a yaml (path/to/config.yaml), you should add:
#
# training:
#   learning_rate: 0.001

If multiple keys are missing they are merged into a single suggestion block. The format matches the source: YAML for from_yaml, JSON for from_json, and a Python dict literal for from_dict.

Development

Setup

pip install -e ".[dev]"

Or install with test dependencies:

pip install pytest
pip install -e .

Running Tests

pytest

To run with verbose output:

pytest -v

Building and Publishing to PyPI

Prerequisites

pip install build twine

Build the distribution

python -m build

This creates a dist/ directory containing a .whl and .tar.gz file.

Upload to PyPI

First, upload to TestPyPI to verify everything looks correct:

twine upload --repository testpypi dist/*

When ready, upload to the real PyPI:

twine upload dist/*

You will be prompted for your PyPI credentials. It is recommended to use an API token instead of your password.

To avoid entering credentials each time, create a ~/.pypirc file:

[pypi]
  username = __token__
  password = pypi-your-api-token-here

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

ghostconfig-0.2.0.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

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

ghostconfig-0.2.0-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ghostconfig-0.2.0.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for ghostconfig-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a77e83523d53404bcea84aca8d9467279e1c062f50a1113fd30294121a4067fc
MD5 9add1af23519460affe0cc3b3ae4588e
BLAKE2b-256 5429089c781d7b59ed6faf7db1eb2240cd9625a50c62a8c7808247a189fda8dc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ghostconfig-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for ghostconfig-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b31f63bb5dc47e674f9413c775b9f2c5cbdf6dd1fc9a52e07b1b494aacfbcbc4
MD5 00c0646f0ed5cf35af8c7f5e2fa82eab
BLAKE2b-256 91851bc33c919c101ed37e25c501eb9a32b90946a2a3c98a0112c34a748448a5

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