Pydantic supported saving and loading prepared

This commit is contained in:
2026-05-19 16:16:14 +02:00
parent 9e8893b4b5
commit e46e494500
2 changed files with 821 additions and 565 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -6,6 +6,7 @@ from collections.abc import Sequence
from typing import Any from typing import Any
import babel import babel
from pydantic import BaseModel
from PySide6.QtCore import QDate, Qt from PySide6.QtCore import QDate, Qt
# %% # %%
@@ -52,21 +53,76 @@ new_content = {
} }
} }
new_content = {
"Stammdaten": {
"Stammdaten_anzahl_kinder-[0]": {"Stammdaten_anzahl_kinder": "5"},
"Stammdaten_anzahl_kinder-[1]": {"Stammdaten_alter_kinder": None},
"Stammdaten_anzahl_kinder-[2]": {"Stammdaten_alter_kinder": None},
"Stammdaten_anzahl_kinder-[3]": {"Stammdaten_alter_kinder": None},
"Stammdaten_anzahl_kinder-[4]": {"Stammdaten_alter_kinder": None},
"Stammdaten_anzahl_kinder-[5]": {"Stammdaten_alter_kinder": None},
}
}
# object Stammdaten_Anzahl_Kinder: Stammdaten_anzahl_kinder: int, Stammdaten_alter_kinder: list[int]
def flat_dict(contents):
for x in contents: def get_leafs(data):
if isinstance(contents, dict): if isinstance(data, dict):
yield from flat_dict(tuple(contents[x])) for value in data.values():
elif isinstance(x, (list, tuple, set)): yield from get_leafs(value)
yield from flat_dict(x) elif isinstance(data, (list, tuple, set)):
for item in data:
yield from get_leafs(item)
else: else:
yield x yield data
def get_leaf_dicts(data):
if isinstance(data, dict):
has_inner_dicts = False
for value in data.values():
for inner_dict in get_leaf_dicts(value):
has_inner_dicts = True
yield inner_dict
if not has_inner_dicts:
yield data
elif isinstance(data, (list, tuple, set)):
for item in data:
yield from get_leaf_dicts(item)
# %% # %%
for x in flat_dict(new_content): for x in get_leafs(new_content):
print(x) print(x)
# %%
export_dict = {}
children_values: list[str] | None = None
for idx, data_dict in enumerate(get_leaf_dicts(new_content)):
if idx == 0:
export_dict.update(data_dict)
else:
for key in data_dict:
if key not in export_dict:
children_values = export_dict.setdefault(key, [])
assert children_values is not None
children_values.append(data_dict[key])
export_dict
# %%
class Stammdaten_AnzahlKinder(BaseModel):
Stammdaten_anzahl_kinder: int | None
Stammdaten_alter_kinder: list[int | None]
# %%
Stammdaten_AnzahlKinder(**export_dict)
# %% # %%
find_dynamic_content(dynamic_content) find_dynamic_content(dynamic_content)