diff --git a/capellambse/model/common/element.py b/capellambse/model/common/element.py index fa0431863..2f32713b3 100644 --- a/capellambse/model/common/element.py +++ b/capellambse/model/common/element.py @@ -404,12 +404,24 @@ def _repr_html_(self) -> str: def get_children(self) -> MixedElementList: """Return all model elements that are children of this one. - The list returned from this method contains all model element - that are direct descendants of this element, with regard to the - XML hierarchy (usually referred to as "owned objects" in the - metamodel). + .... seealso:: + + :meth:`iter_children` + """ + return MixedElementList( + self._model, [ele._element for ele in self.iter_children()] + ) + + def has_children(self) -> bool: + return next(self.iter_children(), None) is not None + + def iter_children(self) -> t.Iterator[GenericElement]: + """Yield model elements that are children of this one. + + The elements yielded from this generator are the direct + descendants of this element, with regard to the XML hierarchy + (usually referred to as "owned objects" in the metamodel). """ - elements: list[etree._Element] = [] for attr in dir(self): if attr.startswith("_"): continue @@ -422,10 +434,9 @@ def get_children(self) -> MixedElementList: ): if acc.aslist is None: if getattr(self, attr) is not None: - elements.append(getattr(self, attr)._element) + yield getattr(self, attr) else: - elements.extend(i._element for i in getattr(self, attr)) - return MixedElementList(self._model, elements) + yield from getattr(self, attr) if t.TYPE_CHECKING: @@ -782,7 +793,7 @@ def __html__(self) -> markupsafe.Markup: return markupsafe.Markup("
(Empty list)
") fragments = ['