18 from typing
import TypeVar, Generic, Union
19 from abc
import ABC, abstractmethod
20 from zipfile
import BadZipFile, ZipFile
21 from pathlib
import Path
26 SampleDataType = TypeVar(
"SampleDataType")
29 class Sample(ABC, Generic[SampleDataType]):
32 Represents the generic class Sample
33 Includes methods that can be used by any instance of Sample
34 and abstract methods that must be implemented by any subclass
41 def path(self) -> Path:
46 def zipPath(self) -> Path:
50 def download(self, decrypt: bool =
True, ignoreCache: bool =
False) ->
None:
52 Downloads the Sample if it is an instance or a subclass of NetworkSample
53 Ignored for instances and subclasses of LocalSample
58 def __unzipSample(self) -> None:
59 if self.
pathpath.exists():
60 shutil.rmtree(self.
pathpath)
62 with ZipFile(self.
zipPathzipPath)
as zipFile:
63 zipFile.extractall(self.
pathpath)
65 def unzip(self, ignoreCache: bool =
False) ->
None:
72 if set to false performs unzip action even if
73 sample is previously unzipped
76 if not self.
zipPathzipPath.exists():
77 raise FileNotFoundError(f
"Sample \"{self.name}\" archive does not exist at path \"{self.zipPath}\"")
79 if self.
pathpath.exists()
and not ignoreCache:
95 def load(self) -> SampleDataType:
98 def joinPath(self, other: Union[Path, str]) -> Path:
100 Joins sample path and provided path
104 other : Union[Path, str]
109 Path -> path created from sample path and provided path
113 >>> print(sampleObj.joinPath("dummy.zip"))
114 Path("path/to/sample/dummy.zip")
117 if isinstance(other, str):
120 return self.path / other
122 def _updateArchive(self) -> None:
123 oldZipPath = self.zipPath.parent / f
"{self.zipPath.stem}-old.zip"
124 self.zipPath.rename(oldZipPath)
126 with ZipFile(self.zipPath,
"w")
as zipFile:
127 for value
in self.path.rglob(
"*"):
128 if not value.is_file():
131 zipFile.write(value, value.relative_to(self.path))
None download(self, bool decrypt=True, bool ignoreCache=False)
Path joinPath(self, Union[Path, str] other)
None unzip(self, bool ignoreCache=False)