colt

🐎 Colt: Effortlessly configure and construct Python objects with colt, a lightweight library inspired by AllenNLP and Tango

https://github.com/altescy/colt

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (15.3%) to scientific vocabulary

Keywords

configuration python

Keywords from Contributors

interactive serializer packaging network-simulation shellcodes hacking autograding observability embedded optim
Last synced: 6 months ago · JSON representation

Repository

🐎 Colt: Effortlessly configure and construct Python objects with colt, a lightweight library inspired by AllenNLP and Tango

Basic Info
Statistics
  • Stars: 25
  • Watchers: 3
  • Forks: 0
  • Open Issues: 7
  • Releases: 34
Topics
configuration python
Created about 6 years ago · Last pushed 6 months ago
Metadata Files
Readme License

README.md

🐎 Colt

CI Actions Status Pulish Actions Status Python version pypi version license

Effortlessly configure and construct Python objects with colt, a lightweight library inspired by AllenNLP and Tango

Quick Links

Introduction

colt is a lightweight configuration utility for Python objects, allowing you to manage complex configurations for your projects easily. Written solely using the Python standard library, colt can construct class objects from JSON-convertible dictionaries, making it simple to manage your settings using JSON or YAML files. The library is particularly suitable for the dependency injection design pattern.

Some key features of colt include:

  • No external dependencies, as it is built using the Python standard library.
  • Construct class objects from JSON-convertible dictionaries.
  • Manage complex configurations using JSON or YAML files.
  • Well-suited for dependency injection design patterns.

Inspired by AllenNLP and Tango, colt aims to offer similar functionality while focusing on a more lightweight and user-friendly design.

Differences between colt and AllenNLP/Tango

While both AllenNLP and Tango construct objects based on the class signature, colt focuses on building objects from the type specified in the configuration. Although colt is aware of the class signature, it primarily uses it for validation when passing objects created from the configuration.

This means that with colt, you don't necessarily need to have the target class available for configuration. As a result, you can conveniently build objects using the colt.build method without requiring the specific class to be present. This distinction makes colt more flexible and easier to work with in various scenarios.

Installation

To install colt, simply run the following command:

shell pip install colt

Usage

Basic Example

Here is a basic example of how to use colt to create class objects from a configuration dictionary:

```python 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"

```

Functionality

Guiding Object Construction with a Target Class

You can guide the object construction process in colt by passing the desired class as the second argument to the colt.build method. Here's an example demonstrating this functionality:

```python @colt.register("foo") class Foo: def init(self, x: str) -> None: self.x = x

config = {"x": "abc"}

Pass the desired class as the second argument

obj = colt.build(config, Foo)

assert isinstance(obj, Foo) assert obj.x == "abc" ```

By providing the target class to colt.build, you can ensure the constructed object is of the desired type while still using the configuration for parameter values.

Registrable class

colt provides the Registrable class, which allows you to divide the namespace for each class. This can be particularly useful when working with larger projects or when you need to manage multiple classes with the same name but different functionality.

Here is an example of how to use the Registrable class to manage different namespaces for Foo and Bar:

```python 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("myclass") 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)

```

Lazy class

colt offers a Lazy class for deferring object creation until needed, which can be useful in cases where constructing an object is computationally expensive or should be delayed until certain conditions are met.

Here's a concise example demonstrating the Lazy class usage with colt:

```python import dataclasses import colt from colt import Lazy

@dataclasses.dataclass class Foo: x: str y: int

@dataclasses.dataclass class Bar: foo: Lazy[Foo]

bar = colt.build({"foo": {"x": "hello"}}, Bar)

Additional parameters can be passed when calling the construct() method

foo = bar.foo.construct(y=10) ```

In this example, Bar contains a Lazy instance of Foo, which will only be constructed when construct() is called. When calling construct(), you can pass additional parameters required for the object's construction. This approach allows you to control when an object is created, optimizing resource usage and computations while providing flexibility in passing parameters.

Advanced Examples

scikit-learn Configuration

Here's an example of how to use colt to configure a scikit-learn model:

```python import colt

from sklearn.datasets import loadiris from sklearn.modelselection import traintestsplit

if name == "main": config = { # these types are imported 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}")

```

In this example, colt is used to configure a VotingClassifier from scikit-learn, combining a RandomForestClassifier and an SVC. The colt configuration dictionary makes it easy to manage the settings of these classifiers and modify them as needed.

Influences

colt is heavily influenced by the following projects:

  • AllenNLP: A popular natural language processing library, which provides a powerful configuration system for managing complex experiments.
  • Tango: A lightweight and flexible library for running machine learning experiments, designed to work well with AllenNLP and other libraries.

These projects have demonstrated the value of a robust configuration system for managing machine learning experiments and inspired the design of colt.

Owner

  • Name: Yasuhiro Yamaguchi
  • Login: altescy
  • Kind: user
  • Location: Kanagawa, Japan
  • Company: Cookpad Inc.

Research Engineer / NLP / ML

GitHub Events

Total
  • Release event: 13
  • Watch event: 1
  • Delete event: 19
  • Push event: 53
  • Pull request event: 39
  • Create event: 33
Last Year
  • Release event: 13
  • Watch event: 1
  • Delete event: 19
  • Push event: 53
  • Pull request event: 39
  • Create event: 33

Committers

Last synced: 8 months ago

All Time
  • Total Commits: 261
  • Total Committers: 2
  • Avg Commits per committer: 130.5
  • Development Distribution Score (DDS): 0.038
Past Year
  • Commits: 89
  • Committers: 1
  • Avg Commits per committer: 89.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
altescy a****y@f****m 251
dependabot[bot] 4****] 10
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 6 months ago

All Time
  • Total issues: 1
  • Total pull requests: 135
  • Average time to close issues: N/A
  • Average time to close pull requests: 7 days
  • Total issue authors: 1
  • Total pull request authors: 2
  • Average comments per issue: 0.0
  • Average comments per pull request: 0.03
  • Merged pull requests: 119
  • Bot issues: 1
  • Bot pull requests: 26
Past Year
  • Issues: 0
  • Pull requests: 61
  • Average time to close issues: N/A
  • Average time to close pull requests: 35 minutes
  • Issue authors: 0
  • Pull request authors: 1
  • Average comments per issue: 0
  • Average comments per pull request: 0.0
  • Merged pull requests: 59
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • dependabot[bot] (1)
Pull Request Authors
  • altescy (109)
  • dependabot[bot] (26)
Top Labels
Issue Labels
dependencies (1)
Pull Request Labels
dependencies (26)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 582 last-month
  • Total dependent packages: 0
  • Total dependent repositories: 4
  • Total versions: 42
  • Total maintainers: 1
pypi.org: colt

A configuration utility for Python object.

  • Versions: 42
  • Dependent Packages: 0
  • Dependent Repositories: 4
  • Downloads: 582 Last month
Rankings
Dependent repos count: 7.5%
Dependent packages count: 10.0%
Stargazers count: 12.9%
Average: 15.8%
Downloads: 18.9%
Forks count: 29.8%
Maintainers (1)
Last synced: 6 months ago

Dependencies

examples/titanic/Pipfile pypi
  • colt >=0.5.0
  • jsonnet *
  • kaggle *
  • lightgbm *
  • nltk *
  • pandas *
  • pdpipe *
examples/titanic/Pipfile.lock pypi
  • birch ==0.0.35
  • certifi ==2022.5.18.1
  • charset-normalizer ==2.0.12
  • click ==8.1.3
  • colt ==0.7.3
  • decore ==0.0.1
  • idna ==3.3
  • joblib ==1.1.0
  • jsonnet ==0.18.0
  • kaggle ==1.5.12
  • lightgbm ==3.3.2
  • nltk ==3.7
  • numpy ==1.22.4
  • pandas ==1.4.2
  • pdpipe ==0.2.5
  • python-dateutil ==2.8.2
  • python-slugify ==6.1.2
  • pytz ==2022.1
  • regex ==2022.6.2
  • requests ==2.28.0
  • scikit-learn ==1.1.1
  • scipy ==1.8.1
  • six ==1.16.0
  • skutil ==0.0.18
  • sortedcontainers ==2.4.0
  • strct ==0.0.32
  • text-unidecode ==1.3
  • threadpoolctl ==3.1.0
  • tqdm ==4.64.0
  • urllib3 ==1.26.9
  • wheel ==0.37.1
poetry.lock pypi
  • atomicwrites 1.4.0 develop
  • attrs 21.4.0 develop
  • black 22.3.0 develop
  • click 8.1.3 develop
  • colorama 0.4.4 develop
  • colorlog 4.8.0 develop
  • dacite 1.6.0 develop
  • flake8 4.0.1 develop
  • gitdb 4.0.9 develop
  • gitpython 3.1.27 develop
  • iniconfig 1.1.1 develop
  • isort 5.10.1 develop
  • jedi 0.17.2 develop
  • mccabe 0.6.1 develop
  • mypy 0.950 develop
  • mypy-extensions 0.4.3 develop
  • packaging 21.3 develop
  • parso 0.7.1 develop
  • pathspec 0.9.0 develop
  • platformdirs 2.5.2 develop
  • pluggy 1.0.0 develop
  • py 1.11.0 develop
  • pycodestyle 2.8.0 develop
  • pyflakes 2.4.0 develop
  • pyparsing 3.0.9 develop
  • pysen 0.10.2 develop
  • pytest 7.1.2 develop
  • python-jsonrpc-server 0.4.0 develop
  • python-language-server 0.36.2 develop
  • smmap 5.0.0 develop
  • tomli 2.0.1 develop
  • tomlkit 0.11.0 develop
  • typing-extensions 4.2.0 develop
  • ujson 5.4.0 develop
  • unidiff 0.7.3 develop
pyproject.toml pypi
  • black ^22.3.0 develop
  • flake8 ^4.0.1 develop
  • isort ^5.10.1 develop
  • mypy ^0.950 develop
  • pysen ^0.10.1 develop
  • pytest ^7.1.2 develop
  • python-language-server ^0.36.2 develop
  • python >=3.8,<3.11
.github/workflows/ci.yaml actions
  • actions/checkout v3 composite
  • actions/setup-python v4 composite
.github/workflows/pypi-publish.yaml actions
  • JRubics/poetry-publish v1.8 composite
  • actions/checkout v2 composite