Confection

1.3.3 · active · verified Wed Apr 08

Confection is a lightweight and flexible configuration system for Python, designed to handle complex nested configurations with features like interpolation, validation, and registry integration. It is currently at version 1.3.3 and maintains an active release cadence, often aligning with updates to related libraries like spaCy and Thinc.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a function in the registry, create a `Config` object, and resolve the configuration to execute the registered function with provided arguments. It also shows an example of loading a config from a string.

from confection import Config, set_registry

@set_registry("my_functions", "add")
def add_function(a: int, b: int):
    return a + b

config = Config({"model": {"func": {"@my_functions": "add"}, "a": 1, "b": 2}})

resolved = config.resolve()
result = resolved["model"]["func"](resolved["model"]["a"], resolved["model"]["b"])

print(f"Result: {result}")

# Example with direct config creation
config_direct = Config().from_str("""
[model]
func = @my_functions.add
a = 10
b = 20
""")
resolved_direct = config_direct.resolve()
result_direct = resolved_direct["model"]["func"](resolved_direct["model"]["a"], resolved_direct["model"]["b"])

print(f"Direct Config Result: {result_direct}")

view raw JSON →