Git Product home page Git Product logo

pytailor's Introduction

pytailor

Build Status Code style: black codecov Total alerts Language grade: Python GitHub license

Set up configuration for your application or library with ease.

Features

pytailor gives you a few key features that make your life easy when creating libraries or applications:

  • Easily create configuration following the config.py pattern
  • Use environment variables in your configuration and change them dynamically at runtime
  • Use a .env file and follow the twelve-factor app methodology

Example

Get up and running with full configuration quickly:

from pytailor import Tailor

class Config(object):
    DEBUG = False

config = Tailor()
# load from a simple object
config.from_object(Config)
# add and watch environment variables
config.from_envar("DEBUG")
# use a dotenv file
config.from_dotenv("/path/to/.env/")
# config works like a dict!
config["DEBUG"] # True

Installation

Installation is easy using pip:

pip install pytailor

Advanced Usage

Usage with pipenv

If your using a dev workflow tool like Pipenv, you can easily pair pytailor with it. Pipenv will automatically load .env files for you out of the box. To track variables loaded from a .env file when using Pipenv, simply use this pattern:

# .env file
DEBUG=true
from pytailor import Tailor
config = Tailor()
# watch each variable loaded by Pipenv
config.from_envar("DEBUG")
config["DEBUG"]  # True

Usage with python-dotenv

python-dotenv is a useful tool for managing .env files in multiple environments. As with the previous example, it's easy to pair with pytailor.

# .env file
DEBUG=true
from dotenv import load_dotenv
from pytailor import Tailor

load_dotenv()
config = Tailor()
# watch each variable loaded by python-dotenv
config.from_envar("DEBUG")
config["DEBUG"]  # True

How to contribute

  1. Check for open issues or open a fresh issue to start a discussion around a feature idea or a bug.

  2. Fork the repository on GitHub to start making your changes to the master branch (or branch off of it).

  3. Write a test which shows that the bug was fixed or that the feature works as expected. Make sure to use pre-commit too!

  4. Send a pull request and bug the maintainer until it gets merged and published. Make sure to add yourself to AUTHORS.

pytailor's People

Contributors

bradleygolden avatar dependabot[bot] avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Forkers

mirror-dump

pytailor's Issues

Rename watch_env_var

watch_env_var is confusingly named. It should be called something like "from_env_var"

For example

config.watch_env_var("FOO")

could be something like:

config.from_environ("FOO")
# or
config.from_env_var("FOO")
# or
config.from_envar("FOO")

or perhaps we can get more creative with something like:

config.environ["FOO"]
config.environ.get("FOO")

Where environ is a dictionary like object that takes brackets or the .get() method.

Allow default env var values

You should be able to specify a default value in the event an environment variable doesn't exist.

For example:

config.from_envar("FOO")

Could be:

config.from_envar("FOO", default="BAR")

Loading multiple env vars

The user should be able to load multiple env vars at a single time rather than one at a time.

Currently we have to do the following:

config = Tailor()
config.from_envar("FOO")
config.from_envar("BAZ")
config.from_envar("BAR")

It could look something like this:

config = Tailor()
config.from_envars(["FOO", "BAR", "BAZ"])

Allow renaming of env vars

When loading env vars, you should be able to rename them with a different alias. This is because some applications prefer to have custom env vars in the system so they don't clash with other environment vars that preexisted prior to the application.

Suggestion:

# export DEBUG=true
config.watch_env_var("APPNAME_DEBUG", name="DEBUG")
assert config["DEBUG"] == True
assert APPNAME_DEBUG not in config

KeyError in watch_env_var

value = os.environ[name]

This line produces an error if the user does not have an environment variable set already set. It should look for the default value first and then throw an error if the environment variable doesn't exist.

i.e.

config.watch_env_var("SOME_VAR")

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.