Coder Social home page Coder Social logo

pjconf's Introduction

pjconf

A simple configuration interface to use JSON files as configuration files.

Installation

PJC is installed via pip.

pip install pjconf

Usage

Loading configuration is straightforward, just import and point to your json file.

import pjconf as pjc

# Load a json file as a configuration dictionary.
config = pjc.load_config('config.json')

# or, load a configuration file with an optional default set of configuration elsewhere
config = pjc.load_config('defaults.json', 'config.json', ignore_missing=True)

Configuration files are loaded in a cascading sequence, multiple positional filepaths are loaded and override/update the previous keys. Setting ignore_missing=True will allow for a flexible resolve of optional configuration; disabling it will raise a FileNotFoundError on the first file it can't find.

Accessing configuration uses a get() method, with a cool dot-recursive syntax, optional defaults and runtime type-casting.

Let's assume the following configuration file.

{
    "opt1": true
    "opt2": 0.1
    "opt3": {
        "subopt1": "hello"
    } 
}

You can use the following syntax to access your configuration.

# Access a simple option
opt1 = config.get('opt1')
# True

# Access a recursive (nested) option
subopt1 = config.get('opt3.subopt1')
# "hello"

# Try to access a missing option, but provide a default
missing_opt = config.get('missing', default=True)
# True

# Runtime type-casting
opt2asint = config.get('opt2', cast=int)
# 0

For the last example, the cast argument takes a callable with a single parameter - you can be as simple or as complicated (i.e. lambdas or defined functions) as you like.

pjconf's People

Contributors

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