Skip to main content

colt

CI Actions Status Pulish Actions Status Python version pypi version license

Quick Links

Introduction

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

Installation

pip install colt

Examples

Basic Usage

import typing as tp
import colt

@colt.register("foo")
class Foo:
    def __init__(self, message: str) -> None:
        self.message = message

@colt.register("bar")
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 = colt.build(config)

    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 = colt.build(config)
    model.fit(X_train, 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):
    pass

class Bar(colt.Registrable):
    pass

@Foo.register("baz")
class FooBaz(Foo):
    pass

@Bar.register("baz")
class BarBaz(Bar):
    pass

@colt.register("my_class")
class MyClass:
    def __init__(self, foo: Foo, bar: Bar):
        self.foo = foo
        self.bar = bar

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

    obj = colt.build(config)

    assert isinstance(obj.foo, FooBaz)
    assert isinstance(obj.bar, BarBaz)

Release files for colt 0.9.0

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

Source distribution (sdist)

Source distribution for colt 0.9.0
File Size Uploaded
colt-0.9.0.tar.gz 8.7 kB Details

Built distribution (wheel)

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

Total release size: 17.3 kB

Release files / colt-0.9.0.tar.gz

Download URL colt-0.9.0.tar.gz
Size 8.7 kB
Tags Source
SHA-256 checksum
How to use checksums
e9532719831486ae8e87365c298f2ccad8193e61d497f2b48464cde11a3e4600
BLAKE2b-256 checksum
How to use checksums
363c030597fc49c22d5c909c7106d18529b78bb01405d9068558316f123aadad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.8.2 Linux/5.15.0-1020-azure

Release files / colt-0.9.0-py3-none-any.whl

Download URL colt-0.9.0-py3-none-any.whl
Size 8.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3af7a32c700e97afbf34d648094aae47b0c6d5e5645cfa3e5f5dc14f8d9543d3
BLAKE2b-256 checksum
How to use checksums
bcc6b6fff8f9d9ec4577b5458cf0ba9496941dc7d30c225e0203e9d27c055c86
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.1 CPython/3.8.2 Linux/5.15.0-1020-azure

Release history Release notifications | RSS feed

0.21.0

2 release files

0.20.0

2 release files

0.19.0

2 release files

0.17.4

2 release files

0.17.3

2 release files

0.17.2

2 release files

0.16.7

2 release files

0.16.6

2 release files

0.16.5

2 release files

0.16.4

2 release files

0.16.3

2 release files

0.16.2

2 release files

0.14.3

2 release files

0.14.2

2 release files

0.14.1

2 release files

0.14.0

2 release files

0.13.1

2 release files

0.13.0

2 release files

0.12.0

2 release files

0.11.3

2 release files

0.11.2

2 release files

0.10.0

2 release files

0.9.3

2 release files

0.9.2

2 release files

0.9.1

2 release files

This release

0.9.0 This release

2 release files

0.8.1

2 release files

0.8.0

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.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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