4bcf005f4a
* chore: bump specklepy * refactor: requirements files * refactor: dockerfile * refactor: update GitHub actions * refactor: update devcontainer config * docs: updated `README` * docs: better explanations * docs: wording * chore: dependabot config * docs: keeping things in sync * fix: skip pip upgrade * refactor: updated devcontainer * refactor: toml instead of requirements * refactor: update docker * docs: updated `README` * chore: classifiers does nothing * chore: wrap up the v3 transition --------- Co-authored-by: Gergő Jedlicska <gergo@jedlicska.com>
28 lines
800 B
Python
28 lines
800 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]:
|
|
"""Flatten a base object into an iterable of bases.
|
|
|
|
This function recursively traverses the `elements` or `@elements` attribute of the
|
|
base object, yielding each nested base object.
|
|
|
|
Args:
|
|
base (Base): The base object to flatten.
|
|
|
|
Yields:
|
|
Base: Each nested base object in the hierarchy.
|
|
"""
|
|
# Attempt to get the elements attribute, fallback to @elements if necessary
|
|
elements = getattr(base, "elements", getattr(base, "@elements", None))
|
|
|
|
if elements is not None:
|
|
for element in elements:
|
|
yield from flatten_base(element)
|
|
|
|
yield base
|