Last active
September 3, 2022 12:58
-
-
Save meyer1994/8406b1f766308e9474a349fbac3d4d79 to your computer and use it in GitHub Desktop.
Bytes iterator to file object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import io | |
import itertools | |
class IteratorIO(io.RawIOBase): | |
def __init__(self, iterator: [bytes]): | |
super(IteratorIO, self).__init__() | |
self.iterator = iterator | |
def read(self, n: int = -1) -> bytes: | |
if n == -1: | |
return b''.join(self.iterator) | |
data = b'' | |
i = n | |
while i > 0: | |
try: | |
chunk = next(self.iterator) | |
i -= len(chunk) | |
data += chunk | |
except StopIteration: | |
return data | |
last = data[:n] | |
data = data[i:] | |
self.iterator = itertools.chain([data], self.iterator) | |
return last | |
# Usage | |
i = (i for i in (b'1234567890', b'abcdefghij')) | |
i = IteratorIO(i) | |
assert i.read(12) == b'1234567890ab' | |
assert i.read(1) == b'c' | |
assert i.read(2) == b'de' | |
assert i.read(10) == b'fghij' | |
assert i.read(5) == b'' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment