AbsReaderWriter.py 1.26 KB
Newer Older
kernel.h@qq.com's avatar
kernel.h@qq.com committed
1
2
3
4
5
6
7
from abc import ABC, abstractmethod


class AbsReaderWriter(ABC):
    """
    同时支持二进制和文本读写的抽象类
    """
kernel.h@qq.com's avatar
kernel.h@qq.com committed
8
9
    MODE_TXT = "text"
    MODE_BIN = "binary"
liukaiwen's avatar
liukaiwen committed
10

kernel.h@qq.com's avatar
kernel.h@qq.com committed
11
    def __init__(self, parent_path):
liukaiwen's avatar
liukaiwen committed
12
        # 初始化代码可以在这里添加,如果需要的话
kernel.h@qq.com's avatar
kernel.h@qq.com committed
13
        self.parent_path = parent_path # 对于本地目录是父目录,对于s3是会写到这个apth下。
liukaiwen's avatar
liukaiwen committed
14

kernel.h@qq.com's avatar
kernel.h@qq.com committed
15
    @abstractmethod
liukaiwen's avatar
liukaiwen committed
16
    def read(self, path: str, mode="text"):
kernel.h@qq.com's avatar
kernel.h@qq.com committed
17
18
19
20
        """
        无论对于本地还是s3的路径,检查如果path是绝对路径,那么就不再 拼接parent_path, 如果是相对路径就拼接parent_path
        """
        raise NotImplementedError
kernel.h@qq.com's avatar
kernel.h@qq.com committed
21
22

    @abstractmethod
kernel.h@qq.com's avatar
kernel.h@qq.com committed
23
24
25
26
27
    def write(self, content: str, path: str, mode=MODE_TXT):
        """
        无论对于本地还是s3的路径,检查如果path是绝对路径,那么就不再 拼接parent_path, 如果是相对路径就拼接parent_path
        """
        raise NotImplementedError
liukaiwen's avatar
liukaiwen committed
28

kernel.h@qq.com's avatar
kernel.h@qq.com committed
29
30
31
32
33
34
    @abstractmethod
    def read_jsonl(self, path: str, byte_start=0, byte_end=None, encoding='utf-8'):
        """
        无论对于本地还是s3的路径,检查如果path是绝对路径,那么就不再 拼接parent_path, 如果是相对路径就拼接parent_path
        """
        raise NotImplementedError