Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance robustness of the basic flatten command #20

Merged
merged 1 commit into from
May 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions flatten.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,22 @@


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:
for element in base["elements"]:
"""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