Git Product home page Git Product logo

real_simple_config's Introduction

Real Simple Config

Because I dislike all the other python config libraries, and it shouldn't have to be hard.

All I wanted was a config system that:

  • Was easy to set up
  • Was easy to mock/test
  • Was easy to get values out of
  • Was easy for end users to write configuration for
  • Was easy to supply defaults to

This config system allows the user to specify configs in the following ways:

  • As an environment variable for each config lineitem
  • As a yaml configuration file specified on the command line
  • As a default yaml filename in their ~/.config directory
  • As default values provided by the application developer

It tries each of these in order until it finds one.

Restrictions: If you want to use environment variables to override settings, keep your yaml files flat and have all values be strings.

Installing

I haven't put it on pypi or even github, but it'll be something like

pip install git+https://github.com/buchuki/real_simple_config.git

Using

To set up configuration, call init_config like so:

    import real_simple
    real_simple.init_config(
        user_config,
        'my_app_default_config.yaml',
        'MYAPP_',
        {
            'FOO': 'def_value',
            'BAR': ''})

Where:

  • user_config is an optional filename the user passed to you on the command line using your preferred command parsing library.
  • my_app_default_config.yaml is the name of a file you want the user to read from in ~/.config if they didn't explicitly pass a config file.
  • MYAPP_ is a prefix for environment variable lookup. Any key in the default dictionary can be looked up from a command-line with export MYAPP_FOO='my_val'.
  • The dictionary is the list of default values for the configuration. Any key not in this dictionary will not be looked up in the user's config files or environment. So set defaults to None if you require the user to look them up.

To look up a configuration value, use:

    from real_simple import conf
    val = conf('FOO')

Testing

You can mock out real_simple.init_config or real_simple.CONFIGURATION depending on your needs. The former would be mocked if your test code is called before your app has called init_config. The latter would be mocked if you want to specify mocked config values after init_config has been called. The latter is the most likely case.

real_simple_config's People

Watchers

Dusty Phillips avatar James Cloos 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.