python 3.x - How to use a generator inside an openhook in a fileinput object? -
i try replace tabulation characters 8 whitespaces in fileinput
structure using openhook
argument of fileinput
class.
here current source code:
from io import textiowrapper class iterstream(textiowrapper): """ file-like streaming iterator. """ def __init__(self, generator): self.generator = generator self.iterator = iter(generator) self.leftover = '' def __iter__(self): return self.iterator def next(self): return self.iterator.next() def __next__(self): return self.iterator.__next__() def read(self, size): data = self.leftover count = len(self.leftover) try: while count < size: chunk = self.__next__() data += chunk count += len(chunk) except stopiteration: self.leftover = '' return data return data[:size] def readline(self, size): return self.read(size) def readlines(self, size): return self.read(size) def close(self): pass def streamfilter(filter): def stream(iostream): return iterstream(filter(iostream)) return stream @streamfilter def tab_filter(stream): line in stream: yield line.replace ('\t', ' ' * 8) def fileinput_hook(filename, mode): return tab_filter(open(filename, mode)) if __name__ == "__main__": import fileinput f = fileinput.input(files='file.txt', openhook=fileinput_hook) line in f: print(f.filename() + ": " + str(f.filelineno()) + ": " + line)
note follow reasonning (i might wrong way do):
i try create generator (
tab_filter
) able transform tabulation 8 whitespaces.then, create wrapper can take generator/iterator , pretend normal file (
iterstream
).i add few cosmetic changes make easier manipulate filters (the
@streamfilter
decorator).i create
fileinput_hook()
function intended hook on file insidefileinput
object.
once done, have create fileinput
object , browse through it.
unfornately, following error:
traceback (most recent call last): file "./example.py", line 60, in <module> line in f: file "/usr/lib/python3.5/fileinput.py", line 265, in __next__ line = self.readline() file "/usr/lib/python3.5/fileinput.py", line 370, in readline self._buffer = self._file.readlines(self._bufsize) file "./example.py", line 36, in readlines return self.read(size) file "./example.py", line 23, in read chunk = self.iterator.next() attributeerror: 'generator' object has no attribute 'next'
so, did wrong, , how achieve want? far? there better ways it?
edit after solving confusion between __next__
, next
, following error:
traceback (most recent call last): file "./example.py", line 63, in <module> line in f: file "/usr/lib/python3.5/fileinput.py", line 265, in __next__ line = self.readline() file "/usr/lib/python3.5/fileinput.py", line 370, in readline self._buffer = self._file.readlines(self._bufsize) file "./example.py", line 39, in readlines return self.read(size) file "./example.py", line 26, in read chunk = self.__next__() valueerror: i/o operation on uninitialized object
edit2
in fact, works alokthakur said... not know happened.
if using python 3.x should use __next__()
instead of next()
change
self.iterator.next()
to
self.iterator.__next__()
Comments
Post a Comment