Skip to content

Commit c2bdb38

Browse files
authored
Use Pydantic Dataclasses and Features if available (#9)
1 parent 4366ff5 commit c2bdb38

14 files changed

+870
-4
lines changed

.github/workflows/unittests.yml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ jobs:
1010
strategy:
1111
matrix:
1212
python-version: ["3.11", "3.12"]
13+
pydantic: [true, false]
1314
os: [ubuntu-latest]
1415
steps:
1516
- uses: actions/checkout@v4
@@ -21,6 +22,9 @@ jobs:
2122
run: |
2223
python -m pip install --upgrade pip
2324
pip install tox
25+
- name: install pydantic if requested
26+
if: matrix.run_step == 'true'
27+
run: pip install -r dev_requirements/requirements-pydantic.txt
2428
- name: Run the Unit Tests via Tox
2529
run: |
2630
tox -e tests

README.md

+48
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,54 @@ assert {awf.pruefidentifikator for awf in ahb.anwendungsfaelle} == {
7979

8080
Die vollständigen Beispiele finden sich in den [unittests](unittests).
8181

82+
### Verwendung mit Pydantic
83+
Per default verwendet fundamend die [dataclasses aus der Python-Standardlibrary](https://docs.python.org/3/library/dataclasses.html).
84+
Es lässt sich aber auch direkt mit [Pydantic](https://docs.pydantic.dev/latest/) und den [Pydantic dataclasses](https://docs.pydantic.dev/2.7/concepts/dataclasses/) verwenden.
85+
Wenn entweder pydantic schon installiert ist, oder mittels
86+
```bash
87+
pip install fundamend[pydantic]
88+
```
89+
mit installiert wird, dann sind Datenmodelle, die von `AhbReader` und `MigReader` zurückgegeben werden, automatisch pydantic Objekte.
90+
91+
Mit Pydantic können die Ergebnisse auch leicht bspw. als JSON exportiert werden:
92+
```python
93+
from pathlib import Path
94+
95+
from pydantic import RootModel
96+
from fundamend import Anwendungshandbuch, AhbReader
97+
98+
ahb = AhbReader(Path("UTILTS_AHB_1.1d_Konsultationsfassung_2024_04_02.xml")).read()
99+
ahb_json = RootModel[Anwendungshandbuch](ahb).model_dump(mode="json")
100+
```
101+
102+
Das Ergebnis sieht dann so aus:
103+
```json
104+
{
105+
"veroeffentlichungsdatum": "2024-04-02",
106+
"autor": "BDEW",
107+
"versionsnummer": "1.1d",
108+
"anwendungsfaelle": [
109+
{
110+
"pruefidentifikator": "25001",
111+
"beschreibung": "Berechnungsformel",
112+
"kommunikation_von": "NB an MSB / LF",
113+
"format": "AWF",
114+
"segments": [
115+
{
116+
"id": "UNH",
117+
"name": "Nachrichten-Kopfsegment",
118+
"number": "00001",
119+
"ahb_status": "Muss",
120+
"data_elements": [
121+
{
122+
"id": "D_0062",
123+
"name": "Nachrichten-Referenznummer",
124+
"codes": []
125+
},
126+
```
127+
128+
### JSON Schemas
129+
Das fundamend Datenmodell ist auch als JSON Schema verfügbar: [`json_schemas`](json_schemas).
82130

83131
## Verwendung und Mitwirken
84132
Der Code ist MIT-lizenziert und kann daher frei verwendet werden.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pydantic
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#
2+
# This file is autogenerated by pip-compile with Python 3.11
3+
# by the following command:
4+
#
5+
# pip-compile requirements-pydantic.in
6+
#
7+
annotated-types==0.7.0
8+
# via pydantic
9+
pydantic==2.7.2
10+
# via -r requirements-pydantic.in
11+
pydantic-core==2.18.3
12+
# via pydantic
13+
typing-extensions==4.12.0
14+
# via
15+
# pydantic
16+
# pydantic-core

dev_requirements/requirements-type_check.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ mypy==1.10.0
99
# via -r dev_requirements/requirements-type_check.in
1010
mypy-extensions==1.0.0
1111
# via mypy
12-
typing-extensions==4.10.0
12+
typing-extensions==4.12.0
1313
# via mypy

0 commit comments

Comments
 (0)