Code Monkey home page Code Monkey logo

yaconfig's Introduction

yaconfig

Github release PyPI

license MIT

Build Status

Python package to assist configuration of Python software.

Installation

To install the latest release:

pip3 install yaconfig

Usage

This package is intended to be used by other package to assist their configuration. The recommended workflow follows:

  • Add a config module. Its purpose should be to define the configuration variables (the metaconfig) and to provide a single instance of the actual configuration (the values) for your application. If you want to implement a more complex logic to handle multiple configuration, you can do it here. Simple example:
"""File config.py"""
import yaconfig

metaconfig = yaconfig.MetaConfig(
    yaconfig.Variable("text", type=str, default="Hello world", help="Text to output")
)


# Get a default configuration, which should be overridden when the execution starts
config = yaconfig.Config(metaconfig)
  • In the entry point of your program, load the previous module and initialize the config using the desired method. The variables can be accessed from now on. Example:
"""File main.py"""
from config import config  # Just need the config object of the previous module
# Use a relative import instead if intended to run as a package
# from .config import config

try:
    config.load_json("config.json")
except FileNotFoundError:
    print("config.json file not found. Using the default configuration instead.")

print(config["text"])

Note that if that file can be loaded as a module, you should avoid initializing the config. You can use __main__ or build a launching script to prevent this.

  • To document the configuration, you can use the methods of the metaconfig variable you have defined. This can be done manually from the interpreter or automated by writing a script. Some examples follow:
from config import metaconfig # Or wherever the config was placed

metaconfig.generate_json_example()  # Generate a config.example.json file
metaconfig.generate_environment_example()  # Generate a environment.example.sh file
metaconfig.interactive_json()  # Prompt the user to generate a config.json file
metaconfig.interactive_environment()  # Prompt the user to generate a environment.sh file

yaconfig's People

Contributors

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