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