AbsReaderWriter.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334
  1. from abc import ABC, abstractmethod
  2. class AbsReaderWriter(ABC):
  3. """
  4. 同时支持二进制和文本读写的抽象类
  5. """
  6. MODE_TXT = "text"
  7. MODE_BIN = "binary"
  8. def __init__(self, parent_path):
  9. # 初始化代码可以在这里添加,如果需要的话
  10. self.parent_path = parent_path # 对于本地目录是父目录,对于s3是会写到这个apth下。
  11. @abstractmethod
  12. def read(self, path: str, mode="text"):
  13. """
  14. 无论对于本地还是s3的路径,检查如果path是绝对路径,那么就不再 拼接parent_path, 如果是相对路径就拼接parent_path
  15. """
  16. raise NotImplementedError
  17. @abstractmethod
  18. def write(self, content: str, path: str, mode=MODE_TXT):
  19. """
  20. 无论对于本地还是s3的路径,检查如果path是绝对路径,那么就不再 拼接parent_path, 如果是相对路径就拼接parent_path
  21. """
  22. raise NotImplementedError
  23. @abstractmethod
  24. def read_jsonl(self, path: str, byte_start=0, byte_end=None, encoding='utf-8'):
  25. """
  26. 无论对于本地还是s3的路径,检查如果path是绝对路径,那么就不再 拼接parent_path, 如果是相对路径就拼接parent_path
  27. """
  28. raise NotImplementedError