Skip to content

Instantly share code, notes, and snippets.

@AstraLuma
Created October 1, 2014 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AstraLuma/7fe44dce0c812c6166fb to your computer and use it in GitHub Desktop.
Save AstraLuma/7fe44dce0c812c6166fb to your computer and use it in GitHub Desktop.
Bit of code to make a stream look like a buffer
from __future__ import absolute_import, division, print_function, unicode_literals
import six
if six.PY2:
__metaclass__ = type
class StreamBuffer:
"""
Makes a buffer look like a stream. Assumes infinite stream.
"""
def __init__(self, stream):
self._stream = stream
self._buffer = "" # XXX: Is there a mutable string type in Python 2?
self._chunksize = 1 # Used for iteration
def _readtosize(self, size):
while len(self._buffer) < size:
self._buffer += self._stream.read(size-len(self._buffer))
def __iter__(self):
for c in self._buffer:
yield c
while True:
l = len(self._buffer)
self._readtosize(l+self._chunksize)
for c in self._buffer[l:]:
yield c
def __getitem__(self, index):
if isinstance(index, slice):
if index.stop is None or index.stop < 0:
raise IndexError("Slices must have a stop")
self._readtosize(index.stop)
return self._buffer[index]
elif index < 0:
raise IndexError("Infinite buffers don't support ")
else:
return self._buffer[index]
def __delitem__(self, index):
"""del sb[int|slice]
Removes part of the buffer, freeing memory.
Only contiguous chunks at the begining of the buffer will be removed.
"""
# Frees buffer
if isinstance(index, slice):
if index.stop is None or index.stop < 0:
raise IndexError("Slices must have a stop")
if index.start not in (None, 0):
raise IndexError("Slices must be at the start")
if index.step not in (None, 1):
raise IndexError("Will only delete contiguous blocks")
self._readtosize(index.stop)
self._buffer = self._buffer[index.stop:] # Can't use del because immutable
else:
if index != 0:
raise IndexError("Will only delete at the begining")
self._buffer = self._buffer[index+1:] # Can't use del because immutable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment