Skip to main content

Load toml/yaml/json config files into classes for a typed config (type hinting etc.)

Project description

Classy Configuraptor

Configuraptor

Load config files into Python classes for a typed config (for type hinting etc.). Supported file types are toml/yaml/json, and .env/.ini to a lesser degree (see Supported Config File Types).

PyPI - Version PyPI - Python Version
Code style: black License: MIT
su6 checks Coverage

Table of Contents

Installation

pip install configuraptor

Usage

Configuraptor can be used to load your config files into structured Python classes.

# examples/example_from_readme.toml
[config]
name = "Hello World!"

[config.reference]
number = 42
numbers = [41, 43]
string = "42"

Could be loaded into Python classes using the following code:

# examples/example_from_readme.py
from configuraptor import load_into, TypedConfig


######################
# with basic classes #
######################

class SomeRegularClass:
    number: int
    numbers: list[int]
    string: str


class Config:
    name: str
    reference: SomeRegularClass


if __name__ == '__main__':
    my_config = load_into(Config, "example_from_readme.toml")  # or .json, .yaml

    print(my_config.name)
    # Hello World!
    print(my_config.reference.numbers)
    # [41, 43]


########################
# alternative notation #
########################

class SomeOtherRegularClass:
    number: int
    numbers: list[int]
    string: str


class OtherConfig(TypedConfig):
    name: str
    reference: SomeRegularClass


if __name__ == '__main__':
    my_config = OtherConfig.load("example_from_readme.toml")  # or .json, .yaml

    print(my_config.name)
    # Hello World!
    print(my_config.reference.numbers)
    # [41, 43]

    # TypedConfig has an extra benefit of allowing .update:
    my_config.update(numbers=[68, 70])

More examples can be found in examples.

Supported Config File Types

  • .toml: supports the most types (strings, numbers, booleans, datetime, lists/arrays, dicts/tables);
  • .json: supports roughly the same types as toml (except datetime);
  • .yaml: supports roughly the same types as toml, backwards compatible with JSON;
  • .env: only supports strings. You can use convert_types=True to try to convert to your annotated types;
  • .ini: only supports strings. You can use convert_types=True to try to convert to your annotated types;

For other file types, a custom Loader can be written. See examples/readme.md#Custom File Types

License

configuraptor is distributed under the terms of the MIT license.

Changelog

See CHANGELOG.md

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

configuraptor-1.13.0.tar.gz (536.4 kB view details)

Uploaded Source

Built Distribution

configuraptor-1.13.0-py3-none-any.whl (19.9 kB view details)

Uploaded Python 3

File details

Details for the file configuraptor-1.13.0.tar.gz.

File metadata

  • Download URL: configuraptor-1.13.0.tar.gz
  • Upload date:
  • Size: 536.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.24.1

File hashes

Hashes for configuraptor-1.13.0.tar.gz
Algorithm Hash digest
SHA256 c452b9a34765feddda979a390555ec541551fbee4933b86f7ad22dd44d77d0bf
MD5 7c7ab4f0328824e863a1c2cbcf33f674
BLAKE2b-256 73068a752c94955eac2fdc0182d648496fccbecfbc13f6ac4e52daf1c84c9c9d

See more details on using hashes here.

File details

Details for the file configuraptor-1.13.0-py3-none-any.whl.

File metadata

File hashes

Hashes for configuraptor-1.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 95fce58df0dff0b2ef5699fbcd4af891f75856ad263967fac1fc91725d93b4ae
MD5 d59e4e6972aee135ac3bcb525e3b7813
BLAKE2b-256 7e2eac7fae82c7c94a952f1b9af7dc69fa33351c304f7a9b12d73bba3d67c249

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