Skip to content

Commit

Permalink
Merge pull request #195 from cverstege/NestedSiblingFileCollection_Sp…
Browse files Browse the repository at this point in the history
…eedup

Speedup NestedSiblinFileCollection when possible
  • Loading branch information
riga authored Nov 26, 2024
2 parents 540f48f + 7a890bd commit c4bd0c4
Showing 1 changed file with 12 additions and 20 deletions.
32 changes: 12 additions & 20 deletions law/target/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,26 +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
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
# 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 c4bd0c4

Please sign in to comment.