Skip to content

Instantly share code, notes, and snippets.

@KarimLulu
Last active November 24, 2019 14:24
Show Gist options
  • Save KarimLulu/53b334e3646cefb37c9bfa08730bb4aa to your computer and use it in GitHub Desktop.
Save KarimLulu/53b334e3646cefb37c9bfa08730bb4aa to your computer and use it in GitHub Desktop.
Read from several files as though they are glued into a single one
import io
class IOChain(object):
def __init__(self, files):
self.files = iter(files)
try:
self._current_file = next(self.files)
except StopIteration:
self._current_file = None
def read(self, size=None):
if size == -1:
size = None
if self._current_file is None or size == 0:
return b''
data = self._current_file.read(size)
if size is None or len(data) < size:
try:
self._current_file = next(self.files)
except StopIteration:
self._current_file = None
return data + self.read(None if size is None else size - len(data))
def test_read_no_files():
file_chain = IOChain([])
assert b'' == file_chain.read(50)
def test_read():
file_chain = IOChain([io.BytesIO(x) for x in (b'123', b'456', b'789')])
assert b'1' == file_chain.read(1)
assert b'234' == file_chain.read(3)
assert b'56789' == file_chain.read()
assert b'' == file_chain.read(None)
assert b'' == file_chain.read(-1)
test_read_no_files()
test_read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment