Created
June 18, 2012 08:43
-
-
Save shiweifu/2947513 to your computer and use it in GitHub Desktop.
Wrapper to convert file-like objects to iterables
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
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