Skip to main content

A library representing YAML config files as object.

Project description

yaconfigobject

Build Status codecov Documentation Status

Yet Anoter Configuration Object, this time using YAML.

A Python package that provides application wide configuration as an object, where all configuration items are accessible as attributes directly:

from yaconfigobject import Config

CONFIG = Config()

property = CONFIG.topic.subtopic

Configurations are stored in a YAML file inside the module/config directory of the application. A file called config.yaml existing in the applications current working directory will take precedence over the packaged file (in the config directory).

Use in a module

To make sure every piece of code has access to the same configured information, it is recommended to instantiate a Config object in the modules __init__.py file. The Config object needs to be present before any other imports take place.

An example __init__.py:

from yaconfigobject import Config

CONFIG = Config('module.yaml')

from .module_main import *

Advanced usage

Several options might help you adapting the Config class to your needs.

Defining search paths

To tell yaconfigobject.Config() where to search for config files, the paths keyword argument can be specified as a list:

CONFIG = Config(paths=['~', '~/.local/share/my_app'])

Using custom config file names

For applications where different parts use a separate configuration, it is highly recommended to use a custom file name:

CONFIG = Config(name='my_app.yaml')

Adding values in code

Of course you can also specify config values through code:

from yaconfigobject import Config

static_config = Config(database='sqlite:///:memory:')

CONFIG = Config(name='my_app.yaml')
CONFIG.update(static_config)

Loading config values from environment variables

yaconfigobject will try to load config values from environment variables prefixed with the uppercase module or application name when initialising a Config object.

CONFIG = Config(name='my_app.yaml')

All environment variables starting with MY_APP will be considered a config value. Config items from the environment will always take precedence.

NOTE: When using environment variables, config item identifiers cannot contain underscores(_)! The app / module name is the only exception.

======= History

1.2.3 (2020-04-28)

  • First release on PyPI.

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

yaconfigobject-1.2.3.tar.gz (14.0 kB view details)

Uploaded Source

File details

Details for the file yaconfigobject-1.2.3.tar.gz.

File metadata

  • Download URL: yaconfigobject-1.2.3.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.1

File hashes

Hashes for yaconfigobject-1.2.3.tar.gz
Algorithm Hash digest
SHA256 764d9d3daa33af9bf35a2c366484b012c9566f124192994ce7bfae7595f3ae0e
MD5 51c6f84f3b885315e47d7e0812c926d8
BLAKE2b-256 d8697602a6e0a450768cf9ead393cdec62b55adb040087621c85fa12989f16e9

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