setconfig 🔌
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:
-
@dataclass - Pydantic
BaseModel - Python
SimpleNamespace(dotted dict)
Features:
- Loading from streams
- Multiple config files
- Value overriding
- Tuple-friendly
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'
Features
Loading from string/StringIO/etc
from setconfig import load_config_stream
config = load_config_stream('done: true')
Multiple config files, full sample here
config = load_config('config.base.yaml', 'config.dev.yaml', 'config.feature-x.yaml', into=Config)
Configs are processed in the order they are passed to load_config (from left to right), where
last overrides the previous ones
Value overriding, full sample here
config = load_config('config.yaml', into=Config, override={'timeout': 10})
Extra parsing params
config = load_config('config.yaml', into=Config, check_types=False)
Where check_types is a dacite flag, see https://github.com/konradhalas/dacite#type-checking
Tuple-friendly
There is known issue in dacite that raises type error
when loading list into tuple. Pull request with fix is ready
since May 2023, but not merged yet... That's why setconfig has its own fix
FAQ
Why only YAML?
There should be one-- and preferably only one --obvious way to do it
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)
Metadata
Release files for setconfig 1.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| setconfig-1.4.1.tar.gz | 4.9 kB | Without provenance | Details for setconfig-1.4.1.tar.gz |
Release files / setconfig-1.4.1.tar.gz
| Download URL | setconfig-1.4.1.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fc2f8e5dc2d640ae38e5e9376bbe8809041d62e1322150b8481e4fe0f333245b
|
|
BLAKE2b-256 checksum How to use checksums |
935b274ad022ccee6879332e8aa8c6a41194f544807662bb9510c5f10064d947
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.10.6
|
Security for setconfig 1.4.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.