Iterate over chunks of the open file ``fd`` delimited by ``sep``. Does not trim.
(fd, sep=None, read_size=4096)
| 174 | |
| 175 | |
| 176 | def iter_separated(fd, sep=None, read_size=4096): |
| 177 | """Iterate over chunks of the open file ``fd`` delimited by ``sep``. Does not trim.""" |
| 178 | buf = fd.read(read_size) |
| 179 | is_str = isinstance(buf, str) |
| 180 | part = "" if is_str else b"" |
| 181 | sep = sep or ("\n" if is_str else b"\n") |
| 182 | while len(buf) > 0: |
| 183 | part2, *items = buf.split(sep) |
| 184 | *full, part = (part + part2, *items) # type: ignore |
| 185 | yield from full |
| 186 | buf = fd.read(read_size) |
| 187 | # won't yield an empty part if stream ended with `sep` |
| 188 | # or if there was no data before EOF |
| 189 | if len(part) > 0: # type: ignore[arg-type] |
| 190 | yield part |