Skip to main content

Lightweight configuration with automatic dataclasses parsing (HOCON/JSON/YAML/PROPERTIES)

Project description

Dataconf

Actions Status PyPI version

Getting started

Requires at least Python 3.8.

# pypi
pip install dataconf
poetry add dataconf

# master
pip install --upgrade git+https://github.com/zifeo/dataconf.git
poetry add git+https://github.com/zifeo/dataconf.git

Usage

from dataclasses import dataclass, field
from typing import List, Dict, Union
from dateutil.relativedelta import relativedelta
import dataconf

conf = """
str = test
str = ${HOSTNAME}
float = 2.2
list = [
    a
    b
]
nested {
    a = test
}
nested_list = [
    {
        a = test1
    }
]
duration = 2s
union = 1
"""

@dataclass
class Nested:
    a: str

@dataclass
class Config:
    str: str
    float: float
    list: List[str]
    nested: Nested
    nested_list: List[Nested]
    duration: relativedelta
    default_factory: Dict[str, str] = field(default_factory=dict)
    union: Union[str, int]

print(dataconf.load(conf, Config))
# TestConf(test='pc.home', float=2.1, list=['a', 'b'], nested=Nested(a='test'), nested_list=[Nested(a='test1')], duration=datetime.timedelta(seconds=2), default_factory={}, union=1)
import dataconf

conf = dataconf.loads('confs/test.hocon', Config)
conf = dataconf.loads('confs/test.json', Config)
conf = dataconf.loads('confs/test.yaml', Config)
conf = dataconf.loads('confs/test.properties', Config)

dataconf.dumps('confs/test.hocon', out='hocon')
dataconf.dumps('confs/test.json', out='json')
dataconf.dumps('confs/test.yaml', out='yaml')
dataconf.dumps('confs/test.properties', out='properties')

Follows same api as python JSON package (e.g. load, loads, dump, dumps). For full HOCON capabilities see here.

CI

dataconf -c confs/test.hocon -m tests.configs -d TestConf -o hocon
# dataconf.exceptions.TypeConfigException: expected type <class 'datetime.timedelta'> at .duration, got <class 'int'>

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

dataconf-0.1.4.tar.gz (8.1 kB view hashes)

Uploaded Source

Built Distribution

dataconf-0.1.4-py3-none-any.whl (9.2 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