Skip to main content

Python package for configuring a python package

Project description

https://travis-ci.org/pytroll/donfig.svg?branch=master https://ci.appveyor.com/api/projects/status/mx2xytb89voslo6h/branch/master?svg=true https://coveralls.io/repos/github/pytroll/donfig/badge.svg?branch=master

Donfig is a python library meant to make configuration easier for other python packages. Donfig can be configured programmatically, by environment variables, or from YAML files in standard locations. The below examples show the basics of using donfig. For more details see the official documentation.

Installation

Donfig can be installed from PyPI using pip:

pip install donfig

Or with conda using the conda-forge channel:

conda install -c conda-forge donfig

Using Donfig

Create the package-wide configuration object for your package named mypkg:

# mypkg/__init__.py
from donfig import Config
config = Config('mypkg')

Use the configuration object:

from mypkg import config
important_val = config.get('important_key')
if important_val:
    # do something
else:
    # something else

Set configuration in Python

Configuration can be modified in python before code using it is called:

# mypkg/work.py
from mypkg import config
config.set(important_key=5)

# use the configuration

Donfig configurations can also be changed as a context manager:

config.set(other_key=True)

with config.set(other_key=False):
    print(config.get('other_key'))  # False

print(config.get('other_key'))  # True

Configure from environment variables

Environment variables are automatically loaded when the Config object is created. Any environment variable starting with the name of the config object in all capital letters and an underscore will be loaded in to the config object:

export MYPKG_MY_KEY="a value"

And can be accessed in python:

from mypkg import config
print(config.get('my_key'))

Configure from YAML file

Donfig will also automatically load any YAML configuration files found in specific paths. The default paths:

  • ~/.config/<config name>/

  • /etc/<config name>/

  • <sys.prefix>/etc/<config name>/

Note the /etc/<config name>/ directory can also be specified with the environment variable DASK_ROOT_CONFIG. Also note that ~/.config/<package name> (or other location specified with DASK_CONFIG) can be created as a custom user configuration file for easier user customization (see documentation for details).

History

Donfig is based on the original configuration logic of the dask library. The code has been modified to use a config object instead of a global configuration dictionary. This makes the configuration logic of dask available to everyone. The name “donfig” is a shortening of “dask.config”, the original dask module that implemented this functionality.

License

Original code from the dask library was distributed under the license specified in DASK_LICENSE.txt. In November 2018 this code was migrated to the Donfig project under the MIT license described in LICENSE.txt. The full copyright for this project is therefore:

Copyright (c) 2018 Donfig Developers
Copyright (c) 2014-2018, Anaconda, Inc. and contributors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

donfig-0.2.0.tar.gz (29.1 kB view details)

Uploaded Source

File details

Details for the file donfig-0.2.0.tar.gz.

File metadata

  • Download URL: donfig-0.2.0.tar.gz
  • Upload date:
  • Size: 29.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.21.0 setuptools/40.6.3 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/2.7.15

File hashes

Hashes for donfig-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0c586c607ff53792f9cd1dc5275ccc154cfd734b415c385c2a0436f17ce4e924
MD5 35aa2d8bc9df4538754f1b3b1df00871
BLAKE2b-256 551e7aa3cd78dc183d348892dfe315a03c77013d968a7e0d57938b3fd9799687

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page