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.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

Uploaded Python 3

File details

Details for the file configload-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: configload-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for configload-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 7b6d84ee15bc8e9e92f5ba70c2701ccee3b54a136ecb9432bbee0efcd9997eda
MD5 93078590546ca043319258351be4beed
BLAKE2b-256 e8b0e4a68dce7cf027070c24cca25c4734d78a20d1c6a3c45392e49724f3dd66

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