Skip to content

Commit

Permalink
- CachedPath bugfix, try 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Wim Pomp committed Apr 3, 2024
1 parent 153355d commit 37364d9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 27 deletions.
37 changes: 11 additions & 26 deletions ndbioimage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,6 @@ def yxczt(self) -> tuple[int, int, int, int, int]:
return tuple(self[i] for i in 'yxczt') # type: ignore


class CachedPath(WindowsPath if os.name == 'nt' else PosixPath):
""" helper class for checking whether a file has changed, used by OmeCache """

def __init__(self, path: Path | str) -> None:
super().__init__(path)
if self.exists():
self._lstat = super().lstat() # save file metadata like creation time etc.
else:
self._lstat = None

def __eq__(self, other: Path | CachedPath) -> bool:
return super().__eq__(other) and self.lstat() == other.lstat()

def __hash__(self) -> int:
return hash((super().__hash__(), self.lstat()))

def lstat(self):
return self._lstat


class OmeCache(DequeDict):
""" prevent (potentially expensive) rereading of ome data by caching """

Expand All @@ -169,14 +149,19 @@ def __init__(self) -> None:
def __reduce__(self) -> tuple[type, tuple]:
return self.__class__, ()

def __getitem__(self, item: Path | CachedPath) -> OME:
return super().__getitem__(CachedPath(item))
def __getitem__(self, path: Path) -> OME:
return super().__getitem__(self.path_and_lstat(path))

def __setitem__(self, path: Path, value: OME) -> None:
super().__setitem__(self.path_and_lstat(path), value)

def __setitem__(self, key: Path | CachedPath, value: OME) -> None:
super().__setitem__(CachedPath(key), value)
def __contains__(self, path: Path) -> bool:
return super().__contains__(self.path_and_lstat(path))

def __contains__(self, item: Path | CachedPath) -> bool:
return super().__contains__(CachedPath(item))
@staticmethod
def path_and_lstat(path: str | Path) -> tuple[Path, Optional[os.stat_result]]:
path = Path(path)
return path, (path.lstat() if path.exists() else None)


class Imread(np.lib.mixins.NDArrayOperatorsMixin, ABC):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "ndbioimage"
version = "2024.4.2"
version = "2024.4.3"
description = "Bio image reading, metadata and some affine registration."
authors = ["W. Pomp <w.pomp@nki.nl>"]
license = "GPLv3"
Expand Down

0 comments on commit 37364d9

Please sign in to comment.