Code Monkey home page Code Monkey logo

constload's Introduction

constload

Tests

constload is a package designed to simplify the process of loading settings into Python applications by providing certain functionality that is frequently reimplemented from project to project. This includes the ability to set defaults and set certain parameters as required.

Usage examples

When loading settings, you have a couple of options. You can provide pre-parsed data in a dictionary, or you have have data parsed for you from a filepath or from a string.

from constload import ConstantLoader

# Loading JSON from file
c = ConstantLoader("/path/to/settings/file.json")

# Loading JSON from string
c = ConstantLoader("{'hello': 'world'}")

# Using preloaded dict
c = ConstantLoader(your_dict)

It's also trivial to write your own loaders.

def my_loader(data):
    # Basic CSV loader
    my_dict = {}
    lines = data.split("\n")
    for line in lines:
        vals = line.split(",")
        my_dict[vals[0]] = vals[1:]
    return my_dict

c = ConstantLoader("/path/to/file.csv", loader=my_loader)

When you've got your data loaded, you can begin to extract values.

This is done by using tuples that contain paths to your values.

# ("user", "name") is equivalent to ["user"]["name"]
USER_NAME = c.required(("user", "name"))
USER_PETS = c.default(("user", "pets"), [])

Any changes made by c.default are automatically mirrored to the dictionary as they're being made. This means you can work directly with the dictionary, if you so choose.

c.default(("user", "pets"), [])
c.data["user", "name"]  # -> []

Installing

  • With YAML support - pip install constload[yaml]
  • Otherwise - pip install constload

Licence

Licenced under the Mozilla Public Licence 2.0. Licence text.

Support

If you need help, feel free to open an issue.

Contributing

Issues/pull requests are welcome. :)

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.