# %% import dataclasses as dc import enum import re from collections.abc import Sequence from typing import Any import babel from PySide6.QtCore import QDate, Qt # %% DYNAMIC_LIST_KEY_PATTERN = re.compile(r"-\[(\d+)\]") dynamic_content = { "Stammdaten_anzahl_kinder-[0]": {"Stammdaten_anzahl_kinder": "5"}, "Stammdaten_anzahl_kinder-[1]": {"Stammdaten_alter_kinder": "23213"}, "Stammdaten_anzahl_kinder-[2]": {"Stammdaten_alter_kinder": "123123"}, "Stammdaten_anzahl_kinder-[3]": {"Stammdaten_alter_kinder": "123213"}, "Stammdaten_anzahl_kinder-[4]": {"Stammdaten_alter_kinder": "123123"}, "Stammdaten_anzahl_kinder-[5]": {"Stammdaten_alter_kinder": "123123"}, } def find_dynamic_content(content: dict[str, Any]) -> dict[str, Any] | None: found = None for key in dynamic_content.keys(): if DYNAMIC_LIST_KEY_PATTERN.search(key): # found an match: this is dynamic content dictionary print("found") found = dynamic_content break return found # %% new_content = { "Stammdaten": { "Stammdaten_PLZ": "", "Stammdaten_anrede_anschrift": "asdasdas", "Stammdaten_anzahl_kinder": [ { "Stammdaten_anzahl_kinder-[0]": {"Stammdaten_anzahl_kinder": "5"}, "Stammdaten_anzahl_kinder-[1]": {"Stammdaten_alter_kinder": "23213"}, "Stammdaten_anzahl_kinder-[2]": {"Stammdaten_alter_kinder": "123123"}, "Stammdaten_anzahl_kinder-[3]": {"Stammdaten_alter_kinder": "123213"}, "Stammdaten_anzahl_kinder-[4]": {"Stammdaten_alter_kinder": "123123"}, "Stammdaten_anzahl_kinder-[5]": {"Stammdaten_alter_kinder": "123123"}, } ], } } def flat_dict(contents): for x in contents: if isinstance(contents, dict): yield from flat_dict(tuple(contents[x])) elif isinstance(x, (list, tuple, set)): yield from flat_dict(x) else: yield x # %% for x in flat_dict(new_content): print(x) # %% find_dynamic_content(dynamic_content) # %% @dc.dataclass(slots=True) class CountryList: iso_to_country: dict[str, str] for_dropdown: Sequence[tuple[str, str]] def get_country_list_german() -> CountryList: locale = babel.Locale("de", "DE") countries: list[tuple[str, str]] = [] iso_to_country: dict[str, str] = {} for iso_code, country_name in locale.territories.items(): if len(iso_code) == 2 and not iso_code.isdigit(): countries.append((country_name, iso_code)) iso_to_country[iso_code] = country_name countries.sort(key=lambda x: x[0]) return CountryList( iso_to_country=iso_to_country, for_dropdown=tuple(countries), ) # %% laender_liste # %% DYNAMIC_LIST_KEY_PATTERN = r"-\[(\d+)\]" key = "Schulbildung-[12].7b8da0f7-7a0e-4f71-878a-85616099e849" matches = re.search(DYNAMIC_LIST_KEY_PATTERN, key) # %% matches # %% matches.group(1) # %% class COUNTRY(enum.IntEnum): DE = 1 FR = 2 CM = 3 class COUNTRY2(enum.Enum): DE = 1 FR = 2 CM = 3 def give_value(t): print(f"Wert ist: {t}") give_value(COUNTRY.DE) give_value(COUNTRY2.DE) # %% COUNTRY(10) # %% COUNTRY.DE # %% t_str = "asd.yxcxc.dfgjj.aasdsdsdsd.sdsdsdsd" splitted = t_str.split(".") part, rest = splitted[0], splitted[1:] part # %% ".".join([part] + rest) # %% class FormFieldType(enum.StrEnum): TEXT = enum.auto() LONGTEXT = enum.auto() DATE = enum.auto() DATETIME = enum.auto() @dc.dataclass(slots=True) class FormField: key: str label: str type: FormFieldType required: bool def __post_init__(self) -> None: self.label = self.label.strip() if not self.label.endswith(":"): self.label += ":" if self.required: self.label += "*" # %% FormField("name", "Projektbeschreibung", FormFieldType.LONGTEXT, required=True) # %% FormField("name", "Projektbeschreibung:", FormFieldType.LONGTEXT, required=True) # %% FormField("name", "Projektbeschreibung", FormFieldType.LONGTEXT, required=False) # %% FormField("name", "Projektbeschreibung:", FormFieldType.LONGTEXT, required=False) # %% addr.export() # %% set_date = QDate.fromString("26.07.2026", "dd.MM.yyyy") # %% Qt.Tet