Skip to main content

The Strangely Familiar Config Parser

Reason this release was yanked:

Deprecated API

Project description

konfik-logo

>> The Strangely Familiar Config Parser <<

Codecov Code style: black MIT license

📖 Description

Konfik is a simple configuration parser that helps you access your TOML or DOTENV config variables using dot (.) notation. This enables you to do this:

foo_bar_bazz = config.FOO.BAR.BAZZ

instead of this:

foo_bar_bazz = config["FOO"]["BAR"]["BAZZ"]

⚙️ Installation

Install Konfik via pip:

pip install konfik

💡 Examples

Let's see how you can parse a TOML config file and access the variables there. For demonstration, we'll be using the following config.toml file:

# example_config.toml

title = "TOML Example"

[owner]
name = "Tom Preston-Werner"
dob = 1979-05-27T07:32:00-08:00 # First class dates

[servers]
  [servers.alpha]
  ip = "10.0.0.1"
  dc = "eqdc10"

  [servers.beta]
  ip = "10.0.0.2"
  dc = "eqdc10"

[clients]
data = [ ["gamma", "delta"], [1, 2] ]

To parse this in Python:

from konfik import Konfik

# Define the config path
CONFIG_PATH_TOML = "config.toml"

# Initialize the konfik class
konfik = Konfik(config_path=CONFIG_PATH_TOML)

# Serialize and print the confile file
konfik.serialize()

# Get the configuration dictionary from the konfik class
config_toml = konfik.config

# Access and print the variables
title = config_toml.title
owner = config_toml.owner
dob = config_toml.owner.dob
database = config_toml.database.ports
server_ip = config_toml.servers.alpha.ip
clients = config_toml.clients

The .serialize() method will print your entire config file as a colorized Python dictionary object like this:

{
    'title': 'TOML Example',
    'owner': {
        'name': 'Tom Preston-Werner',
        'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=<toml.tz.TomlTz object at
0x7f2dfca308b0>)
    },
    'database': {
        'server': '192.168.1.1',
        'ports': [8001, 8001, 8002],
        'connection_max': 5000,
        'enabled': True
    },
    'servers': {
        'alpha': {'ip': '10.0.0.1', 'dc': 'eqdc10'},
        'beta': {'ip': '10.0.0.2', 'dc': 'eqdc10'}
    },
    'clients': {'data': [['gamma', 'delta'], [1, 2]]}
}

Konfik also exposes a few command-line options for you to introspect your config file and variables. Run:

konfik --help

This will reveal the options associated with the CLI tool:

usage: konfik [-h] [--show SHOW] [--path PATH] [--serialize] [--version]

Konfik CLI

optional arguments:
  -h, --help   show this help message and exit
  --show SHOW  show variables from config file
  --path PATH  add custom config file path
  --serialize  print the serialized config file
  --version    print konfik-cli version number

To inspect the value of a specific variable in a config.toml file you can run:

konfik --show servers.alpha.ip

If you're using a config that's not named as config.toml then you can deliver the path using the --path argument like this:

konfik --path settings/example_config.env --show name

🙋 Why

While working with machine learning models, I wanted an easier way to tune the model parameters without mutating the Python files. I needed something that would simply enable me to access tuple or dictionary data structures from a config file. I couldn't find anything that doesn't try to do a gazillion of other kinds of stuff or doesn't come with the overhead of a significant learning curve.

Neither DOTENV nor YAML catered to my need as I was after something that gives me the ability to store complex data structures without a lot of fuss -- so TOML it is. However,Konfik also supports DOTENV and JSON. Also, not having to write angle brackets ([""]) to access dictionary values is nice!

🎉 Contribution Guidelines

Currently, Konfik doesn't support .yaml out of the box. Maybe that's something you'd like to take a jab at. To do so,

  • Clone the repo

  • Spin up and activate your virtual environment. You can use anything between Python 3.6 to Python 3.9.

  • Install poetry

  • Install the dependencies via:

    poetry install
    
  • Make your changes to the konfik/main.py file

  • Run the tests via the following command. Make sure you've Python 3.6 - Python 3.9 installed, otherwise tox would throw an error.

    tox
    
  • Write a simple unit test for your change

  • Run the linter via:

    make linter
    

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

konfik-1.0.0.tar.gz (6.9 kB view details)

Uploaded Source

Built Distribution

konfik-1.0.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file konfik-1.0.0.tar.gz.

File metadata

  • Download URL: konfik-1.0.0.tar.gz
  • Upload date:
  • Size: 6.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.1 CPython/3.8.5 Linux/5.4.0-7642-generic

File hashes

Hashes for konfik-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5d7b023296c8e811ff12304d02d562c1b932697613999dd86a9fd9eac35cdb2e
MD5 72fa1f4b522184af32d16aeb2d7f6d8b
BLAKE2b-256 de0b037605ea7e67f195fd0950df8984c0874fcf7befbfe90d760bce78f1e96c

See more details on using hashes here.

File details

Details for the file konfik-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: konfik-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.1 CPython/3.8.5 Linux/5.4.0-7642-generic

File hashes

Hashes for konfik-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 91aec4124e6faee327961ad24fe564c0dc91e92c6944f013f87aa15112d62633
MD5 71d418cd7fd94be431388ed75dc6e53a
BLAKE2b-256 8c4a742139883cd9cb888f24521a69c0bcbb326f9f1ad670e1906a5415cfded7

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