Skip to main content

install packages and run Python with them

Project description

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

Project details


Download files

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

Built Distribution

configload-0.0.1-py3-none-any.whl (5.1 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page