Skip to content

Commit

Permalink
Reuse state iteration of wrapped collections
Browse files Browse the repository at this point in the history
  • Loading branch information
cverstege committed Nov 26, 2024
1 parent afef50f commit 7a890bd
Showing 1 changed file with 12 additions and 33 deletions.
45 changes: 12 additions & 33 deletions law/target/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,39 +439,18 @@ def _iter_state(
if basenames is None:
basenames = self._get_basenames()

# helper to check for existence
def exists(t, _basenames):
if optional_existing is not None and t.optional:
return optional_existing
if isinstance(t, SiblingFileCollectionBase):
return t._exists_fwd(
basenames=_basenames,
optional_existing=optional_existing,
)
if isinstance(t, TargetCollection):
return all(exists(_t for _t in flatten_collections(t)))
return t.basename in _basenames

# loop and yield
if keys:
for key, targets in self._iter_flat():
state = all(exists(t, basenames[self._flat_target_collections[t]]) for t in targets)
if state is existing:
if unpack:
targets = self.targets[key]
yield (key, targets) if keys else targets
else: # heavy speedup
for collection in self.collections:
collection_iterator = collection._iter_state(
existing=existing,
optional_existing=optional_existing,
basenames=basenames[collection],
keys=keys,
unpack=unpack
)
for element in collection_iterator:
yield element

# reuse state iteration of wrapped collections
for coll in self.collections:
iter_kwargs = {
"existing": existing,
"optional_existing": optional_existing,
"keys": keys,
"unpack": unpack,
}
if isinstance(coll, SiblingFileCollectionBase) and coll in basenames:
iter_kwargs["basenames"] = basenames[coll]
for obj in coll._iter_state(**iter_kwargs):
yield obj

def _exists_fwd(self, **kwargs):
fwd = [("basenames", "basenames_dict"), ("optional_existing", "optional_existing")]
Expand Down

0 comments on commit 7a890bd

Please sign in to comment.