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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file btconfig-4.4.2.tar.gz
.
File metadata
- Download URL: btconfig-4.4.2.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c7e6b896b1d6b3432dadf7335f690c03a8011c91a4d43047928b827fe481913 |
|
MD5 | 1146c2f65a093923c7207fcc5cadb377 |
|
BLAKE2b-256 | e6bbc9a5c2468e75dce80d03d78246c537131a42c7231afa3c72b1b0f00e7bc0 |
File details
Details for the file btconfig-4.4.2-py3-none-any.whl
.
File metadata
- Download URL: btconfig-4.4.2-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9baa585b5afb3b544496573764eb3ed39d487ecc749cbb6cbea02e46e41106f9 |
|
MD5 | d3de16f88da30dc45ca12a69b164fb23 |
|
BLAKE2b-256 | 2b7375312766cd36ae66d9b3bee2e032aa9d23d41abde599947cbe72caf6f550 |