You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<h1><spanclass="section-number">9. </span>Trees and directed acyclic graphs<aclass="headerlink" href="#trees-and-directed-acyclic-graphs" title="Permalink to this headline">¶</a></h1>
<h1><spanclass="section-number">11. </span>Further object-oriented features<aclass="headerlink" href="#further-object-oriented-features" title="Permalink to this headline">¶</a></h1>
53
+
<divclass="section" id="decorators">
54
+
<h2><spanclass="section-number">11.1. </span>Decorators<aclass="headerlink" href="#decorators" title="Permalink to this headline">¶</a></h2>
55
+
</div>
56
+
<divclass="section" id="abstract-classes">
57
+
<h2><spanclass="section-number">11.2. </span>Abstract classes<aclass="headerlink" href="#abstract-classes" title="Permalink to this headline">¶</a></h2>
58
+
</div>
59
+
<divclass="section" id="abstract-base-classes">
60
+
<spanid="id1"></span><h2><spanclass="section-number">11.3. </span>Abstract base classes<aclass="headerlink" href="#abstract-base-classes" title="Permalink to this headline">¶</a></h2>
<h1><spanclass="section-number">2. </span>Programs in files<aclass="headerlink" href="#programs-in-files" title="Permalink to this headline">¶</a></h1>
54
-
<p>This chapter is about how to combine pieces of code into larger units,
54
+
<p>This week we will start to learn how to combine pieces of code into larger units,
55
55
and how to package up your code so that you or others can do the same.
56
56
You will previously have written Python code in Jupyter notebooks, and
57
57
possibly used an interactive Python environment such as
<h3><spanclass="section-number">2.2.3. </span>When not to use scripts<aclass="headerlink" href="#when-not-to-use-scripts" title="Permalink to this headline">¶</a></h3>
@@ -294,7 +296,7 @@ <h3><span class="section-number">2.3.2. </span>Other forms of import<a class="he
294
296
are to be used frequently or in the middle of formulae: you probably
295
297
don’t really want to write <aclass="reference external" href="https://docs.python.org/3/library/math.html#math.sin" title="(in Python v3.9)"><codeclass="xref py py-func docutils literal notranslate"><spanclass="pre">math.sin()</span></code></a> in every trig formula you
296
298
ever write. One alternative is to rename the module on import. This is
297
-
achieved using the keyword <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">as</span></code> in an import statement. For example,
299
+
achieved using the keyword <aclass="reference external" href="https://docs.python.org/3/reference/simple_stmts.html#import" title="(in Python v3.9)"><codeclass="xref std std-keyword docutils literal notranslate"><spanclass="pre">as</span></code></a> in an import statement. For example,
298
300
it is usual to import the numerical Python module <aclass="reference external" href="https://numpy.org/doc/stable/reference/index.html#module-numpy" title="(in NumPy v1.19)"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">numpy</span></code></a> in the
<p>Attempting to establish whether a program correctly implements the
568
570
intended algorithm is core to effective programming, and programmers
569
571
often spend more time correcting bugs than writing new code. We will
570
-
turn to the question of how to debug in <aclass="reference internal" href="7_debugging.html#debugging"><spanclass="std std-numref">Section 8</span></a>. However,
572
+
turn to the question of how to debug in <aclass="reference internal" href="9_debugging.html#debugging"><spanclass="std std-numref">Section 9</span></a>. However,
571
573
right from the start, we need to test the code we write, so we will cover
572
574
the practical details of including tests in your code here.</p>
573
575
<p>There are a number of Python packages which support code testing. The
574
576
concepts are largely similar so rather than get bogged down in the
575
-
details of multiple frameworks, we will introduce <aclass="reference external" href="https://docs.pytest.org/en/latest/index.html" title="(in pytest v0.1.dev354+ga95da7a)"><spanclass="xref std std-doc">pytest</span></a>, which is one of the most widely used.</p>
577
+
details of multiple frameworks, we will introduce <aclass="reference external" href="https://docs.pytest.org/en/latest/index.html" title="(in pytest v6.2.0.dev230+g775ba63c6)"><spanclass="xref std std-doc">pytest</span></a>, which is one of the most widely used.</p>
576
578
<divclass="section" id="pytest-tests">
577
579
<h3><spanclass="section-number">2.7.1. </span>Pytest tests<aclass="headerlink" href="#pytest-tests" title="Permalink to this headline">¶</a></h3>
578
580
<p>A Pytest test is simply a function whose name starts with <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">test_</span></code>. In
<p>Here we can see an <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">F</span></code> after <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">tests/test_fibonacci.py</span></code> indicating
666
668
that the test failed, and we see some output detailing what went
667
-
wrong. We will learn how to interpret this output in <aclass="reference internal" href="7_debugging.html#debugging"><spanclass="std std-numref">Section 8</span></a>.</p>
669
+
wrong. We will learn how to interpret this output in <aclass="reference internal" href="9_debugging.html#debugging"><spanclass="std std-numref">Section 9</span></a>.</p>
<h3><spanclass="section-number">2.7.3. </span>Additional useful pytest tricks<aclass="headerlink" href="#additional-useful-pytest-tricks" title="Permalink to this headline">¶</a></h3>
series of tests to fail, resulting in a very long list of error messages which
683
685
is hard to read. A useful tool in this circumstance is the <codeclass="xref py py-obj docutils literal notranslate"><spanclass="pre">-x</span></code> option, which
684
686
tells pytest to stop after the first test fail. For example:</p>
<p>Visit the <aclass="reference external" href="https://github.com/object-oriented-python/object-oriented-programming">GitHub repository for these notes</a>.
788
+
Clone that git repository into your course folder, and install the Python
789
+
package it contains into your virtual environment. Check that it has
790
+
installed correctly by running IPython, importing <aclass="reference internal" href="fibonacci.html#module-fibonacci" title="fibonacci"><codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">fibonacci</span></code></a> and
<p>The first GitHub classroom assignment for this module will be released at
773
795
the start of term.</p>
774
796
</div>
775
-
<divclass="proof proof-type-exercise" id="id6">
797
+
<divclass="proof proof-type-exercise" id="id7">
776
798
777
799
<divclass="proof-title">
778
-
<spanclass="proof-type">Exercise 2.2</span>
800
+
<spanclass="proof-type">Exercise 2.3</span>
779
801
780
802
</div><divclass="proof-content">
781
803
<p>Accept the <aclass="reference internal" href="not_released.html"><spanclass="doc">first Github Classroom assignment for this module</span></a> and clone it into your course folder. The assignment
782
804
repository just contains a <codeclass="file docutils literal notranslate"><spanclass="pre">README</span></code> and some tests. Your job in the
783
805
following exercises will be to populate it with the remaining content.</p>
<p>Create a new Python <aclass="reference internal" href="#term-package"><spanclass="xref std std-term">package</span></a> named <codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">math_utils</span></code> containing a
@@ -802,19 +824,27 @@ <h2><span class="section-number">2.10. </span>Exercises<a class="headerlink" hre
<p>After this and every exercise in which you write code, ensure that you
830
+
add any new files to git, commit all of your changes, and push to
831
+
GitHub. Then ensure that the tests pass on GitHub. For more information
832
+
about how to do any of these, refer back the <aclass="reference external" href="https://imperial-fons-computing.github.io/git.html#github-classroom-exercise" title="(in Installation instructions for FoNS v2020.0)"><spanclass="xref std std-ref">Faculty of Natural Sciences
<p>Following <aclass="reference internal" href="#installable-packages"><spanclass="std std-numref">Section 2.4.2</span></a>, create a <codeclass="file docutils literal notranslate"><spanclass="pre">setup.py</span></code> file in
812
842
your exercise repository, so that the <codeclass="xref py py-mod docutils literal notranslate"><spanclass="pre">math_utils</span></code><aclass="reference internal" href="#term-package"><spanclass="xref std std-term">package</span></a> is
<p>Add an <aclass="reference external" href="https://docs.python.org/3/reference/simple_stmts.html#import" title="(in Python v3.9)"><codeclass="xref std std-keyword docutils literal notranslate"><spanclass="pre">import</span></code></a> to <codeclass="file docutils literal notranslate"><spanclass="pre">math_utils.__init__.py</span></code> so that the following
0 commit comments