Skip to main content

A configuration utility for Python object.

Project description


CI Actions Status Pulish Actions Status Python version pypi version license

Quick Links


colt is a configuration utility for Python objects. colt constructs Python objects from a configuration dict which is convertable into JSON. (Inspired by AllenNLP)


pip install colt


Basic Usage

import typing as tp
import colt

class Foo:
    def __init__(self, message: str) -> None:
        self.message = message

class Bar:
    def __init__(self, foos: tp.List[Foo]) -> None:
        self.foos = foos

if __name__ == "__main__":
    config = {
        "@type": "bar",  # specify type name with `@type`
        "foos": [
            {"message": "hello"},  # type of this is inferred from type-hint
            {"message": "world"},

    bar =

    assert isinstance(bar, Bar)

    print(" ".join(foo.message for foo in bar.foos))
        # => "hello world"

scikit-learn Configuration

import colt

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

if __name__ == "__main__":
    config = {
        # import types automatically if type name is not registerd
        "@type": "sklearn.ensemble.VotingClassifier",
        "estimators": [
            ("rfc", { "@type": "sklearn.ensemble.RandomForestClassifier",
                      "n_estimators": 10 }),
            ("svc", { "@type": "sklearn.svm.SVC",
                      "gamma": "scale" }),

    X, y = load_iris(return_X_y=True)
    X_train, X_valid, y_train, y_valid = train_test_split(X, y)

    model =, y_train)

    valid_accuracy = model.score(X_valid, y_valid)
    print(f"valid_accuracy: {valid_accuracy}")

Registrable Class

By using the Registrable class, you can devide namespace into each class. In a following example, Foo and Bar have different namespaces.

import colt

class Foo(colt.Registrable):

class Bar(colt.Registrable):

class FooBaz(Foo):

class BarBaz(Bar):

class MyClass:
    def __init__(self, foo: Foo, bar: Bar): = foo = bar

if __name__ == "__main__":
    config = {
        "@type": "my_class",
        "foo": {"@type": "baz"},
        "bar": {"@type": "baz"}

    obj =

    assert isinstance(, FooBaz)
    assert isinstance(, BarBaz)

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

colt-0.8.0.tar.gz (8.4 kB view hashes)

Uploaded Source

Built Distribution

colt-0.8.0-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

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