Skip to main content

Module for reading configuration files

Project description

Overview

This is a module for reading configuration files.

Currently, the module only supports YAML-formatted files.

Features:

  • Retrieve deeply nested values using dot notation, e.g. section1.key1
  • Retrieve values using wildcards, e.g. section1.*.key2
  • Configuration files can be templated (Jinja)

Prerequisites:

  • Python 3.4+

Installation

  • From pypi: pip3 install btconfig
  • From this git repo: pip3 install git+https://github.com/berttejeda/bert.config.git
    Note: To install a specific version of the library from this git repo,
    suffix the git URL in the above command with @{ tag name }, e.g.:
    git+https://github.com/berttejeda/bert.config.git@3.0.0

Usage Examples

Load a configuration file and retrieve specified key value

Given:

  • Config file at /home/myuser/myconfig.yaml
  • with contents:
section1:
  key1: value1
  key2: value2
  key3: value3
from btconfig import Config
# Initialize App Config
config = Config(config_file_uri='~/myconfig.yaml').read()
value = config.get('section1.key')
print(value)

The above should return value1

Load a configuration file and retrieve a deeply nested value

Given:

  • Config file at /home/myuser/myconfig.yaml
  • with contents:
section1:
  subsection1:
    item1:
      subitem1: value1
    item2: value2
    item3: value3
  subsection2:
    item1: value1
    item2: value2
    item3: value3
  key1: value1
  key2: value2
  key3: value3
section2:
  item1: value1
from btconfig import Config
# Initialize App Config
config = Config(config_file_uri='~/myconfig.yaml').read()
value = config.get('section1.subsection1.item2')
print(value)

The above should return value2

Load a configuration file and retrieve specified key value using wildcard notation

Given:

  • Config file at /home/myuser/myconfig.yaml
  • with contents:
section1:
  subsection1:
    item1:
      subitem1: value1
    item2: value2
    item3: value3
  subsection2:
    item1: value1
    item2: value2
    item3: value3
  key1: value1
  key2: value2
  key3: value3
section2:
  item1: value1
from btconfig import Config
# Initialize App Config
config = Config(config_file_uri='~/myconfig.yaml').read()
value = config.get('section1.*.item1')
print(value)

The above should return [{'subitem1': 'value1'}, 'value1']

Note: When retrieving values via wildcard, the return value is a list object.

Load a configuration file as a python object

Same as the above examples, just invoke the Config object with as_object=True, as with config = Config('~/myconfig.yaml', as_object=True).read()

In this case, retrieving values from the object can be done via dot-notation, as with: print(config.section1.subsection1.item2), or via get method, as with print(settings.section1.subsection1.item2)

Note: This approach does not support retrieving values via wildcard reference.

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

btconfig-4.4.4.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

btconfig-4.4.4-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file btconfig-4.4.4.tar.gz.

File metadata

  • Download URL: btconfig-4.4.4.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.9.6 requests/2.28.1 setuptools/65.6.3 requests-toolbelt/0.10.1 tqdm/4.65.0 CPython/3.10.8

File hashes

Hashes for btconfig-4.4.4.tar.gz
Algorithm Hash digest
SHA256 4fe26e725257ef7c57b451fd6198408fdc09c84a155944ea27152aafa7a1da24
MD5 8c5df5d73b78ddb074aa5a5118c3defc
BLAKE2b-256 254b10bd5fd09c1dd238d5af7a8869598f4c50b5211b228903b6186118e035d5

See more details on using hashes here.

File details

Details for the file btconfig-4.4.4-py3-none-any.whl.

File metadata

  • Download URL: btconfig-4.4.4-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.9.6 requests/2.28.1 setuptools/65.6.3 requests-toolbelt/0.10.1 tqdm/4.65.0 CPython/3.10.8

File hashes

Hashes for btconfig-4.4.4-py3-none-any.whl
Algorithm Hash digest
SHA256 b66280a3463d02b030ea39f43001a3559d2c3f969afcb6c3b13e4af461daaff4
MD5 2a331d974c9d5bf427ef77d970b49ff9
BLAKE2b-256 aa78b38dff71704af82218d584fa5bf477a7402b47fe2270e63f9b5638e14b71

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