From 37364d9cfa8cf31bead8779de2e7785ef2b67102 Mon Sep 17 00:00:00 2001 From: Wim Pomp Date: Wed, 3 Apr 2024 14:56:06 +0200 Subject: [PATCH] - CachedPath bugfix, try 3 --- ndbioimage/__init__.py | 37 +++++++++++-------------------------- pyproject.toml | 2 +- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/ndbioimage/__init__.py b/ndbioimage/__init__.py index 28ac758..1267c95 100755 --- a/ndbioimage/__init__.py +++ b/ndbioimage/__init__.py @@ -133,26 +133,6 @@ class Shape(tuple): 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 """ @@ -169,14 +149,19 @@ class OmeCache(DequeDict): 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, key: Path | CachedPath, value: OME) -> None: - super().__setitem__(CachedPath(key), value) + def __setitem__(self, path: Path, value: OME) -> None: + super().__setitem__(self.path_and_lstat(path), value) - def __contains__(self, item: Path | CachedPath) -> bool: - return super().__contains__(CachedPath(item)) + def __contains__(self, path: Path) -> bool: + return super().__contains__(self.path_and_lstat(path)) + + @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): diff --git a/pyproject.toml b/pyproject.toml index 0b6f830..9d2703d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 "] license = "GPLv3"