Skip to main content

Merge your config files and set parameters from the command line in a simple way.

Project description

CLI Config

Lightweight library to merge your configs (optionally nested) and set parameters from command line.

Documentation :memo: here

PyPI version PythonVersion License

Ruff_logo Black_logo

Ruff Flake8 Pydocstyle MyPy PyLint

Tests Coverage Documentation Status

Installation

In a new virtual environment, install the package with:

pip install cliconfig

Quick start

First create a default config that can be split in multiple files that will be merged (from left to right in make_config function):

# default1.yaml
param1: 1
param2: 0
letters:
  letter1: a
  letter2: b
# default2.yaml
param1: 1
param2: 2  # will override param2 from default1.yaml
letters.letter3: c  # add a new parameter

Now you can set up your program to use the config:

# main.py
from cliconfig import make_config, show_config

config = make_config('default1.yaml', 'default2.yaml')
show_config(config)

Then add one or multiple additional config files that will override the default values. Be careful, the additional config files cannot bring new parameters by default. If you want to add new parameters (not advised for retro-compatibility, readability and security), you can add allow_new_keys=True in make_config function.

# exp1.yaml
letters:
  letter3: C
# exp2.yaml
param1: -1
letters.letter1: A

Now you can launch the program with additional configurations and parameters. The additional configurations are indicated with --config (separate with comma, without space) and the parameters with --<param_name>. The default configuration will be merged with the additional configurations (from left to right), then the parameters will be set.

python main.py --config exp1.yaml,exp2.yaml --param2=-2 --letters.letter2='B'

Will show:

[CONFIG] Merge 2 default configs, 2 additional configs and 2 CLI parameter(s).

Config:
    param1: -1
    param2: -2
    letters:
        letter1: A
        letter2: B
        letter3: C

Note that the configurations are native python dicts.

Manipulate configs

To merge configs, you can use cliconfig.merge_config function. It supports unflatten (or nested) dicts like {'a': {'b': 1, 'c': 2}}, flatten dicts like {'a.b': 1, 'a.c': 2}, and a mix of both. The dicts will be flatten before merging. Sometimes you can have conflicts in flatten operation for instance with {'a.b': 1, 'a': {'b': 2}} that have two different values for a.b. That's why you can use a priority parameter to choose which value to keep before merging.

You can also save, load and display configs with cliconfig.save_config, cliconfig.load_config and cliconfig.show_config functions.

How to contribute

For development, install the package dynamically and dev requirements with:

pip install -e .
pip install -r requirements-dev.txt

Everyone can contribute to CLI Config, and we value everyone’s contributions. Please see our contributing guidelines for more information 🤗

Todo

To do:

  • add json and ini support
  • avoid changing keys order in merge_config

License

Copyright (C) 2023 Valentin Goldité

This program is free software: you can redistribute it and/or modify it under the terms of the MIT License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

This project is free to use for COMMERCIAL USE, MODIFICATION, DISTRIBUTION and PRIVATE USE as long as the original license is include as well as this copy right notice at the top of the modified files.

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

cliconfig-0.1.4.tar.gz (27.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cliconfig-0.1.4-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

Details for the file cliconfig-0.1.4.tar.gz.

File metadata

  • Download URL: cliconfig-0.1.4.tar.gz
  • Upload date:
  • Size: 27.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for cliconfig-0.1.4.tar.gz
Algorithm Hash digest
SHA256 241b3815c070962d6707cb9717680d9c36abbcc8e3f96d1731621fe2af905ec1
MD5 e08b88d7f0cb9d540e12da721d7d890f
BLAKE2b-256 69467d04bcb963d234d785ad452abadca1ddfdbca3c5eac1bff0cf37b57c2e1b

See more details on using hashes here.

File details

Details for the file cliconfig-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: cliconfig-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for cliconfig-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0c0aeeee52bbfae547bda4e9d518f3cfe92462dac2335bd9ec67f34934078355
MD5 66702282c6a26af5aa206332755b1004
BLAKE2b-256 2d5fbb31ad9d9c8b1b2ca9d7ffa149b0f7c1dbfb255f92167f5e8bf35ae1dc0a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page