/usr/share/doc/python-toolz-doc/html/laziness.html is in python-toolz-doc 0.8.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 | <!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>Laziness — Toolz 0.8.2 documentation</title>
<link rel="stylesheet" href="_static/classic.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: './',
VERSION: '0.8.2',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="Control Flow" href="control.html" />
<link rel="prev" title="Function Purity" href="purity.html" />
</head>
<body role="document">
<div class="related" role="navigation" aria-label="related navigation">
<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="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="control.html" title="Control Flow"
accesskey="N">next</a> |</li>
<li class="right" >
<a href="purity.html" title="Function Purity"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Toolz 0.8.2 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="laziness">
<h1>Laziness<a class="headerlink" href="#laziness" title="Permalink to this headline">¶</a></h1>
<p>Lazy iterators evaluate only when necessary. They allow us to semantically
manipulate large amounts of data while keeping very little of it actually in
memory. They act like lists but don’t take up space.</p>
<div class="section" id="example-a-tale-of-two-cities">
<h2>Example - A Tale of Two Cities<a class="headerlink" href="#example-a-tale-of-two-cities" title="Permalink to this headline">¶</a></h2>
<p>We open a file containing the text of the classic text “A Tale of Two Cities”
by Charles Dickens[<a class="reference internal" href="#id1">1</a>].</p>
<div class="code highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">book</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'tale-of-two-cities.txt'</span><span class="p">)</span>
</pre></div>
</div>
<p>Much like a secondary school student, Python owns and opens the book without
reading a single line of the text. The object <code class="docutils literal"><span class="pre">book</span></code> is a lazy iterator!
Python will give us a line of the text only when we explicitly ask it to do so</p>
<div class="code highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">next</span><span class="p">(</span><span class="n">book</span><span class="p">)</span>
<span class="go">"It was the best of times,"</span>
<span class="gp">>>> </span><span class="nb">next</span><span class="p">(</span><span class="n">book</span><span class="p">)</span>
<span class="go">"it was the worst of times,"</span>
</pre></div>
</div>
<p>and so on. Each time we call <code class="docutils literal"><span class="pre">next</span></code> on <code class="docutils literal"><span class="pre">book</span></code> we burn through another line
of the text and the <code class="docutils literal"><span class="pre">book</span></code> iterator marches slowly onwards through the text.</p>
</div>
<div class="section" id="computation">
<h2>Computation<a class="headerlink" href="#computation" title="Permalink to this headline">¶</a></h2>
<p>We can lazily operate on lazy iterators without doing any actual computation.
For example lets read the book in upper case</p>
<div class="code highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">toolz</span> <span class="k">import</span> <span class="nb">map</span> <span class="c1"># toolz' map is lazy by default</span>
<span class="gp">>>> </span><span class="n">loud_book</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">str</span><span class="o">.</span><span class="n">upper</span><span class="p">,</span> <span class="n">book</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">next</span><span class="p">(</span><span class="n">loud_book</span><span class="p">)</span>
<span class="go">"IT WAS THE AGE OF WISDOM,"</span>
<span class="gp">>>> </span><span class="nb">next</span><span class="p">(</span><span class="n">loud_book</span><span class="p">)</span>
<span class="go">"IT WAS THE AGE OF FOOLISHNESS,"</span>
</pre></div>
</div>
<p>It is as if we applied the function <code class="docutils literal"><span class="pre">str.upper</span></code> onto every line of the book;
yet the first line completes instantaneously. Instead Python does the
uppercasing work only when it becomes necessary, i.e. when you call <code class="docutils literal"><span class="pre">next</span></code>
to ask for another line.</p>
</div>
<div class="section" id="reductions">
<h2>Reductions<a class="headerlink" href="#reductions" title="Permalink to this headline">¶</a></h2>
<p>You can operate on lazy iterators just as you would with lists, tuples, or
sets. You can use them in for loops as in</p>
<div class="code highlight-default"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">loud_book</span><span class="p">:</span>
<span class="o">...</span>
</pre></div>
</div>
<p>You can instantiate them all into memory by calling them with the constructors
<code class="docutils literal"><span class="pre">list</span></code>, or <code class="docutils literal"><span class="pre">tuple</span></code>.</p>
<div class="code highlight-default"><div class="highlight"><pre><span></span><span class="n">loud_book</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">loud_book</span><span class="p">)</span>
</pre></div>
</div>
<p>Of course if they are very large then this might be unwise. Often we use
laziness to avoid loading large datasets into memory at once. Many
computations on large datasets don’t require access to all of the data at a
single time. In particular <em>reductions</em> (like sum) often take large amounts of
sequential data (like [1, 2, 3, 4]) and produce much more manageable results
(like 10) and can do so just by viewing the data a little bit at a time. For
example we can count all of the letters in the Tale of Two Cities trivially
using functions from <code class="docutils literal"><span class="pre">toolz</span></code></p>
<div class="code highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">toolz</span> <span class="k">import</span> <span class="n">concat</span><span class="p">,</span> <span class="n">frequencies</span>
<span class="gp">>>> </span><span class="n">letters</span> <span class="o">=</span> <span class="n">frequencies</span><span class="p">(</span><span class="n">concat</span><span class="p">(</span><span class="n">loud_book</span><span class="p">))</span>
<span class="go">{ 'A': 48036,</span>
<span class="go"> 'B': 8402,</span>
<span class="go"> 'C': 13812,</span>
<span class="go"> 'D': 28000,</span>
<span class="go"> 'E': 74624,</span>
<span class="go"> ...</span>
</pre></div>
</div>
<p>In this case <code class="docutils literal"><span class="pre">frequencies</span></code> is a sort of reduction. At no time were more than
a few hundred bytes of Tale of Two Cities necessarily in memory. We could just
have easily done this computation on the entire Gutenberg collection or on
Wikipedia. In this case we are limited by the size and speed of our hard drive
and not by the capacity of our memory.</p>
<table class="docutils footnote" frame="void" id="id1" rules="none">
<colgroup><col class="label" /><col /></colgroup>
<tbody valign="top">
<tr><td class="label">[1]</td><td><a class="reference external" href="http://www.gutenberg.org/cache/epub/98/pg98.txt">http://www.gutenberg.org/cache/epub/98/pg98.txt</a></td></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Laziness</a><ul>
<li><a class="reference internal" href="#example-a-tale-of-two-cities">Example - A Tale of Two Cities</a></li>
<li><a class="reference internal" href="#computation">Computation</a></li>
<li><a class="reference internal" href="#reductions">Reductions</a></li>
</ul>
</li>
</ul>
<h4>Previous topic</h4>
<p class="topless"><a href="purity.html"
title="previous chapter">Function Purity</a></p>
<h4>Next topic</h4>
<p class="topless"><a href="control.html"
title="next chapter">Control Flow</a></p>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/laziness.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<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="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="control.html" title="Control Flow"
>next</a> |</li>
<li class="right" >
<a href="purity.html" title="Function Purity"
>previous</a> |</li>
<li class="nav-item nav-item-0"><a href="index.html">Toolz 0.8.2 documentation</a> »</li>
</ul>
</div>
<div class="footer" role="contentinfo">
© Copyright 2017, Matthew Rocklin, John Jacobsen.
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.5.6.
</div>
</body>
</html>
|