File size: 1,024 Bytes
20bc01d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import io


def comment_filtered_lines(path: str):
    with open(path, encoding="utf-8") as f:
        for line in f:
            if not line.lstrip().startswith("#"):
                yield line


class LineIteratorIO(io.TextIOBase):
    def __init__(self, iterator):
        self._it = iter(iterator)
        self._buf = ""

    def readable(self):
        return True

    def read(self, size=-1):
        # size<0 のときはEOFまで貯めて返す
        if size is None or size < 0:
            try:
                for chunk in self._it:
                    self._buf += chunk
            except StopIteration:
                pass
            out, self._buf = self._buf, ""
            return out

        # size 指定ありのときは、必要分だけバッファを満たす
        while len(self._buf) < size:
            try:
                self._buf += next(self._it)
            except StopIteration:
                break

        out, self._buf = self._buf[:size], self._buf[size:]
        return out