Skip to main content

Smart configuration framework

Reason this release was yanked:

missing files in wheel

Project description

Tests Documentation PyPI Coverage

Confit

Confit is a complete and easy-to-use configuration framework aimed at improving the reproducibility of experiments by relying on the Python typing system, minimal configuration files and command line interfaces.

Getting started

Install the library with pip:

pip install confit

Confit only abstracts the boilerplate code related to configuration and leaves the rest of your code unchanged.

Here is an example:

script.py
+ from confit import Cli, Registry, RegistryCollection
  
+ class registry(RegistryCollection):
+     factory = Registry(("test_cli", "factory"), entry_points=True)
 
+ @registry.factory.register("submodel")
class SubModel:
    # Type hinting is optional but recommended !
    def __init__(self, value: float, desc: str = ""):
        self.value = value
        self.desc = desc
 
 
+ @registry.factory.register("bigmodel")
class BigModel:
    def __init__(self, date: datetime.date, submodel: SubModel):
        self.date = date
        self.submodel = submodel
 
+ app = Cli(pretty_exceptions_show_locals=False)

# you can use @confit.validate_arguments instead if you don't plan on using the CLI
+ @app.command(name="script", registry=registry)
def func(modelA: BigModel, modelB: BigModel, seed: int = 42):
    """
    Display the configured model dates.

    Parameters
    ----------
    modelA : BigModel
        The first model whose date is displayed.
    modelB : BigModel
        The second model whose date is displayed.
    seed : int
        Random seed.
    """
    assert modelA.submodel is modelB.submodel
    print("modelA.date:", modelA.date.strftime("%B %-d, %Y"))
    print("modelB.date:", modelB.date.strftime("%B %-d, %Y"))
 
+ if __name__ == "__main__":
+     app()

Create a new config file

The following also works with YAML files

config.cfg
# CLI sections
[script]
modelA = ${modelA}
modelB = ${modelB}

# CLI common parameters
[modelA]
@factory = "bigmodel"
date = "2003-02-01"

[modelA.submodel]
@factory = "submodel"
value = 12

[modelB]
date = "2003-04-05"
submodel = ${modelA.submodel}

and run the following command from the terminal

python script.py --config config.cfg --seed 43

The generated CLI also has a readable help message based on the function signature and docstrings:

python script.py --help
Display the configured model dates.

Parameters
----------
  --config <Path>
    Load a config file to fill in the following params. Can be repeated.

  --modelA <BigModel>
    The first model whose date is displayed.

    --modelA.<field> VALUE

  --modelB <BigModel>
    The second model whose date is displayed.

    --modelB.<field> VALUE

  --seed <int> (default: 42)
    Random seed.

You can still call the function method from your code, but now also benefit from argument validation !

from script import func, BigModel, SubModel

# To seed before creating the models
from confit.utils.random import set_seed

seed = 42
set_seed(seed)

submodel = SubModel(value=12)
func(
    # BigModel will cast date strings as datetime.date objects
    modelA=BigModel(date="2003-02-01", submodel=submodel),
    # Since the modelB argument was typed, the dict is cast as a BigModel instance
    modelB=dict(date="2003-04-05", submodel=submodel),
    seed=seed,
)
modelA.date: February 1, 2003
modelB.date: April 5, 2003

Serialization

You can also serialize registered classes, while keeping references between instances:

from confit import Config

submodel = SubModel(value=12)
modelA = BigModel(date="2003-02-01", submodel=submodel)
modelB = BigModel(date="2003-02-01", submodel=submodel)
print(Config({"modelA": modelA, "modelB": modelB}).to_str())
[modelA]
@factory = "bigmodel"
date = "2003-02-01"

[modelA.submodel]
@factory = "submodel"
value = 12

[modelB]
@factory = "bigmodel"
date = "2003-02-01"
submodel = ${modelA.submodel}

Error handling

You also benefit from informative validation errors:

func(
    modelA=dict(date="hello", submodel=dict(value=3)),
    modelB=dict(date="2010-10-05", submodel=dict(value="hi")),
)
ConfitValidationError: 2 validation errors for __main__.func()
-> modelA.date
   invalid date format, got 'hello' (str)
-> modelB.submodel.value
   value is not a valid float, got 'hi' (str)

Visit the documentation for more information!

Acknowledgement

We would like to thank Assistance Publique – Hôpitaux de Paris and AP-HP Foundation for funding this project.

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

confit-0.11.0.tar.gz (40.8 kB view details)

Uploaded Source

Built Distribution

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

confit-0.11.0-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file confit-0.11.0.tar.gz.

File metadata

  • Download URL: confit-0.11.0.tar.gz
  • Upload date:
  • Size: 40.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for confit-0.11.0.tar.gz
Algorithm Hash digest
SHA256 8dcced1231567163fc94b4221a9837f3de56662828314bdecbe5bcf1fb89a39d
MD5 4f1c75dfd84c1d265d715a665ecd6c51
BLAKE2b-256 9f75c0add877edef88bfbbccc8d8b83fd5b608fe54ee3f203f90cd71130bbb22

See more details on using hashes here.

File details

Details for the file confit-0.11.0-py3-none-any.whl.

File metadata

  • Download URL: confit-0.11.0-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.25

File hashes

Hashes for confit-0.11.0-py3-none-any.whl
Algorithm Hash digest
SHA256 deb08a3eb61fdd7a9e0799d9b1c08652c6eae07aff64ddbd8eabe8897b7f07c5
MD5 dbfbff49243fbfc7349da2516139a175
BLAKE2b-256 338bdba4f56392605b34227e44a42f7857ecc5e71ff77a8629c68bca93cd47b5

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