# Iter IO
This module implements a [IterIO](# "werkzeug.contrib.iterio.IterIO") that converts an iterator intoa stream object and the other way round. Converting streams intoiterators requires the [greenlet](http://codespeak.net/py/dist/greenlet.html) module.
To convert an iterator into a stream all you have to do is to pass itdirectly to the [IterIO](# "werkzeug.contrib.iterio.IterIO") constructor. In this example we pass ita 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 thatthe code execution doesn't take place yet. An [IterIO](# "werkzeug.contrib.iterio.IterIO") call with acallable as first argument does two things. The function itself is passedan [IterIO](# "werkzeug.contrib.iterio.IterIO") stream it can feed. The object returned by the[IterIO](# "werkzeug.contrib.iterio.IterIO") constructor on the other hand is not an stream object butan 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 thestandard Python file object. Streams are either read-only orwrite-only depending on how the object is created.
If the first argument is an iterable a file like object is returned thatreturns the contents of the iterable. In case the iterable is emptyread operations will return the sentinel value.
If the first argument is a callable then the stream object will becreated and passed to that function. The caller itself however willnot receive a stream but an iterable. The function will be be executedstep by step as something iterates over the returned iterable. Eachcall to flush() will create an item for the iterable. Ifflush() is called without any writes in-between the sentinelvalue will be yielded.
Note for Python 3: due to the incompatible interface of bytes andstreams you should set the sentinel value explicitly to an emptybytestring (b'') if you are expecting to deal with bytes asotherwise the end of the stream is marked with the wrong sentinelvalue.
0.9 新版功能: sentinel parameter was added.
- 开始
- Werkzeug 文档概览
- 安装
- 过渡到 Werkzeug 1.0
- Werkzeug 教程
- API 标准
- 快速开始
- Python 3 Notes
- 服务和测试
- Debugging Applications
- 在服务器运行 WSGI 应用
- 单元测试
- 参考
- Request / Response Objects
- URL Routing
- WSGI Helpers
- HTTP Utilities
- Data Structures
- Utilities
- Context Locals
- Middlewares
- HTTP Exceptions
- 部署
- CGI
- mod_wsgi (Apache)
- FastCGI
- HTTP Proxying
- 贡献模块
- Atom Syndication
- Sessions
- Secure Cookie
- Cache
- Extra Wrappers
- Iter IO
- Fixers
- WSGI Application Profiler
- Lint Validation Middleware
- 额外说明
- Werkzeug Changelog
- Important Terms
- Unicode
- Dealing with Request Data