You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

_data_source.py 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import os
  2. import typing as _typing
  3. class OnlineDataSource:
  4. @property
  5. def _raw_directory(self) -> str:
  6. return os.path.join(self.__path, "raw")
  7. @property
  8. def _processed_directory(self) -> str:
  9. return os.path.join(self.__path, "processed")
  10. @property
  11. def _raw_filenames(self) -> _typing.Iterable[str]:
  12. raise NotImplementedError
  13. @property
  14. def _processed_filenames(self) -> _typing.Iterable[str]:
  15. raise NotImplementedError
  16. @property
  17. def _raw_file_paths(self) -> _typing.Iterable[str]:
  18. return [
  19. os.path.join(self._raw_directory, raw_filename)
  20. for raw_filename in self._raw_filenames
  21. ]
  22. @property
  23. def _processed_file_paths(self) -> _typing.Iterable[str]:
  24. return [
  25. os.path.join(self._processed_directory, processed_filename)
  26. for processed_filename in self._processed_filenames
  27. ]
  28. @classmethod
  29. def __files_exist(cls, files: _typing.Iterable[str]) -> bool:
  30. return all([os.path.exists(file) for file in files])
  31. @classmethod
  32. def __make_directory(cls, path):
  33. os.makedirs(os.path.expanduser(os.path.normpath(path)), exist_ok=True)
  34. def _fetch(self):
  35. raise NotImplementedError
  36. def __fetch(self):
  37. if not self.__files_exist(self._raw_file_paths):
  38. self.__make_directory(self._raw_directory)
  39. self._fetch()
  40. def _process(self):
  41. raise NotImplementedError
  42. def __preprocess(self):
  43. if not self.__files_exist(self._processed_file_paths):
  44. self.__make_directory(self._processed_directory)
  45. self._process()
  46. def __getitem__(self, index: int) -> _typing.Any:
  47. raise NotImplementedError
  48. def __len__(self) -> int:
  49. raise NotImplementedError
  50. def __init__(self, path: str):
  51. self.__path: str = os.path.expanduser(os.path.normpath(path))
  52. self.__fetch()
  53. self.__preprocess()