Skip to main content

Easily deserialize Data Classes from ini files

Project description

dataclasses-configobj

Hydrate Data Classes from ini/dat/toml files.

Aims to be dataclasses-json, but for configobj.

This is very alpha right now, but the feaures which work, should work:

Usage

Define the shape of your config:

  • Each subsection will be mapped to a nested class
  • Each subsection may define a single _many
  • A _many must have type List[...]
  • The type of List of a _many shall have a _name

Example:

@dataclass
class Single:
    other: str

@dataclass
class OneOfMany:
    _name: str
    val: str

@dataclass
class Config:
    single: Single
    _many: List[OneOfMany]
    optional: Optional[str] = None

To load this .ini file:

[single]
other = hello
[one]
val = apple
[two]
val = banana

We can read, validate, and lift to an instance of Config with:

spec = core.to_spec(Config)
co = configobj.ConfigObj(infile=infile, configspec=spec)

validator = validate.Validator()
co.validate(validator)

config: Config = dataclasses.lift(Config, co)

To yield config:

Config(
    single=Single(other='hello'),
    optional=None,
    withdefault='test123'
    _many=[
        OneOfMany(_name='one', val='apple'),
        OneOfMany(_name='two', val='banana')
    ]
)

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

dataclasses-configobj-0.0.4.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

dataclasses_configobj-0.0.4-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file dataclasses-configobj-0.0.4.tar.gz.

File metadata

  • Download URL: dataclasses-configobj-0.0.4.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for dataclasses-configobj-0.0.4.tar.gz
Algorithm Hash digest
SHA256 96e77cf0e2a38d66b3859e36b8224ef157452a9e14ba5091e727b0df327c3be6
MD5 a85c6c2fe4e253659df4308f2805069f
BLAKE2b-256 0302dced431be7626508c0244998b3f606e6df0cac27022a47ed9e6b7e58ab16

See more details on using hashes here.

File details

Details for the file dataclasses_configobj-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: dataclasses_configobj-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.6

File hashes

Hashes for dataclasses_configobj-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 63c0372017ed80117e283ae083d65fe77258063d08100631060bb60d409de8cc
MD5 e993dfa467e52f7f2e5df61575f5f415
BLAKE2b-256 9f25f8d134f9206c552c5daa7539f4c51a25bee45297a859ec0716ecd049d5b9

See more details on using hashes here.

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