Files
Jonathon Broughton bcdf172982 Update flatten.py
2024-03-12 08:39:45 +00:00

16 lines
557 B
Python

"""Helper module for a simple speckle object tree flattening."""
from collections.abc import Iterable
from specklepy.objects import Base
def flatten_base(base: Base) -> Iterable[Base]:
"""Take a base and flatten it to an iterable of bases."""
if hasattr(base, "elements") and base.elements is not None:
# Check if base.elements is not only present and non-None, but also an iterable
if isinstance(base.elements, Iterable):
for element in base.elements:
yield from flatten_base(element)
yield base