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.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. import errno
  34. try:
  35. os.makedirs(os.path.expanduser(os.path.normpath(path)))
  36. except OSError as e:
  37. if e.errno != errno.EEXIST and os.path.isdir(path):
  38. raise e
  39. def _fetch(self):
  40. raise NotImplementedError
  41. def __fetch(self):
  42. if not self.__files_exist(self._raw_file_paths):
  43. self.__make_directory(self._raw_directory)
  44. self._fetch()
  45. def _process(self):
  46. raise NotImplementedError
  47. def __preprocess(self):
  48. if not self.__files_exist(self._processed_file_paths):
  49. self.__make_directory(self._processed_directory)
  50. self._process()
  51. def __getitem__(self, index: int) -> _typing.Any:
  52. raise NotImplementedError
  53. def __len__(self) -> int:
  54. raise NotImplementedError
  55. def __init__(
  56. self, path: str,
  57. # transform: _typing.Optional[_typing.Callable[[_typing.Any], _typing.Any]] = ...
  58. ):
  59. self.__path: str = os.path.expanduser(os.path.normpath(path))
  60. # self.__transform: _typing.Optional[_typing.Callable[[_typing.Any], _typing.Any]] = (
  61. # transform if transform not in (Ellipsis, None) and callable(transform) else None
  62. # )
  63. self.__fetch()
  64. self.__preprocess()