Skip to content

Commit

Permalink
pathmapper: don't use temporary lists
Browse files Browse the repository at this point in the history
Should speed up items_exclude_children() a bit
  • Loading branch information
mr-c committed Dec 1, 2023
1 parent d9dff8c commit c0e6f1f
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions cwltool/pathmapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
import stat
import urllib
import uuid
from pathlib import Path
from typing import Dict, Iterator, List, Optional, Tuple, cast
from typing import (
Dict,
ItemsView,
Iterable,
Iterator,
KeysView,
List,
Optional,
Tuple,
cast,
)

from mypy_extensions import mypyc_attr
from schema_salad.exceptions import ValidationException
Expand Down Expand Up @@ -210,21 +219,30 @@ def mapper(self, src: str) -> MapperEnt:
return MapperEnt(p.resolved, p.target + src[i:], p.type, p.staged)
return self._pathmap[src]

def files(self) -> List[str]:
return list(self._pathmap.keys())
def files(self) -> KeysView[str]:
"""Return a dictionary keys view of locations."""
return self._pathmap.keys()

def items(self) -> List[Tuple[str, MapperEnt]]:
return list(self._pathmap.items())
def items(self) -> ItemsView[str, MapperEnt]:
"""Return a dictionary items view."""
return self._pathmap.items()

def items_exclude_children(self) -> List[Tuple[str, MapperEnt]]:
def items_exclude_children(self) -> ItemsView[str, MapperEnt]:
"""Return a dictionary items view minus any entries which are childern of other entries."""
newitems = {}
keys = [key for key, entry in self.items()]
keys = self.files()

def parents(path: str) -> Iterable[str]:
result = key
while result != "":
result = os.path.dirname(result)
yield result

for key, entry in self.items():
parents = Path(key).parents
if any([Path(key_) in parents for key_ in keys]):
if not keys.isdisjoint(parents(key)):
continue
newitems[key] = entry
return list(newitems.items())
return newitems.items()

def reversemap(
self,
Expand Down

0 comments on commit c0e6f1f

Please sign in to comment.