GithubHelp home page GithubHelp logo

lco's Introduction

Light Config (lco)

Load a config file once and use it as you would use enviroment variables from anywhere in your code.

Somewhere:

import lco

lco.init("some_config.yml")

You can also set the enviroment variable LCO_CONFIG instead.

Somewhere else entirely, 10 layers deep in some object.

import lco

lco["some_value"]
lco["some_group"]["some_value"]

This works across threads and processes.

Installation

You can use pip install lco.

But this is such a small package, it might be best to just copy the code below into your own lco.py.

Show Code
import os
import sys
import yaml

class LCO(object):
  def __init__(self):
    global _lco_obj
    if "_lco_obj" not in globals():
      if "LCO_CONFIG" in os.environ:
        config = os.environ["LCO_CONFIG"]
        if os.path.exists(config):
          with open(config, "r") as f:
            _lco_obj = yaml.safe_load(f)
        else:
          raise ValueError("LCO_CONFIG is not a file: %s" % config)

  def init(self, config):
    global _lco_obj
    if "_lco_obj" not in globals():
      with open(config, "r") as f:
        _lco_obj = yaml.safe_load(f)
      os.environ["LCO_CONFIG"] = config

  def __getitem__(self, name):
    global _lco_obj
    if name not in _lco_obj:
      raise KeyError("No such key: %s" % name)
    return _lco_obj[name]

sys.modules["lco"] = LCO()

But why?

This is mostly useful for research code, MVPs or Prototypes, where one doesn't want to pass configuration variables from object to object or across scopes. I wouldn't recommend this for production code as you're essentially using global variables.

What filetypes are supported?

So far, only yaml. Feel free to open a pull request with the filetype of your choice.

lco's People

Contributors

minixc avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

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