Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stefanofusai/pyamldantic
Validate and serialize YAML config files with Pydantic
https://github.com/stefanofusai/pyamldantic
pydantic yaml
Last synced: 25 days ago
JSON representation
Validate and serialize YAML config files with Pydantic
- Host: GitHub
- URL: https://github.com/stefanofusai/pyamldantic
- Owner: stefanofusai
- License: mit
- Created: 2024-09-30T10:21:12.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-12-28T01:46:58.000Z (30 days ago)
- Last Synced: 2024-12-28T02:24:13.273Z (30 days ago)
- Topics: pydantic, yaml
- Language: Python
- Homepage:
- Size: 87.9 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# pyamldantic
Validate and serialize YAML config files with Pydantic, with support for environment variables.
This package uses [uv](https://docs.astral.sh/uv/) for project management. To get started, ensure that **uv** is installed on your machine and updated to the `0.5.6` version. Detailed installation instructions for **uv** can be found [here](https://docs.astral.sh/uv/getting-started/installation/).
## Installation
```bash
uv add pyamldantic
```## Usage
`config.yaml`
```yaml
database:
host: $DATABASE_HOST
name: $DATABASE_NAME
password: $DATABASE_PASSWORD
port: $DATABASE_PORT
user: $DATABASE_USER
timeout: $DATABASE_TIMEOUT?
environment: development
is_debug: true
````config.py`
```python
from pyamldantic import YAMLConfig
from pydantic import BaseModel, SecretStrclass DatabaseSchema(BaseModel):
host: str
name: str
password: SecretStr
port: int
user: str
ssl: bool = False
timeout: int | None = Noneclass Schema(BaseModel):
database: DatabaseSchema
environment: str
is_debug: boolconfig = YAMLConfig.load("config.yaml", schema=Schema)
````main.py`
```python
from .config import configif __name__ == "__main__":
print(f"Initializing {config.environment} environment...")
...
```## Development
```bash
uv sync --frozen --group=development
uv run --frozen pre-commit install --install-hooks
uv run --frozen pre-commit install --hook-type=commit-msg
```## Testing
```bash
uv sync --frozen --group=testing
uv run --frozen pytest
```## Acknowledgments
This project was inspired by [envyaml](https://github.com/thesimj/envyaml).
## Contributing
Contributions are welcome! To get started, please refer to our [contribution guidelines](https://github.com/stefanofusai/pyamldantic/blob/main/CONTRIBUTING.md).
## Issues
If you encounter any problems while using this package, please open a new issue [here](https://github.com/stefanofusai/pyamldantic/issues).