abionics / setconfig

Multi-structure YAML config loader πŸπŸ”Œ

Home Page:https://pypi.org/project/setconfig

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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', data_class=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', data_class=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)

About

Multi-structure YAML config loader πŸπŸ”Œ

https://pypi.org/project/setconfig

License:MIT License


Languages

Language:Python 100.0%