Skip to main content

Very opinionated configuration loading package for Python projects

Project description

Confuk

This is yet another package for managing configuration files in Python projects.

It exposes one function that lets you provide a path to a TOML/YAML/JSON configuration file. It parses the config file into a dictionary by default. If a config class is provided when parsing, the class instance will be created using a dictionary of keyword arguments coming from the original TOML/YAML/JSON file.

In human words: I made this package so that I don't have to explicilty load, parse and return a class instance every single time I have something to do with a configuration file:

from confuk import parse_config
from pathlib import Path
from somewhere import ConfigClass

cfg_dict = parse_config(Path("some.toml"))  # returns a dictionary
cfg_obj = parse_config(Path("some.toml"), ConfigClass)  # returns an instance of `ConfigClass`

[!tip] confuk also supports a number of output configuration styles out-of-the-box, including omegaconf, Pydantic and EasyDict.

Installation

pip install confuk

Or:

poetry add confuk

Building from source

You can build the package using Poetry:

  1. Clone this repo.
  2. Run poetry build.
  3. Grab the installable wheel from the dist folder and install it with pip or add the package as a local dependency of another project.

Special features

Config output formats

EasyDict

If you really hate referring to dictionary keys and you do not intend to create a custom configuration class for your config, you can parse the file to an EasyDict:

cfg_edict = parse_config(Path("some.toml"), "attr")

Now, if the key something exists in the configuration file, you can simply refer to it using an attribute:

cfg_edict.something

OmegaConf

OmegaConf is one of the most complete configuration systems for Python applications. If you want to leverage its features while still working with confuk as a front-end, you can simply parse the configuration into an instance of omegaconf.DictConfig by doing the following:

cfg = parse_config(Path("some.toml"), "omega")

Pydantic

If you're a fan of Pydantic with custom config classes for automatic validation, just use any class that inherits from BaseModel:

from confuk import parse_config
from pathlib import Path
from pydantic import BaseModel

class Metrics(BaseModel):
    psnr: float
    ssim: float


cfg_dict = parse_config(Path("some.toml"), Metrics)  # returns a dictionary

All currently supported formats

Format cfg_class argument
dict "d" / None
EasyDict "ed" / "edict" / "attr"
OmegaConf "o" / "omega" / "omegaconf"
pydantic BaseModel class
custom any class supporting **kwargs in the constructor

Imports

Because keeping hundreds of config files can become tedious, especially when there is shared values between them, you might want to consider using the imports functionality.

Say you have a TOML file from which you want to inherit values:

[something]
value = 1
another_value = 2

[something_else]
value = 3

You can "import" it using a preamble:

[pre]
imports = [
    "${this_dir}/test_imported.toml",
]

[something]
value = 69

[!note] Older versions of confuk used the $this_dir syntax instead. This will be supported going into the future but it won't work with variable interpolation (expect it to only work for the special interpolation markers such as $this_dir and $cwd).

This is equivalent to specifying a config like:

[something]
value = 69
another_value = 2

[something_else]
value = 3

Note that you can use two special interpolation markers to specify paths in the import section:

  • ${this_dir} -> points to a directory relative to the configuration file that contains the import section
  • ${cwd} -> points to the current working directory

[!warning] The preamble will be removed after it's processed. It's there only to control how confuk should process the loaded configuration files and it's dropped afterwards. Do not put any meaningful configuration into your preamble, except for confuk's control elements.

What about inheriting selected values?

Unsupported. And I do not plan to add support for cherrypicking values from other configs. It makes things way messier in my opinion, as it becomes way harder to reason about the flow of variables.

What about variable interpolation?

This is supported with the syntax that OmegaConf uses, e.g. path = "${some.root.path}/file.txt" will pick up the path variable from some.root config section.

[!note] We are using omegaconf for this under the hood since they already have a great parser for this and there's no use duplicating work.

What about deeply nested configs?

If you like the deeply nested folder-file structure for your configs then Hydra might be more for you. I've used it before and it's very good but I personally find the design choice of creating directory structures for configs quite tedious.

confuk strives to be flatter: you import another config file in the preamble section and you have a choice of what to override. This makes it more comfortable to use when you have one default.toml config file for something and then create a bunch of configurations overriding certain values. This is useful for experiments in the AI/ML space, where I'm spending most of my time now.

You are of course free to structure your files as you please but don't expect a feature similar to Hydra's defaults in confuk – I do indeed use Hydra for applications which require such a system!

Command-line overrides

One of the most fantastic features I've found when using Hydra was the ability to override values from the config file on the command line. This is convenient when you want to quickly test some changes to your configuration without going through the trouble of creating a new config file.

So I concluded it would be fun to implement it in confuk in a similar fashion. Here's how it works:

import confuk


@confuk.main(config=Path(__file__).parent / "test.toml", config_format="o", verbose=False)
def main(cfg, *args):
    console = Console()
    console.print(cfg)
    return cfg

This decorator behaves similarly to @hydra.main decorator and it creates a minimal argument parser for your application entrypoint under the hood.

Now, when running the app, you can specify any value overrides on the command line. For example if your config looks like this:

[my]
mother = 1

[your.dad]
father = 1

And you run your CLI app with the argument your.dad.father=3, you will override the pertinent value from 1 to 3.

[!tip] The underlying argument parser also contains a --config option. You can use it to switch to a different config path on the command line, without a need to rely on the default one that has been set in the decorator.

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

confuk-0.3.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

confuk-0.3.0-py3-none-any.whl (8.0 kB view details)

Uploaded Python 3

File details

Details for the file confuk-0.3.0.tar.gz.

File metadata

  • Download URL: confuk-0.3.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for confuk-0.3.0.tar.gz
Algorithm Hash digest
SHA256 42ae5176e99fe38aea1b3e2d68ad88fedb70b2e6f6d675e69a0d933ff6204030
MD5 c8359a7ff6994d7d40ad9a97d37a0535
BLAKE2b-256 f0ecb8201738101b3c2e34a67cff74cb48d1ab7179df8f16207da1775bf52f72

See more details on using hashes here.

File details

Details for the file confuk-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: confuk-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 8.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for confuk-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 338f14f6f499225de7b0c8577712a667f888a33e95e2e4c71c835d5fc8ce1a73
MD5 9e47bfd509612688cd63582c18bfb48e
BLAKE2b-256 20e37e4ee2bcf9e377191bc4ab97208f0ab75c766b1e6cdb9c039bd4b5f4273c

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