Skip to content

Commit c2ab582

Browse files
committed
Website build
1 parent 396d37e commit c2ab582

File tree

7 files changed

+150
-111
lines changed

7 files changed

+150
-111
lines changed

1_programs_in_files.html

Lines changed: 24 additions & 25 deletions
Large diffs are not rendered by default.

2_objects.html

Lines changed: 58 additions & 58 deletions
Large diffs are not rendered by default.

3_style.html

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,22 @@ <h2><span class="section-number">4.5. </span>Parsimony and modularity<a class="h
372372
<p class="admonition-title">Note</p>
373373
<p>Put in an example here of some horrific code that can be radically simplified.</p>
374374
</div>
375+
<div class="badcode docutils container">
376+
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="p">[]</span>
377+
378+
<span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">9999</span><span class="p">):</span>
379+
<span class="k">if</span> <span class="n">_</span> <span class="o">%</span> <span class="mi">1</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">_</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">_</span> <span class="o">%</span> <span class="mi">3</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">_</span> <span class="o">%</span> <span class="mi">4</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">_</span> <span class="o">%</span> <span class="mi">5</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">_</span> <span class="o">%</span> <span class="mi">6</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="n">_</span> <span class="o">%</span> <span class="mi">7</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
380+
<span class="n">result</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">_</span><span class="p">)</span>
381+
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
382+
</pre></div>
383+
</div>
384+
</div>
385+
<div class="goodcode docutils container">
386+
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="p">[</span><span class="n">num</span> <span class="k">for</span> <span class="n">num</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">9999</span><span class="p">)</span> <span class="k">if</span> <span class="nb">all</span><span class="p">(</span><span class="n">num</span> <span class="o">%</span> <span class="n">x</span> <span class="o">==</span> <span class="mi">0</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">8</span><span class="p">))]</span>
387+
<span class="nb">print</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
388+
</pre></div>
389+
</div>
390+
</div>
375391
<div class="section" id="use-comprehensions">
376392
<h3><span class="section-number">4.5.1. </span>Use comprehensions<a class="headerlink" href="#use-comprehensions" title="Permalink to this headline"></a></h3>
377393
<p>It is very common to write loops to populate collection objects with
@@ -386,7 +402,7 @@ <h3><span class="section-number">4.5.1. </span>Use comprehensions<a class="heade
386402
</div>
387403
<p>This is a fairly typical, if simple, example. It takes three lines of
388404
code: one to initialise the list, one to loop, and one to add the
389-
values to the list. Alternatively, if we had used a <a class="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-listcomps" title="(in Python v3.8)"><span class="xref std std-ref">list
405+
values to the list. Alternatively, if we had used a <a class="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-listcomps" title="(in Python v3.9)"><span class="xref std std-ref">list
390406
comprehension</span></a>, all three steps would have been subsumed into a single
391407
operation:</p>
392408
<div class="goodcode docutils container">
@@ -396,8 +412,8 @@ <h3><span class="section-number">4.5.1. </span>Use comprehensions<a class="heade
396412
</div>
397413
<p>At least for fairly simple operations, comprehensions are almost
398414
always easier for the reader to understand than loops. In addition to
399-
lists, comprehensions are also available for <a class="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-sets" title="(in Python v3.8)"><span class="xref std std-ref">sets</span></a>
400-
and <a class="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-dictionaries" title="(in Python v3.8)"><span class="xref std std-ref">dictionaries</span></a>.</p>
415+
lists, comprehensions are also available for <a class="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-sets" title="(in Python v3.9)"><span class="xref std std-ref">sets</span></a>
416+
and <a class="reference external" href="https://docs.python.org/3/tutorial/datastructures.html#tut-dictionaries" title="(in Python v3.9)"><span class="xref std std-ref">dictionaries</span></a>.</p>
401417
</div>
402418
<div class="section" id="redundant-logical-expressions">
403419
<h3><span class="section-number">4.5.2. </span>Redundant logical expressions<a class="headerlink" href="#redundant-logical-expressions" title="Permalink to this headline"></a></h3>
@@ -408,7 +424,7 @@ <h3><span class="section-number">4.5.2. </span>Redundant logical expressions<a c
408424
</div>
409425
</div>
410426
<p>To see the problem with this statement, let’s write out its truth table:</p>
411-
<table class="docutils align-default">
427+
<table class="center-align-center-col docutils align-default">
412428
<colgroup>
413429
<col style="width: 28%" />
414430
<col style="width: 72%" />
@@ -428,7 +444,7 @@ <h3><span class="section-number">4.5.2. </span>Redundant logical expressions<a c
428444
</tbody>
429445
</table>
430446
<p>In other words, the expressions <code class="xref py py-obj docutils literal notranslate"><span class="pre">var</span></code> and <code class="xref py py-obj docutils literal notranslate"><span class="pre">var</span> <span class="pre">==</span> <span class="pre">True</span></code> are logically
431-
equivalent (at least assuming <code class="xref py py-obj docutils literal notranslate"><span class="pre">var</span></code> is a <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#bltin-boolean-values" title="(in Python v3.8)"><span class="xref std std-ref">boolean value</span></a>), so it would
447+
equivalent (at least assuming <code class="xref py py-obj docutils literal notranslate"><span class="pre">var</span></code> is a <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#bltin-boolean-values" title="(in Python v3.9)"><span class="xref std std-ref">boolean value</span></a>), so it would
432448
have been more parsimonious to write:</p>
433449
<div class="goodcode docutils container">
434450
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="n">var</span><span class="p">:</span>
@@ -447,7 +463,7 @@ <h3><span class="section-number">4.5.2. </span>Redundant logical expressions<a c
447463
</pre></div>
448464
</div>
449465
</div>
450-
<p>Finally, the use of <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#else" title="(in Python v3.8)"><span class="xref std std-ref">else</span></a> (or <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#elif" title="(in Python v3.8)"><span class="xref std std-ref">elif</span></a>) can reduce the number
466+
<p>Finally, the use of <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#else" title="(in Python v3.9)"><span class="xref std std-ref">else</span></a> (or <a class="reference external" href="https://docs.python.org/3/reference/compound_stmts.html#elif" title="(in Python v3.9)"><span class="xref std std-ref">elif</span></a>) can reduce the number
451467
of logical expressions that the reader has to read and
452468
understand. This means that:</p>
453469
<div class="badcode docutils container">
@@ -474,7 +490,7 @@ <h3><span class="section-number">4.5.2. </span>Redundant logical expressions<a c
474490
</div>
475491
<div class="section" id="use-the-fact-that-every-object-is-true-or-false">
476492
<h3><span class="section-number">4.5.3. </span>Use the fact that every object is True or False<a class="headerlink" href="#use-the-fact-that-every-object-is-true-or-false" title="Permalink to this headline"></a></h3>
477-
<p>Every Python object is logically either <a class="reference external" href="https://docs.python.org/3/library/constants.html#True" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">True</span></code></a> or <a class="reference external" href="https://docs.python.org/3/library/constants.html#False" title="(in Python v3.8)"><code class="xref py py-data docutils literal notranslate"><span class="pre">False</span></code></a> according to the
493+
<p>Every Python object is logically either <a class="reference external" href="https://docs.python.org/3/library/constants.html#True" title="(in Python v3.9)"><code class="xref py py-data docutils literal notranslate"><span class="pre">True</span></code></a> or <a class="reference external" href="https://docs.python.org/3/library/constants.html#False" title="(in Python v3.9)"><code class="xref py py-data docutils literal notranslate"><span class="pre">False</span></code></a> according to the
478494
following rules:</p>
479495
<ol class="arabic simple">
480496
<li><p>None is False.</p></li>
@@ -484,16 +500,16 @@ <h3><span class="section-number">4.5.3. </span>Use the fact that every object is
484500
<li><p>The null string <code class="xref py py-obj docutils literal notranslate"><span class="pre">&quot;&quot;</span></code> is False, a string containing any characters is True.</p></li>
485501
<li><p>A user-defined class is True unless:</p>
486502
<ol class="loweralpha simple">
487-
<li><p>It defines the <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__bool__" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__bool__()</span></code></a> <a class="reference internal" href="2_objects.html#term-special-method"><span class="xref std std-term">special
503+
<li><p>It defines the <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__bool__" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__bool__()</span></code></a> <a class="reference internal" href="2_objects.html#term-special-method"><span class="xref std std-term">special
488504
method</span></a>. In this case the truth value is whatever this method
489505
returns.</p></li>
490-
<li><p>It doesn’t define <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__bool__" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__bool__()</span></code></a> but does define
491-
<a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__len__" title="(in Python v3.8)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__len__()</span></code></a>. In this case the object is False if the
506+
<li><p>It doesn’t define <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__bool__" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__bool__()</span></code></a> but does define
507+
<a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__len__" title="(in Python v3.9)"><code class="xref py py-meth docutils literal notranslate"><span class="pre">__len__()</span></code></a>. In this case the object is False if the
492508
length is zero, and True otherwise.</p></li>
493509
</ol>
494510
</li>
495511
</ol>
496-
<p>These rules are laid out formally in <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#truth" title="(in Python v3.8)"><span class="xref std std-ref">the Python documentation</span></a>. One way that they can be used to write simpler, clearer code
512+
<p>These rules are laid out formally in <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#truth" title="(in Python v3.9)"><span class="xref std std-ref">the Python documentation</span></a>. One way that they can be used to write simpler, clearer code
497513
is in the very common case of code that should only execute if a
498514
collection object actually contains something. In that case, this form
499515
of test is to be preferred:</p>

_sources/1_programs_in_files.rst.txt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ internal module structure of a package in a logical way while still
425425
providing users with direct access to the most important or most
426426
frequently used features.
427427

428-
The eagle-eyed reader will have noticed the extra `.` in front of
428+
The eagle-eyed reader will have noticed the extra . in front of
429429
`.module_1`. This marks this import as a *relative import*. In other
430430
words, in looking for `module_1.py`, Python should look for files in
431431
the same folder as the module where the import statement occurs,
@@ -444,15 +444,14 @@ reader that the import is from the current package.
444444
Making packages installable
445445
~~~~~~~~~~~~~~~~~~~~~~~~~~~
446446

447-
In order for the :ref:`import statement <python:import>` to work,
448-
Python needs to know about the package being imported. This is
449-
achieved by installing the package. In order to make a package
450-
installable, we need to provide Python with a bit more information
451-
about the package. This information is contained in a Python module
452-
which must be called `setup.py`. This file isn't part of the package
453-
and does not go in the package folder. Instead, it should be placed in
454-
the top-level folder of your git repository, so that the Python
455-
package installer will be able to find it.
447+
In order for the :ref:`import statement <python:import>` to work, Python needs
448+
to know that the package being imported exists, and where to find it. This is
449+
achieved by *installing* the package. In order to make a package installable, we
450+
need to provide Python with a bit more information about the package. This
451+
information is contained in a Python module which must be called `setup.py`.
452+
This file isn't part of the package and does not go in the package folder.
453+
Instead, it should be placed in the top-level folder of your git repository, so
454+
that the Python package installer will be able to find it.
456455

457456
At the very least, `setup.py` should contain the following:
458457

_sources/3_style.rst.txt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,24 @@ function or method is small enough for a reader to understand.
341341

342342
Put in an example here of some horrific code that can be radically simplified.
343343

344+
.. container:: badcode
345+
346+
.. code-block:: python3
347+
348+
result = []
349+
350+
for _ in range(1, 9999):
351+
if _ % 1 == 0 and _ % 2 == 0 and _ % 3 == 0 and _ % 4 == 0 and _ % 5 == 0 and _ % 6 == 0 and _ % 7 == 0:
352+
result.append(_)
353+
print(result)
354+
355+
.. container:: goodcode
356+
357+
.. code-block:: python3
358+
359+
result = [num for num in range(1, 9999) if all(num % x == 0 for x in range(1, 8))]
360+
print(result)
361+
344362
Use comprehensions
345363
..................
346364

@@ -386,12 +404,14 @@ One exceptionally common failure of parsimony is to write expressions of the fol
386404
387405
To see the problem with this statement, let's write out its truth table:
388406

389-
===== =============
390-
`var` `var == True`
391-
===== =============
392-
T T
393-
F F
394-
===== =============
407+
.. rst-class:: center-align-center-col
408+
409+
===== =============
410+
`var` `var == True`
411+
===== =============
412+
T T
413+
F F
414+
===== =============
395415

396416
In other words, the expressions `var` and `var == True` are logically
397417
equivalent (at least assuming `var` is a :ref:`boolean value <bltin-boolean-values>`), so it would

_static/fenics.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ table.imagegrid td{
371371
text-align: center;
372372
}
373373

374+
table.center-align-center-col td:last-child {
375+
text-align: center;
376+
width: 200%;
377+
}
378+
374379
table.docutils {
375380
margin: auto;
376381
}

searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)