Skip to main content

Dataconf

Actions Status PyPI version

Simple dataclasses configuration management for Python with hocon/json/yaml/properties/env-vars/dict/cli support.

Getting started

Requires at least Python 3.9.

# pypi
pip install dataconf
poetry add dataconf

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

# local repo/dev
poetry install
pre-commit install

Usage

import os
from dataclasses import dataclass, field
from typing import List, Dict, Text, Union, Tuple
from dateutil.relativedelta import relativedelta
from datetime import datetime, timedelta
import dataconf

conf = """
str_name = test
str_name = ${?HOME}
dash-to-underscore = true
float_num = 2.2
iso_datetime = "2000-01-01T20:00:00"
iso_duration = "P123DT4H5M6S"
variable_length_tuple_data = [
    1
    2
    3
]
tuple_data = [
    a
    P1D
]
# this is a comment
list_data = [
    a
    b
]
nested {
    a = test
    b : 1
}
nested_list = [
    {
        a = test1
        b : 2.5
    }
]
duration = 2s
union = 1
people {
    name = Thailand
}
zone {
    area_code = 42
}
"""

class AbstractBaseClass:
    pass

@dataclass
class Person(AbstractBaseClass):
    name: Text

@dataclass
class Zone(AbstractBaseClass):
    area_code: int

@dataclass
class Nested:
    a: Text
    b: float

@dataclass
class Config:
    str_name: Text
    dash_to_underscore: bool
    float_num: float
    iso_datetime: datetime
    iso_duration: timedelta
    variable_length_tuple_data: Tuple[int, ...]
    tuple_data: Tuple[Text, timedelta]
    list_data: List[Text]
    nested: Nested
    nested_list: List[Nested]
    duration: relativedelta
    union: Union[Text, int]
    people: AbstractBaseClass
    zone: AbstractBaseClass
    default: Text = 'hello'
    default_factory: Dict[Text, Text] = field(default_factory=dict)

print(dataconf.string(conf, Config))
# Config(
#   str_name='/users/root/',
#   dash_to_underscore=True,
#   float_num=2.2,
#   iso_datetime=datetime.datetime(2000, 1, 1, 20, 0),
#   iso_duration=datetime.timedelta(days=123, seconds=14706),
#   variable_length_tuple_data=(1,2,3),
#   tuple_data=('a', datetime.timedelta(days=1)),
#   list_data=['a', 'b'],
#   nested=Nested(a='test', b=1),
#   nested_list=[Nested(a='test1', b=2.5)],
#   duration=relativedelta(seconds=+2),
#   union=1,
#   people=Person(name='Thailand'),
#   zone=Zone(area_code=42),
#   default='hello',
#   default_factory={}
# )

@dataclass
class Example:
    hello: str
    world: str
    foo: List[str]

os.environ['DC_WORLD'] = 'monde'

print(
    dataconf
    .multi
    .url('https://raw.githubusercontent.com/zifeo/dataconf/main/confs/simple.hocon')
    .env('DC')
    .on(Example)
)
# Example(hello='bonjour',world='monde')

API

import dataconf

conf = dataconf.string('{ name: Test }', Config)
conf = dataconf.string('name:\n\tvalue: Test', Config, loader=dataconf.YAML)  # dataconf.HOCON by default
conf = dataconf.env('PREFIX_', Config)
conf = dataconf.dict({'name': 'Test'}, Config)
conf = dataconf.url('https://raw.githubusercontent.com/zifeo/dataconf/master/confs/test.hocon', Config)  # hocon, json, yaml, properties
conf = dataconf.file('confs/test.hocon', Config)  # hocon, json, yaml, properties
conf = dataconf.cli(sys.argv, Config)

# Aggregation
conf = dataconf.multi.string(...).env(...).url(...).file(...).dict(...).cli(...).on(Config)

# Same api as Python json/yaml packages (e.g. `load`, `loads`, `dump`, `dumps`)
conf = dataconf.load('confs/test.hocon', Config)  # hocon, json, yaml, properties
conf = dataconf.load('confs/test.yaml', Config, loader=dataconf.YAML)  # dataconf.HOCON by default
dataconf.dump('confs/test.hocon', conf, out='hocon')
dataconf.dump('confs/test.json', conf, out='json')
dataconf.dump('confs/test.yaml', conf, out='yaml')
dataconf.dump('confs/test.properties', conf, out='properties')

For full HOCON capabilities see here.

Parse env vars

PREFIX_VAR=a
PREFIX_VAR_NAME=b
PREFIX_TEST__NAME=c
PREFIX_LS_0=d
PREFIX_LS_1=e
PREFIX_LSLS_0_0=f
PREFIX_LSOB_0__NAME=g
PREFIX_NESTED_="{ name: Test }"
PREFIX_SUB_="{ value: ${PREFIX_VAR} }"

is equivalent to

{
    var = a
    var_name = b
    test {
        name = c
    }
    ls = [
        d
        e
    ]
    lsls = [
        [
            f
        ]
    ]
    lsob = [
        {
            name = g
        }
    ]
    nested {
        # parse nested config by suffixing env var with `_`
        name: Test
    }
    sub {
        # will have value "a" at parsing, useful for aliases
        value = ${PREFIX_VAR}
    }
}

Note that when using .env source, the strict mode is disabled and value might be casted.

Parse CLI arguments

Same as env vars parse (dashes are converted to underscore, e.g. TEST_A--test-a).

CLI usage

Can be used for validation or converting between supported file formats (-o).

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'>

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dataconf-3.7.0.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dataconf-3.7.0-py3-none-any.whl (16.5 kB view details)

Uploaded Python 3

File details

Details for the file dataconf-3.7.0.tar.gz.

File metadata

  • Download URL: dataconf-3.7.0.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.17.0-1022-azure

File hashes

Hashes for dataconf-3.7.0.tar.gz
Algorithm Hash digest
SHA256 614b7588b43bcd29d819b24c582be5d8c67cda4d9747c81c730ae7ca79ee0eaa
MD5 3eaf90417c36bde9f3969fb51858011b
BLAKE2b-256 96a601db958e8e81c6d621dee3d0f807d566d86276f244a6d246cfbc9aaae066

See more details on using hashes here.

File details

Details for the file dataconf-3.7.0-py3-none-any.whl.

File metadata

  • Download URL: dataconf-3.7.0-py3-none-any.whl
  • Upload date:
  • Size: 16.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.3 Linux/6.17.0-1022-azure

File hashes

Hashes for dataconf-3.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 680c786fe2d50b495eb1091c0bcda8656ef28f11cb08cdf9aac56edcbce72b98
MD5 de51fa54bcb4ddc7cad13169d85d40f4
BLAKE2b-256 243cbcaebf1072c9f2b54c06737394434b4e35eb6034a477984260d179f684fe

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

3.7.0 This release

2 files

3.6.0

2 files

3.5.0

2 files

3.4.0

2 files

3.3.0

2 files

3.2.0

2 files

3.1.0

2 files

3.0.0

2 files

2.5.0

2 files

2.4.0

2 files

2.3.0

2 files

2.2.2

2 files

2.2.1

2 files

2.2.0

2 files

2.1.3

2 files

2.1.2

2 files

2.1.1

2 files

2.1.0

2 files

2.0.0

2 files

1.5.0

2 files

1.4.0

2 files

1.3.0

2 files

1.2.0

2 files

1.1.0

2 files

1.0.3

2 files

1.0.2

2 files

1.0.1

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 files

0.1.6

2 files

0.1.5

2 files

0.1.4

2 files

0.1.3

2 files

0.1.2

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page