Skip to content

Commit

Permalink
Fix flat iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
cifkao committed Jan 13, 2021
1 parent 46cf21f commit 0afacb5
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion muspy/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ class ComplexBase(Base):

def __init__(self, **kwargs):
Base.__init__(self, **kwargs)
self._flat = self._flat_generator()
self._flat = _GeneratorIterable(self._flat_generator)

def __iadd__(
self: ComplexBaseType, other: Union[ComplexBaseType, Iterable]
Expand Down Expand Up @@ -755,3 +755,13 @@ def _flat_generator(self) -> Iterable:
value: list
for _, _, value in self._traverse_lists(attr=None, recursive=True):
yield from value


class _GeneratorIterable:
"""Turns a generator function into a reusable iterable."""

def __init__(self, generator_fn: Callable[[], Iterable]):
self._generator_fn = generator_fn

def __iter__(self):
return self._generator_fn()

0 comments on commit 0afacb5

Please sign in to comment.