Giter Site home page Giter Site logo

confboy's Introduction

Confboy

Better configs with TOML support. Copy of https://github.com/Euromance/confboy package

Installation

Use the package manager pip to install confboy.

pip install confboy1

Usage

import confboy1 as confboy


base_config = {
    'nested': {
        'one': 1,
        'two': 2,
    },
    'three': 3,
}

config = confboy.Config(base_config)

config.nested.one  # Returns `1`
config.three       # Returns `3`

Although it is advised not to change the config during runtime, it is also possible:

config.nested.one = 10
config.nested.one  # Returns `10`

config.new_nested = {'one': 1}
config.new_nested.one  # Returns `10`

Confboy also supports dynamic values that can use other values from the same config. You can use it to build connection URLs or whatever. Shines with changing config during runtime: change config.postgres.user and config.postgres.url will be rebuilt on every query if it's a callable. You can do various things with it including mapping and filtering values the app got from the TOML config, cast types, whatever comes to your mind. Callables work on any nested level.

config = confboy.Config({
    'a': {'a': 1},
    'b': 2,
    'a_plus_b': 'callable:add',  # Tell confboy to call `add` from provided callables
})


def add():
    return config.a.a + config.b


config.register_callable(add)
config.a_plus_b  # Returns 3

Confboy can take values from TOML files as well. Provided config will be merged over base config.

# config.toml

[nested]
one = 1
two = 2

not = "nested"
config = confboy.Config(toml_config_path='config.toml')
config.nested.one  # Returns 1
config.not         # Returns 'nested'

Confboy's merges are soft: if the value is present in the config but not in patch, it won't be deleted from the config. It supports value deletion and everything. Just try it out or check the source code!

Async functions as callables

Currently confboy does not support asynchronous functions as callables. That would bring interface inconsistency like await config.foo vs config.foo. You can wrap your asynchronous function like that:

config.register_callable(
    functools.partial(asyncio.run, async_func()))

Although that'd block the event loop.

confboy's People

Contributors

ulfr avatar

Watchers

 avatar

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.