Current File : //usr/share/doc/python-py-1.4.32/html/io.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>py.io &mdash; py 1.4.32 documentation</title>
    
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '1.4.32',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="py 1.4.32 documentation" href="index.html" />
    <link rel="next" title="py.log documentation and musings" href="log.html" />
    <link rel="prev" title="py.code: higher level python code and introspection objects" href="code.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="log.html" title="py.log documentation and musings"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="code.html" title="py.code: higher level python code and introspection objects"
             accesskey="P">previous</a> |</li>
        <li><a href="index.html">py 1.4.32 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="py-io">
<h1>py.io<a class="headerlink" href="#py-io" title="Permalink to this headline">¶</a></h1>
<p>The &#8216;py&#8217; lib provides helper classes for capturing IO during
execution of a program.</p>
<div class="section" id="io-capturing-examples">
<h2>IO Capturing examples<a class="headerlink" href="#io-capturing-examples" title="Permalink to this headline">¶</a></h2>
<div class="section" id="py-io-stdcapture">
<h3><tt class="docutils literal"><span class="pre">py.io.StdCapture</span></tt><a class="headerlink" href="#py-io-stdcapture" title="Permalink to this headline">¶</a></h3>
<p>Basic Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">py</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">capture</span> <span class="o">=</span> <span class="n">py</span><span class="o">.</span><span class="n">io</span><span class="o">.</span><span class="n">StdCapture</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="s">&quot;hello&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">out</span><span class="p">,</span><span class="n">err</span> <span class="o">=</span> <span class="n">capture</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">out</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s">&quot;hello&quot;</span>
<span class="go">True</span>
</pre></div>
</div>
<p>For calling functions you may use a shortcut:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">py</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">():</span> <span class="k">print</span> <span class="s">&quot;hello&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">res</span><span class="p">,</span> <span class="n">out</span><span class="p">,</span> <span class="n">err</span> <span class="o">=</span> <span class="n">py</span><span class="o">.</span><span class="n">io</span><span class="o">.</span><span class="n">StdCapture</span><span class="o">.</span><span class="n">call</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">out</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span> <span class="o">==</span> <span class="s">&quot;hello&quot;</span>
<span class="go">True</span>
</pre></div>
</div>
</div>
<div class="section" id="py-io-stdcapturefd">
<h3><tt class="docutils literal"><span class="pre">py.io.StdCaptureFD</span></tt><a class="headerlink" href="#py-io-stdcapturefd" title="Permalink to this headline">¶</a></h3>
<p>If you also want to capture writes to the stdout/stderr
filedescriptors you may invoke:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">py</span><span class="o">,</span> <span class="nn">sys</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">capture</span> <span class="o">=</span> <span class="n">py</span><span class="o">.</span><span class="n">io</span><span class="o">.</span><span class="n">StdCaptureFD</span><span class="p">(</span><span class="n">out</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">in_</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="s">&quot;world&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">out</span><span class="p">,</span><span class="n">err</span> <span class="o">=</span> <span class="n">capture</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">err</span>
<span class="go">&#39;world&#39;</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="py-io-object-reference">
<h2>py.io object reference<a class="headerlink" href="#py-io-object-reference" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="py.io.StdCaptureFD">
<em class="property">class </em><tt class="descclassname">py.io.</tt><tt class="descname">StdCaptureFD</tt><big>(</big><em>out=True</em>, <em>err=True</em>, <em>mixed=False</em>, <em>in_=True</em>, <em>patchsys=True</em>, <em>now=True</em><big>)</big><a class="headerlink" href="#py.io.StdCaptureFD" title="Permalink to this definition">¶</a></dt>
<dd><p>This class allows to capture writes to FD1 and FD2
and may connect a NULL file to FD0 (and prevent
reads from sys.stdin).  If any of the 0,1,2 file descriptors
is invalid it will not be captured.</p>
<dl class="method">
<dt id="py.io.StdCaptureFD.resume">
<tt class="descname">resume</tt><big>(</big><big>)</big><a class="headerlink" href="#py.io.StdCaptureFD.resume" title="Permalink to this definition">¶</a></dt>
<dd><p>resume capturing with original temp files.</p>
</dd></dl>

<dl class="method">
<dt id="py.io.StdCaptureFD.done">
<tt class="descname">done</tt><big>(</big><em>save=True</em><big>)</big><a class="headerlink" href="#py.io.StdCaptureFD.done" title="Permalink to this definition">¶</a></dt>
<dd><p>return (outfile, errfile) and stop capturing.</p>
</dd></dl>

<dl class="method">
<dt id="py.io.StdCaptureFD.readouterr">
<tt class="descname">readouterr</tt><big>(</big><big>)</big><a class="headerlink" href="#py.io.StdCaptureFD.readouterr" title="Permalink to this definition">¶</a></dt>
<dd><p>return snapshot value of stdout/stderr capturings.</p>
</dd></dl>

<dl class="classmethod">
<dt id="py.io.StdCaptureFD.call">
<em class="property">classmethod </em><tt class="descname">call</tt><big>(</big><em>func</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#py.io.StdCaptureFD.call" title="Permalink to this definition">¶</a></dt>
<dd><p>return a (res, out, err) tuple where
out and err represent the output/error output
during function execution.
call the given function with args/kwargs
and capture output/error during its execution.</p>
</dd></dl>

<dl class="method">
<dt id="py.io.StdCaptureFD.reset">
<tt class="descname">reset</tt><big>(</big><big>)</big><a class="headerlink" href="#py.io.StdCaptureFD.reset" title="Permalink to this definition">¶</a></dt>
<dd><p>reset sys.stdout/stderr and return captured output as strings.</p>
</dd></dl>

<dl class="method">
<dt id="py.io.StdCaptureFD.suspend">
<tt class="descname">suspend</tt><big>(</big><big>)</big><a class="headerlink" href="#py.io.StdCaptureFD.suspend" title="Permalink to this definition">¶</a></dt>
<dd><p>return current snapshot captures, memorize tempfiles.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="py.io.StdCapture">
<em class="property">class </em><tt class="descclassname">py.io.</tt><tt class="descname">StdCapture</tt><big>(</big><em>out=True</em>, <em>err=True</em>, <em>in_=True</em>, <em>mixed=False</em>, <em>now=True</em><big>)</big><a class="headerlink" href="#py.io.StdCapture" title="Permalink to this definition">¶</a></dt>
<dd><p>This class allows to capture writes to sys.stdout|stderr &#8220;in-memory&#8221;
and will raise errors on tries to read from sys.stdin. It only
modifies sys.stdout|stderr|stdin attributes and does not
touch underlying File Descriptors (use StdCaptureFD for that).</p>
<dl class="method">
<dt id="py.io.StdCapture.done">
<tt class="descname">done</tt><big>(</big><em>save=True</em><big>)</big><a class="headerlink" href="#py.io.StdCapture.done" title="Permalink to this definition">¶</a></dt>
<dd><p>return (outfile, errfile) and stop capturing.</p>
</dd></dl>

<dl class="method">
<dt id="py.io.StdCapture.resume">
<tt class="descname">resume</tt><big>(</big><big>)</big><a class="headerlink" href="#py.io.StdCapture.resume" title="Permalink to this definition">¶</a></dt>
<dd><p>resume capturing with original temp files.</p>
</dd></dl>

<dl class="method">
<dt id="py.io.StdCapture.readouterr">
<tt class="descname">readouterr</tt><big>(</big><big>)</big><a class="headerlink" href="#py.io.StdCapture.readouterr" title="Permalink to this definition">¶</a></dt>
<dd><p>return snapshot value of stdout/stderr capturings.</p>
</dd></dl>

<dl class="classmethod">
<dt id="py.io.StdCapture.call">
<em class="property">classmethod </em><tt class="descname">call</tt><big>(</big><em>func</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#py.io.StdCapture.call" title="Permalink to this definition">¶</a></dt>
<dd><p>return a (res, out, err) tuple where
out and err represent the output/error output
during function execution.
call the given function with args/kwargs
and capture output/error during its execution.</p>
</dd></dl>

<dl class="method">
<dt id="py.io.StdCapture.reset">
<tt class="descname">reset</tt><big>(</big><big>)</big><a class="headerlink" href="#py.io.StdCapture.reset" title="Permalink to this definition">¶</a></dt>
<dd><p>reset sys.stdout/stderr and return captured output as strings.</p>
</dd></dl>

<dl class="method">
<dt id="py.io.StdCapture.suspend">
<tt class="descname">suspend</tt><big>(</big><big>)</big><a class="headerlink" href="#py.io.StdCapture.suspend" title="Permalink to this definition">¶</a></dt>
<dd><p>return current snapshot captures, memorize tempfiles.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="py.io.TerminalWriter">
<em class="property">class </em><tt class="descclassname">py.io.</tt><tt class="descname">TerminalWriter</tt><big>(</big><em>file=None</em>, <em>stringio=False</em>, <em>encoding=None</em><big>)</big><a class="headerlink" href="#py.io.TerminalWriter" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">py.io</a><ul>
<li><a class="reference internal" href="#io-capturing-examples">IO Capturing examples</a><ul>
<li><a class="reference internal" href="#py-io-stdcapture"><tt class="docutils literal"><span class="pre">py.io.StdCapture</span></tt></a></li>
<li><a class="reference internal" href="#py-io-stdcapturefd"><tt class="docutils literal"><span class="pre">py.io.StdCaptureFD</span></tt></a></li>
</ul>
</li>
<li><a class="reference internal" href="#py-io-object-reference">py.io object reference</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="code.html"
                        title="previous chapter">py.code: higher level python code and introspection objects</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="log.html"
                        title="next chapter">py.log documentation and musings</a></p>
  <h3>This Page</h3>
  <ul class="this-page-menu">
    <li><a href="_sources/io.txt"
           rel="nofollow">Show Source</a></li>
  </ul>
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="log.html" title="py.log documentation and musings"
             >next</a> |</li>
        <li class="right" >
          <a href="code.html" title="py.code: higher level python code and introspection objects"
             >previous</a> |</li>
        <li><a href="index.html">py 1.4.32 documentation</a> &raquo;</li> 
      </ul>
    </div>

    <div class="footer">
        &copy; Copyright 2010, holger krekel et. al..
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-7597274-14']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

  </body>
</html>