Werkzeug

Iter IO

This module implements a IterIO that converts an iterator into a stream object and the other way round. Converting streams into iterators requires the greenlet module.

To convert an iterator into a stream all you have to do is to pass it directly to the IterIO constructor. In this example we pass it a newly created generator:

def foo():
    yield "something\n"
    yield "otherthings"
stream = IterIO(foo())
print stream.read()         # read the whole iterator

The other way round works a bit different because we have to ensure that the code execution doesn’t take place yet. An IterIO call with a callable as first argument does two things. The function itself is passed an IterIO stream it can feed. The object returned by the IterIO constructor on the other hand is not an stream object but an iterator:

def foo(stream):
    stream.write("some")
    stream.write("thing")
    stream.flush()
    stream.write("otherthing")
iterator = IterIO(foo)
print iterator.next()       # prints something
print iterator.next()       # prints otherthing
iterator.next()             # raises StopIteration
class werkzeug.contrib.iterio.IterIO
Instances of this object implement an interface compatible with the standard Python file object. Streams are either read-only or write-only depending on how the object is created.