Skip to main content

Multi-structure YAML config loader 🐍🔌

Project description

setconfig 🔌

[!TIP] Don't forget to star this repo if you like it! ⭐

Some developers prefer to use @dataclass while others prefer BaseModel. This holy war is not going to end soon. So now they can use the same loader and config file in different parts/microservices of one project.

Currently supported:

Installation

pip install setconfig

Usage sample

Dataclass, full sample here

from dataclasses import dataclass
from setconfig import load_config

@dataclass
class Node:
    host: str
    port: int

@dataclass
class Config:
    nodes: list[Node]

config = load_config('config.yaml', into=Config)

print(config)
# >>> Config(nodes=[Node(host='1.1.1.1', port=1000)])
print(config.nodes[0].host)
# >>> '1.1.1.1'

Pydantic, full sample here

from pydantic import BaseModel
from setconfig import load_config

class Node(BaseModel):
    host: str
    port: int

class Config(BaseModel):
    nodes: list[Node]

config = load_config('config.yaml', into=Config)

print(config)
# >>> Config(nodes=[Node(host='1.1.1.1', port=1000)])
print(config.nodes[0].host)
# >>> '1.1.1.1'

SimpleNamespace, full sample here

from setconfig import load_config

config = load_config('config.yaml')

print(config)
# >>> Config(nodes=[Node(host='1.1.1.1', port=1000)])
print(config.nodes[0].host)
# >>> '1.1.1.1'

FAQ

Why only YAML?

There should be one-- and preferably only one --obvious way to do it

(c) Zen of Python

How to load from string/StringIO/etc?

Use load_config_stream

from setconfig import load_config_stream

config = load_config_stream('done: true')

I want to use structure from X package

Create an issue or PR :)

More

PyPI: https://pypi.org/project/setconfig

Repository: https://github.com/abionics/setconfig

Developer: Alex Ermolaev (Abionics)

Email: abionics.dev@gmail.com

License: MIT (see LICENSE.txt)

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

setconfig-1.3.1.tar.gz (4.0 kB view details)

Uploaded Source

File details

Details for the file setconfig-1.3.1.tar.gz.

File metadata

  • Download URL: setconfig-1.3.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for setconfig-1.3.1.tar.gz
Algorithm Hash digest
SHA256 17a685100dfaf46098bbf01a7e5511f275ffb6cbd173b19bbf259069df9a1789
MD5 abe65962065cfa6be60b25e433047306
BLAKE2b-256 583eb4038f029efc8c15f3a77834af804d0e9f320676201e42339ccb532e8386

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