Skip to content

Commit cb828d3

Browse files
committed
Website build
1 parent 155badd commit cb828d3

22 files changed

+136
-101
lines changed

10_trees_and_directed_acyclic_graphs.html

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,9 @@ <h3><span class="section-number">10.2.2. </span>Traversing <a class="reference i
440440
<p>This time the visitor <a class="reference external" href="https://docs.python.org/3/library/functions.html#sum" title="(in Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">sums</span></code></a> the results from its children, and adds
441441
one for itself.</p>
442442
<p>What about preorder traversal? This time we need a little more code (not much)
443-
as <code class="xref std std-numref docutils literal notranslate"><span class="pre">preorder_recursive</span></code> shows.</p>
443+
as <a class="reference internal" href="#preorder-recursive"><span class="std std-numref">Listing 10.5</span></a> shows.</p>
444444
<div class="literal-block-wrapper docutils container" id="id17">
445-
<div class="code-block-caption"><span class="caption-number">Listing 10.5 </span><span class="caption-text">The simple preorder visitor from
445+
<span id="preorder-recursive"></span><div class="code-block-caption"><span class="caption-number">Listing 10.5 </span><span class="caption-text">The simple preorder visitor from
446446
<a class="reference internal" href="example_code.html#example_code.graphs.previsitor" title="example_code.graphs.previsitor"><code class="xref py py-func docutils literal notranslate"><span class="pre">example_code.graphs.previsitor()</span></code></a>.</span><a class="headerlink" href="#id17" title="Permalink to this code"></a></div>
447447
<div class="highlight-python3 notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
448448
2
@@ -505,7 +505,7 @@ <h3><span class="section-number">10.2.2. </span>Traversing <a class="reference i
505505
</div>
506506
</div>
507507
<div class="section" id="expression-trees">
508-
<h2><span class="section-number">10.3. </span>Expression trees<a class="headerlink" href="#expression-trees" title="Permalink to this headline"></a></h2>
508+
<span id="expr-trees"></span><h2><span class="section-number">10.3. </span>Expression trees<a class="headerlink" href="#expression-trees" title="Permalink to this headline"></a></h2>
509509
<p>One important application of trees is in representing arithmetic expressions.
510510
Consider the expression <span class="math notranslate nohighlight">\(2y + 4^{(5 + x)}\)</span>. Suppose, further, that
511511
we want to represent this on a computer in such a way that we can perform
@@ -623,11 +623,10 @@ <h2><span class="section-number">10.3. </span>Expression trees<a class="headerli
623623
is a pair of round brackets containing the string representation of each
624624
item in the tuple.</p>
625625
</dd>
626-
<dt><a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__str__" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__str__()</span></code></a></dt><dd><p>This is the human-readable string output, using <span class="xref std std-term">infix</span> operators, so
627-
in the example above we would expect to see <code class="xref py py-obj docutils literal notranslate"><span class="pre">2</span> <span class="pre">*</span> <span class="pre">y</span> <span class="pre">+</span> <span class="pre">4</span> <span class="pre">^</span> <span class="pre">(5</span> <span class="pre">+</span> <span class="pre">x)</span></code> This looks
628-
sort of straightforward, simply associate the correct symbol with each
629-
operator class as a <a class="reference internal" href="8_inheritance.html#term-class-attribute"><span class="xref std std-term">class attribute</span></a> and place the string
630-
representation of the operands on either side.</p>
626+
<dt><a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__str__" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__str__()</span></code></a></dt><dd><p>This is the human-readable string output, using <a class="reference internal" href="3_objects.html#term-infix-operator"><span class="xref std std-term">infix operators</span></a>, so in the example above we would expect to see
627+
<code class="xref py py-obj docutils literal notranslate"><span class="pre">2</span> <span class="pre">*</span> <span class="pre">y</span> <span class="pre">+</span> <span class="pre">4</span> <span class="pre">^</span> <span class="pre">(5</span> <span class="pre">+</span> <span class="pre">x)</span></code> This looks sort of straightforward, simply associate the correct
628+
symbol with each operator class as a <a class="reference internal" href="8_inheritance.html#term-class-attribute"><span class="xref std std-term">class attribute</span></a> and place the
629+
string representation of the operands on either side.</p>
631630
<p>The challenge is to correctly include the brackets. In order to do this, it
632631
is necessary to associate with every expression class a <a class="reference internal" href="8_inheritance.html#term-class-attribute"><span class="xref std std-term">class
633632
attribute</span></a> whose value is a number giving an operator precedence to that
@@ -639,14 +638,14 @@ <h2><span class="section-number">10.3. </span>Expression trees<a class="headerli
639638
</dd>
640639
</dl>
641640
<p>Individual operator classes therefore need to define very little, just two
642-
<span class="xref std std-term">class attributes</span>, one for the operator precedence, and one to set the
643-
symbol when printing the operator.</p>
641+
<a class="reference internal" href="8_inheritance.html#term-class-attribute"><span class="xref std std-term">class attributes</span></a>, one for the operator precedence, and
642+
one to set the symbol when printing the operator.</p>
644643
<p>Let’s now consider <code class="xref py py-class docutils literal notranslate"><span class="pre">Terminal</span></code>. What does it need to set?</p>
645644
<dl class="simple">
646645
<dt><a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__init__" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code></a></dt><dd><p>The <a class="reference internal" href="3_objects.html#term-constructor"><span class="xref std std-term">constructor</span></a> for <code class="xref py py-class docutils literal notranslate"><span class="pre">Expression</span></code> assumes that an expression is
647646
defined by a series of operands. Terminals have an empty list of operands
648647
but do have something that other expressions lack, a value. In the case of
649-
<span class="xref std std-term">Number</span>, this is a number, while for <span class="xref std std-term">Symbol</span> the value is a
648+
<code class="xref py py-class docutils literal notranslate"><span class="pre">Number</span></code>, this is a number, while for <code class="xref py py-class docutils literal notranslate"><span class="pre">Symbol</span></code> the value is a
650649
string (usually a single character). <code class="xref py py-class docutils literal notranslate"><span class="pre">Terminal</span></code> therefore needs its
651650
own <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> which will take a value argument.
652651
<code class="xref py py-class docutils literal notranslate"><span class="pre">Terminal.__init__</span></code> still has to call the <a class="reference internal" href="8_inheritance.html#term-superclass"><span class="xref std std-term">superclass</span></a>
@@ -823,7 +822,7 @@ <h3><span class="section-number">10.3.3. </span>Single dispatch functions<a clas
823822
<a class="reference external" href="https://docs.python.org/3/library/functools.html#functools.singledispatch" title="(in Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">functools.singledispatch()</span></code></a> turns a function into
824823
a single dispatch function. The <code class="xref py py-obj docutils literal notranslate"><span class="pre">&#64;</span></code> symbol marks
825824
<a class="reference external" href="https://docs.python.org/3/library/functools.html#functools.singledispatch" title="(in Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">singledispatch()</span></code></a> as a <a class="reference external" href="https://docs.python.org/3/glossary.html#term-decorator" title="(in Python v3.9)"><span class="xref std std-term">decorator</span></a>. We’ll return to them
826-
in <code class="xref std std-numref docutils literal notranslate"><span class="pre">decorators</span></code>. For the moment, we just need to know that
825+
in <a class="reference internal" href="11_further_object-oriented_features.html#decorators"><span class="std std-numref">Section 11.1</span></a>. For the moment, we just need to know that
827826
<code class="xref py py-obj docutils literal notranslate"><span class="pre">&#64;singledispatch</span></code> turns the function it precedes into a single dispatch
828827
function.</p>
829828
<p>Next we turn our attention to the implementation of evaluation for the different
@@ -1034,10 +1033,10 @@ <h3><span class="section-number">10.5.2. </span><a class="reference internal" hr
10341033
discussed above. Instead, we can construct a postorder DAG visitor using a
10351034
<a class="reference internal" href="5_abstract_data_types.html#term-stack"><span class="xref std std-term">stack</span></a> to replace the recursion in keeping track of what to evaluate
10361035
next, and a <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(in Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> to record the nodes we have already visited, and the
1037-
results of visiting them. <code class="xref std std-numref docutils literal notranslate"><span class="pre">nonrecursive_postvisit</span></code> illustrates one such
1036+
results of visiting them. <a class="reference internal" href="#nonrecursive-postvisit"><span class="std std-numref">Listing 10.8</span></a> illustrates one such
10381037
algorithm.</p>
10391038
<div class="literal-block-wrapper docutils container" id="id23">
1040-
<div class="code-block-caption"><span class="caption-number">Listing 10.8 </span><span class="caption-text">Pythonic pseudocode for a non-recursive postorder <a class="reference internal" href="#term-DAG"><span class="xref std std-term">DAG</span></a> visitor.</span><a class="headerlink" href="#id23" title="Permalink to this code"></a></div>
1039+
<span id="nonrecursive-postvisit"></span><div class="code-block-caption"><span class="caption-number">Listing 10.8 </span><span class="caption-text">Pythonic pseudocode for a non-recursive postorder <a class="reference internal" href="#term-DAG"><span class="xref std std-term">DAG</span></a> visitor.</span><a class="headerlink" href="#id23" title="Permalink to this code"></a></div>
10411040
<div class="highlight-python3 notranslate"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
10421041
2
10431042
3
@@ -1098,7 +1097,7 @@ <h3><span class="section-number">10.5.2. </span><a class="reference internal" hr
10981097
</div>
10991098
<div class="section" id="differentiation-as-an-expression-visitor">
11001099
<h2><span class="section-number">10.6. </span>Differentiation as an expression visitor<a class="headerlink" href="#differentiation-as-an-expression-visitor" title="Permalink to this headline"></a></h2>
1101-
<p>In <code class="xref std std-numref docutils literal notranslate"><span class="pre">expr_trees</span></code> we showed how a tree visitor could implement the
1100+
<p>In <a class="reference internal" href="#expr-trees"><span class="std std-numref">Section 10.3</span></a> we showed how a tree visitor could implement the
11021101
evaluation of a symbolic expression. You might very well protest that if the
11031102
only thing you wanted to do was evaluate arithmetic expressions then you could
11041103
have just written a Python function and avoided a lot of code. In fact, almost
@@ -1134,8 +1133,7 @@ <h2><span class="section-number">10.6. </span>Differentiation as an expression v
11341133
</div>
11351134
<div class="section" id="glossary">
11361135
<h2><span class="section-number">10.7. </span>Glossary<a class="headerlink" href="#glossary" title="Permalink to this headline"></a></h2>
1137-
<blockquote>
1138-
<div><dl class="glossary simple">
1136+
<dl class="glossary simple">
11391137
<dt id="term-child-node">child node</dt><dd><p>The children of a node in a <a class="reference internal" href="#term-DAG"><span class="xref std std-term">DAG</span></a> are the targets of the edges
11401138
emerging from that node. In this context, the node from which these
11411139
edges emanate is called the <a class="reference internal" href="#term-parent-node"><span class="xref std std-term">parent node</span></a>.</p>
@@ -1174,7 +1172,6 @@ <h2><span class="section-number">10.7. </span>Glossary<a class="headerlink" href
11741172
most one edge.</p>
11751173
</dd>
11761174
</dl>
1177-
</div></blockquote>
11781175
</div>
11791176
<div class="section" id="exercises">
11801177
<h2><span class="section-number">10.8. </span>Exercises<a class="headerlink" href="#exercises" title="Permalink to this headline"></a></h2>

11_further_object-oriented_features.html

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,23 @@
5454
<div class="section" id="further-object-oriented-features">
5555
<h1><span class="section-number">11. </span>Further object-oriented features<a class="headerlink" href="#further-object-oriented-features" title="Permalink to this headline"></a></h1>
5656
<div class="section" id="decorators">
57-
<h2><span class="section-number">11.1. </span>Decorators<a class="headerlink" href="#decorators" title="Permalink to this headline"></a></h2>
57+
<span id="id1"></span><h2><span class="section-number">11.1. </span>Decorators<a class="headerlink" href="#decorators" title="Permalink to this headline"></a></h2>
5858
</div>
5959
<div class="section" id="abstract-classes">
60-
<h2><span class="section-number">11.2. </span>Abstract classes<a class="headerlink" href="#abstract-classes" title="Permalink to this headline"></a></h2>
60+
<span id="id2"></span><h2><span class="section-number">11.2. </span>Abstract classes<a class="headerlink" href="#abstract-classes" title="Permalink to this headline"></a></h2>
6161
</div>
6262
<div class="section" id="abstract-base-classes">
63-
<span id="id1"></span><h2><span class="section-number">11.3. </span>Abstract base classes<a class="headerlink" href="#abstract-base-classes" title="Permalink to this headline"></a></h2>
63+
<span id="id3"></span><h2><span class="section-number">11.3. </span>Abstract base classes<a class="headerlink" href="#abstract-base-classes" title="Permalink to this headline"></a></h2>
64+
<div class="section" id="glossary">
65+
<h3><span class="section-number">11.3.1. </span>Glossary<a class="headerlink" href="#glossary" title="Permalink to this headline"></a></h3>
66+
<dl class="glossary simple">
67+
<dt id="term-abstract-class">abstract class</dt><dd><p>A class designed only to be the <a class="reference internal" href="8_inheritance.html#term-parent-class"><span class="xref std std-term">parent</span></a> of other
68+
classes, and never to be instantiated itself. Abstract classes often
69+
define the interfaces of <a class="reference internal" href="3_objects.html#term-method"><span class="xref std std-term">methods</span></a> but leave their implementations
70+
to the concrete <a class="reference internal" href="8_inheritance.html#term-child-class"><span class="xref std std-term">child classes</span></a>.</p>
71+
</dd>
72+
</dl>
73+
</div>
6474
</div>
6575
</div>
6676

0 commit comments

Comments
 (0)