Skip to main content

Code style: black GitHub Workflow Status PyPI version PyPI pyversions License: MIT

configload

Teeny package to support loading a dataclass from a config file (.ini, .json; optionally, with extensions .yaml, .toml).

Note: dynaconf is a much more featureful implementation of a similar idea, so for anything serious it might be a better choice! see also configloader for another similar implementation

Install

# install with extras: support for yaml and toml (requires extra dependencies)
pip install configload[yaml,toml]

Usage

Instantiate a child class like so:

from configload import PyConfigFile

class MyConfigClass(PyConfigFile):
    # setting defaults makes it simpler to instantiate
    yolo: int = 1234
    heyo: str = "hi!"
    numbertime: float = 1.23

myconfig = MyConfigClass(configfile="settings.ini")

# keys from "settings.ini" that match fields in the dataclass will be converted
# to specified types and loaded into myconfig. unknown keys are silently skipped

Limitations

Flat structure

To keep things consistent between ini and structured formats, and due to the limitations of dataclasses, really only top-level (flat) structure is suited to this.

For example, this JSON:

{
    "top_level_key": {
        "nested_key": 1
    }
}

Will result in a dataclass with a dict field .top_level_key, but after that, nested values will have to use dict semantics, i.e.:

# to access nested keys:
myconfig.top_level_key["nested_key"]

Simple types only

Only simple dataclass types (str, int, list, dict, etc) are supported, and ini format only supports scalar types.

TODO support may be added for more complex data types by using ast.literal_eval to convert string repr of a data structure in an ini key value, but this is:

  • kinda sketchy, error prone
  • might not be that useful anyway 🤷

Example might be:

[DEFAULT]
# python string repr of a dictionary
key_1 = { "nested_key": 123, }

.ini default section

The .ini format has section markers (eg [DEFAULT]) as optional, but the python standard library configparser requires at least one section marker.

This library (configload) will only parse keys from the [DEFAULT] section, all other sections are ignored.

If the ini file doesn't contain a [DEFAULT] section an error will be raised.

Config File Formats

Subjective comparision:

  • .ini - built into python standard library, supports comments, no types
  • .json - built in, no comments 😞, types
  • .yaml - not built in, comments, types
  • .toml - not built in, comments, types

Metadata

Release files for configload 0.0.1

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

Built distribution (wheel)

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

Release files / configload-0.0.1-py3-none-any.whl

Download URL configload-0.0.1-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b6d84ee15bc8e9e92f5ba70c2701ccee3b54a136ecb9432bbee0efcd9997eda
BLAKE2b-256 checksum
How to use checksums
e8b0e4a68dce7cf027070c24cca25c4734d78a20d1c6a3c45392e49724f3dd66
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.8.2

Release history Release notifications | RSS feed

This release

0.0.1 This release

1 release file

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