Skip to main content

A model-driven configuration object for TOML or dict-based configs.

Project description

Readme

MDTC - Model-driven TOML Configuration.

A lightweight config singleton meant for storing your application's config state no matter where or how many times it is instantiated. You can pass this object around across your entire app and not worry about config mutations, unvalidated config values or lack of IDE completions. Originally meant for use with TOML key/value-based configs, but any k/v object should work as long as it complies with the model.

The source documentation can be found here

What is MDTC for?

  • Avoids having to use or chain .get() or retrieve config values via cfg["foo"]["bar"]["baz"].
  • Code-completion-friendly via model-driven approach.
  • Custom configuration validation (either via Pydantic's interfaces or custom-built validators you define).
  • Immutable config state support. The config itself is immutable by default - you cannot replace config.foo with another value, for instance.
  • Supports nicer type hints instead of a huge TypeDict or another approach for a config dictionary loaded into Python.

What MDTC is not for

  • It is not meant to replace other methods of loading TOML or dict configs, it simply provides an alternative for housing your TOML config values.
  • It is not meant as "less code". The guarantees it provides require a different implementation approach, and won't always result in less upfront code.
  • Codebases using other approaches or small configs won't benefit from this approach as much.

Dependencies

None, just the Python standard library.

Examples

Simple Configuration

import tomllib # python3.11-only, use tomli for <=3.10

from dataclasses import dataclass
from mdtc import Config

@dataclass
class FooCfg:
    foo: str
    bar: str

    _name: str = "misc"
    _key: str = "config.misc"


class MyConf(Config):
    misc: FooCfg

cfg = """
[config.misc]
foo="bar"
bar="baz"
"""

toml = tomllib.loads(cfg)

config = MyConf(toml)

Pydantic Models in your Configuration

import tomllib # python3.11-only, use tomli for <=3.10

from pydantic import BaseModel
from mdtc import Config


class FooCfg(BaseModel):
    _name: str = "misc"
    _key: str = "config.misc"
    
    foo: str
    bar: str


class MyConf(Config):
    misc: FooCfg


cfg = """
[config.misc]
foo="bar"
bar="baz"
"""

toml = tomllib.loads(cfg)

config = MyConf(toml)

Pydantic dataclass Example

import tomllib # python3.11-only, use tomli for <=3.10

from pydantic import Field, validator
from pydantic.dataclasses import dataclass

from mdtc import Config


@dataclass
class FooCfg:
    foo: str
    bar: str = Field(title="A bar to get drinks in..")

    _name: str = "misc"
    _key: str = "config.misc"

    @validator("foo")
    def name_must_contain_space(cls, v):
        if " " in v:
            raise ValueError("must NOT contain a space!")
        return v.title()


class MyConf(Config):
    misc: FooCfg


cfg = """
[config.misc]
foo="bar"
bar="baz"
"""

toml = tomllib.loads(cfg)

config = MyConf(toml)

Contributing

Coming soon..

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

mdtc-0.1.4.tar.gz (9.5 kB view details)

Uploaded Source

Built Distribution

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

mdtc-0.1.4-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file mdtc-0.1.4.tar.gz.

File metadata

  • Download URL: mdtc-0.1.4.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.6 Linux/5.15.0-1034-azure

File hashes

Hashes for mdtc-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a0b78a906b4d0ba91259745b5fd09de033e66cbaf5dae064e9fc604f56827e30
MD5 90c41978c622cad2c77ec2eb5baf0309
BLAKE2b-256 f7012af1af3b192a5a842f938ceecb6f26d446ae18bd6f364750127073acdd49

See more details on using hashes here.

File details

Details for the file mdtc-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: mdtc-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.1 CPython/3.10.6 Linux/5.15.0-1034-azure

File hashes

Hashes for mdtc-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ebb6767cbbf43bcafa3c911555f32a7c1fa135e58f9c3f04a90736fa42aa4484
MD5 824cc9733b0d1264114aef9b11cfcab7
BLAKE2b-256 ef229b01ab773fc861e8f3079c30fc1b9bf716f66e612c5c72976af8c51a05d4

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