Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

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!

Subcommands

Confit applications can be composed into groups of subcommands:

app = Cli()
training = Cli()


@training.command(name="run")
def run(epochs: int = 10):
    print(f"Training for {epochs} epochs")


app.add_subcommands(training, name="training")

Run the nested command with:

python script.py training run --config config.yml --epochs 20

Acknowledgement

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

Release files for confit 0.13.0.dev1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for confit 0.13.0.dev1
File Size Uploaded
confit-0.13.0.dev1.tar.gz 48.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for confit 0.13.0.dev1
File Interpreter ABI Platform
confit-0.13.0.dev1-py3-none-any.whl Python 3 none any Details

Total release size: 83.4 kB

Release files / confit-0.13.0.dev1.tar.gz

Download URL confit-0.13.0.dev1.tar.gz
Size 48.2 kB
Tags Source
SHA-256 checksum
How to use checksums
5cfc34645cf6f6539bf5f08c83538d1a718970d68e1a1093f4b50221e71ce979
BLAKE2b-256 checksum
How to use checksums
3c37aa38daabfee8e1f134c2e2280f39e13978bd940300d2beb354cde5d9adc7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / confit-0.13.0.dev1-py3-none-any.whl

Download URL confit-0.13.0.dev1-py3-none-any.whl
Size 35.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
93ca1d1114c9d653547bc68c3a1726b27e3a4622a4ee2c250337c6bb989a0945
BLAKE2b-256 checksum
How to use checksums
e61fd81ee94a3e11e50e41c7a01c35685d2b242debe399ab56f9ad8bebabe884
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

0.13.0.dev1 This release

2 release files

0.12.0

2 release files

0.11.1

2 release files

0.11.0

2 release files

0.10.2

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.5

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.6

2 release files

0.5.5

1 release file

0.5.4

1 release file

0.5.3

1 release file

0.5.2

1 release file

0.5.1

1 release file

0.5.0

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.0

1 release file

0.2.1

1 release file

0.0.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page