import io | |
import sys | |
class IteratorFile(io.TextIOBase): | |
""" given an iterator which yields strings, | |
return a file like object for reading those strings """ | |
def __init__(self, it): | |
self._it = it | |
self._f = io.StringIO() | |
def read(self, length=sys.maxsize): | |
try: | |
while self._f.tell() < length: | |
self._f.write(next(self._it) + "\n") | |
except StopIteration as e: | |
# soak up StopIteration. this block is not necessary because | |
# of finally, but just to be explicit | |
pass | |
except Exception as e: | |
print("uncaught exception: {}".format(e)) | |
finally: | |
self._f.seek(0) | |
data = self._f.read(length) | |
# save the remainder for next read | |
remainder = self._f.read() | |
self._f.seek(0) | |
self._f.truncate(0) | |
self._f.write(remainder) | |
return data | |
def readline(self): | |
return next(self._it) |
This comment has been minimized.
This comment has been minimized.
This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or In jurisdictions that recognize copyright laws, the author or authors THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, For more information, please refer to http://unlicense.org/ |
This comment has been minimized.
This comment has been minimized.
Thanks for asking! |
This comment has been minimized.
Hi, what's the licence on this file?
If it doesn't have one could you specify one please?
Thanks