Skip to content

Instantly share code, notes, and snippets.

@shiweifu
Created June 18, 2012 08:43
Show Gist options
  • Save shiweifu/2947513 to your computer and use it in GitHub Desktop.
Save shiweifu/2947513 to your computer and use it in GitHub Desktop.
Wrapper to convert file-like objects to iterables
class FileWrapper:
"""Wrapper to convert file-like objects to iterables"""
def __init__(self, filelike, blksize=8192):
self.filelike = filelike
self.blksize = blksize
if hasattr(filelike,'close'):
self.close = filelike.close
def __getitem__(self,key):
data = self.filelike.read(self.blksize)
if data:
return data
raise IndexError
def __iter__(self):
return self
def next(self):
data = self.filelike.read(self.blksize)
if data:
return data
raise StopIteration
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment