<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Werkzeug Documentation</title> <link rel="stylesheet" href="../_static/style.css" type="text/css"> <link rel="stylesheet" href="../_static/print.css" type="text/css" media="print"> <link rel="stylesheet" href="../_static/pygments.css" type="text/css"> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '0.6.1' }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/interface.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/werkzeug.js"></script> <link rel="contents" title="Global table of contents" href="../contents.html"> <link rel="index" title="Global index" href="../genindex.html"> <link rel="search" title="Search" href="../search.html"> <link rel="top" title="Werkzeug v0.6.1 documentation" href="../index.html"> <link rel="up" title="Contributed Modules" href="index.html"> <link rel="next" title="Fixers" href="fixers.html"> <link rel="prev" title="Extra Wrappers" href="wrappers.html"> </head> <body> <div class="page"> <div class="header"> <h1 class="heading"><a href="../index.html" title="back to the documentation overview"><span>Werkzeug</span></a></h1> </div> <ul class="navigation"> <li class="indexlink"><a href="../index.html">Overview</a></li> <li><a href="wrappers.html">« Extra Wrappers</a></li> <li class="active"><a href="#">Iter IO</a></li> <li><a href="fixers.html">Fixers »</a></li> </ul> <div class="body"> <div class="section" id="module-werkzeug.contrib.iterio"> <span id="iter-io"></span><h1>Iter IO<a class="headerlink" href="#module-werkzeug.contrib.iterio" title="Permalink to this headline">¶</a></h1> <p>This module implements a <a title="werkzeug.contrib.iterio.IterIO" class="reference internal" href="#werkzeug.contrib.iterio.IterIO"><tt class="xref py py-class docutils literal"><span class="pre">IterIO</span></tt></a> that converts an iterator into a stream object and the other way round. Converting streams into iterators requires the <a class="reference external" href="http://codespeak.net/py/dist/greenlet.html">greenlet</a> module.</p> <p>To convert an iterator into a stream all you have to do is to pass it directly to the <a title="werkzeug.contrib.iterio.IterIO" class="reference internal" href="#werkzeug.contrib.iterio.IterIO"><tt class="xref py py-class docutils literal"><span class="pre">IterIO</span></tt></a> constructor. In this example we pass it a newly created generator:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">foo</span><span class="p">():</span> <span class="k">yield</span> <span class="s">"something</span><span class="se">\n</span><span class="s">"</span> <span class="k">yield</span> <span class="s">"otherthings"</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">IterIO</span><span class="p">(</span><span class="n">foo</span><span class="p">())</span> <span class="k">print</span> <span class="n">stream</span><span class="o">.</span><span class="n">read</span><span class="p">()</span> <span class="c"># read the whole iterator</span> </pre></div> </div> <p>The other way round works a bit different because we have to ensure that the code execution doesn’t take place yet. An <a title="werkzeug.contrib.iterio.IterIO" class="reference internal" href="#werkzeug.contrib.iterio.IterIO"><tt class="xref py py-class docutils literal"><span class="pre">IterIO</span></tt></a> call with a callable as first argument does two things. The function itself is passed an <a title="werkzeug.contrib.iterio.IterIO" class="reference internal" href="#werkzeug.contrib.iterio.IterIO"><tt class="xref py py-class docutils literal"><span class="pre">IterIO</span></tt></a> stream it can feed. The object returned by the <a title="werkzeug.contrib.iterio.IterIO" class="reference internal" href="#werkzeug.contrib.iterio.IterIO"><tt class="xref py py-class docutils literal"><span class="pre">IterIO</span></tt></a> constructor on the other hand is not an stream object but an iterator:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">foo</span><span class="p">(</span><span class="n">stream</span><span class="p">):</span> <span class="n">stream</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"some"</span><span class="p">)</span> <span class="n">stream</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"thing"</span><span class="p">)</span> <span class="n">stream</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span> <span class="n">stream</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">"otherthing"</span><span class="p">)</span> <span class="n">iterator</span> <span class="o">=</span> <span class="n">IterIO</span><span class="p">(</span><span class="n">foo</span><span class="p">)</span> <span class="k">print</span> <span class="n">iterator</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="c"># prints something</span> <span class="k">print</span> <span class="n">iterator</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="c"># prints otherthing</span> <span class="n">iterator</span><span class="o">.</span><span class="n">next</span><span class="p">()</span> <span class="c"># raises StopIteration</span> </pre></div> </div> <dl class="class"> <dt id="werkzeug.contrib.iterio.IterIO"> <em class="property">class </em><tt class="descclassname">werkzeug.contrib.iterio.</tt><tt class="descname">IterIO</tt><a class="headerlink" href="#werkzeug.contrib.iterio.IterIO" title="Permalink to this definition">¶</a></dt> <dd>Instances of this object implement an interface compatible with the standard Python <tt class="xref py py-class docutils literal"><span class="pre">file</span></tt> object. Streams are either read-only or write-only depending on how the object is created.</dd></dl> </div> <div style="clear: both"></div> </div> <div class="footer"> © Copyright 2008 by the <a href="http://pocoo.org/">Pocoo Team</a>, documentation generated by <a href="http://sphinx.pocoo.org/">Sphinx</a> </div> </div> </body> </html>