Code Monkey home page Code Monkey logo

mdtc's Introduction

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..

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.