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
Copy file name to clipboardExpand all lines: 2_programs_in_files.html
+60-17Lines changed: 60 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -289,7 +289,20 @@ <h3><span class="section-number">2.4.3. </span>When not to use scripts<a class="
289
289
</div>
290
290
<divclass="section" id="modules">
291
291
<spanid="id2"></span><h2><spanclass="section-number">2.5. </span>Modules<aclass="headerlink" href="#modules" title="Permalink to this headline">¶</a></h2>
292
-
<p>A module is, like a script, a plain text file containing Python
292
+
<detailsclass="sphinx-bs dropdown card mb-3">
293
+
<summaryclass="summary-title card-header">
294
+
Video: a first Python module.<divclass="summary-down docutils">
<pclass="card-text">Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=972f92c6-6b55-4510-9c2c-ac8600fca11a">watch this video on Panopto</a></p>
304
+
</div>
305
+
</details><p>A module is, like a script, a plain text file containing Python
293
306
code. Modules must have names ending in <codeclass="file docutils literal notranslate"><spanclass="pre">.py</span></code>. So far, that’s
294
307
identical to a script. Indeed, it’s sometimes possible (though not
295
308
always advisable) to use the same file as both a script and a
@@ -348,7 +361,7 @@ <h3><span class="section-number">2.5.1. </span>Importing and namespaces<a class=
348
361
<p>Namespaces are a simple but fundamental concept in programming. To
349
362
quote one of the key developers of the Python language:</p>
350
363
<blockquote>
351
-
<div><p>Namespaces are one honking great idea – let’s do more of those! <aclass="footnote-reference brackets" href="#peters" id="id3">1</a></p>
364
+
<div><p>Namespaces are one honking great idea – let’s do more of those! <aclass="footnote-reference brackets" href="#peters" id="id4">1</a></p>
352
365
</div></blockquote>
353
366
<divclass="admonition note">
354
367
<pclass="admonition-title">Note</p>
@@ -426,7 +439,20 @@ <h3><span class="section-number">2.5.2. </span>Other forms of import<a class="he
426
439
</div>
427
440
<divclass="section" id="packages">
428
441
<h2><spanclass="section-number">2.6. </span>Packages<aclass="headerlink" href="#packages" title="Permalink to this headline">¶</a></h2>
429
-
<p>Modules are the principal mechanism for storing code which is intended
442
+
<detailsclass="sphinx-bs dropdown card mb-3">
443
+
<summaryclass="summary-title card-header">
444
+
Video: a first Python package.<divclass="summary-down docutils">
<pclass="card-text">Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=c4b0aedd-02a8-45d1-946b-ac86015b6d0b">watch this video on Panopto</a></p>
454
+
</div>
455
+
</details><p>Modules are the principal mechanism for storing code which is intended
430
456
to be used by other code. However, putting all of the code for a
431
457
complex area of mathematics in a single huge Python file is not a
432
458
great idea. Readers of that code will struggle to see the logical
<spanid="id4"></span><h3><spanclass="section-number">2.6.1. </span>Importing packages<aclass="headerlink" href="#importing-packages" title="Permalink to this headline">¶</a></h3>
516
+
<spanid="id6"></span><h3><spanclass="section-number">2.6.1. </span>Importing packages<aclass="headerlink" href="#importing-packages" title="Permalink to this headline">¶</a></h3>
491
517
<p>The system for importing packages is the same as that described in
492
518
<aclass="reference internal" href="#modules"><spanclass="std std-numref">Section 2.5</span></a>, though the nested nature of packages makes the
493
519
process somewhat more involved. Importing a package also imports all
@@ -595,15 +621,28 @@ <h3><span class="section-number">2.6.3. </span>Installing a package from local c
595
621
</div>
596
622
<divclass="section" id="testing-frameworks">
597
623
<h2><spanclass="section-number">2.7. </span>Testing frameworks<aclass="headerlink" href="#testing-frameworks" title="Permalink to this headline">¶</a></h2>
598
-
<p>Attempting to establish whether a program correctly implements the
<pclass="card-text">Imperial students can also <aclass="reference external" href="https://imperial.cloud.panopto.eu/Panopto/Pages/Viewer.aspx?id=c636383d-6125-4a7c-bad7-ac86015b6d4c">watch this video on Panopto</a></p>
636
+
</div>
637
+
</details><p>Attempting to establish whether a program correctly implements the
599
638
intended algorithm is core to effective programming, and programmers
600
639
often spend more time correcting bugs than writing new code. We will
601
640
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,
602
641
right from the start, we need to test the code we write, so we will cover
603
642
the practical details of including tests in your code here.</p>
604
643
<p>There are a number of Python packages which support code testing. The
605
644
concepts are largely similar so rather than get bogged down in the
606
-
details of multiple frameworks, we will introduce <aclass="reference external" href="https://docs.pytest.org/en/latest/index.html" title="(in pytest v0.1.dev212+g64bb5f2)"><spanclass="xref std std-doc">pytest</span></a>, which is one of the most widely used.</p>
645
+
details of multiple frameworks, we will introduce <aclass="reference external" href="https://docs.pytest.org/en/latest/index.html" title="(in pytest v0.1.dev244+g902739c)"><spanclass="xref std std-doc">pytest</span></a>, which is one of the most widely used.</p>
607
646
<divclass="section" id="pytest-tests">
608
647
<h3><spanclass="section-number">2.7.1. </span>Pytest tests<aclass="headerlink" href="#pytest-tests" title="Permalink to this headline">¶</a></h3>
609
648
<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
@@ -757,7 +796,7 @@ <h2><span class="section-number">2.8. </span>Writing code to a specified interfa
757
796
right names, accepting arguments of the correct type and so on. If it does not,
758
797
then the tests will simply fail. Changing the tests to suit your preferred
759
798
interface is not an acceptable answer, your code needs to comply with the
760
-
interface specified in the tests <aclass="footnote-reference brackets" href="#interface-errors" id="id5">2</a>.</p>
799
+
interface specified in the tests <aclass="footnote-reference brackets" href="#interface-errors" id="id8">2</a>.</p>
761
800
<p>This requirement to code to a published specification is not an artifact of the
762
801
testing framework: it is often the case that code written in a research or
763
802
business setting needs to conform with a standard or other published interface
@@ -797,7 +836,7 @@ <h2><span class="section-number">2.10. </span>Exercises<a class="headerlink" hre
797
836
<pclass="admonition-title">Note</p>
798
837
<p>Put a link to the first week quiz here.</p>
799
838
</div>
800
-
<divclass="proof proof-type-exercise" id="id6">
839
+
<divclass="proof proof-type-exercise" id="id9">
801
840
802
841
<divclass="proof-title">
803
842
<spanclass="proof-type">Exercise 2.1</span>
@@ -807,7 +846,7 @@ <h2><span class="section-number">2.10. </span>Exercises<a class="headerlink" hre
807
846
installation page</span></a> to create the folder structure
808
847
you will use for this course on your computer. Start with an overall folder
809
848
for the module, and create a virtual environment in that module.</p>
@@ -816,14 +855,18 @@ <h2><span class="section-number">2.10. </span>Exercises<a class="headerlink" hre
816
855
<p>Visit the <aclass="reference external" href="https://github.com/object-oriented-python/object-oriented-programming">GitHub repository for these notes</a>.
817
856
Clone that git repository into your course folder, and install the Python
818
857
package it contains into your virtual environment. Check that it has
819
-
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>You could also run iPython, import <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 try out
<p>The first GitHub classroom assignment for this module will be released at
824
867
the start of term.</p>
825
868
</div>
826
-
<divclass="proof proof-type-exercise" id="id8">
869
+
<divclass="proof proof-type-exercise" id="id11">
827
870
828
871
<divclass="proof-title">
829
872
<spanclass="proof-type">Exercise 2.3</span>
@@ -832,7 +875,7 @@ <h2><span class="section-number">2.10. </span>Exercises<a class="headerlink" hre
832
875
<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
833
876
repository just contains a <codeclass="file docutils literal notranslate"><spanclass="pre">README</span></code> and some tests. Your job in the
834
877
following exercises will be to populate it with the remaining content.</p>
@@ -861,7 +904,7 @@ <h2><span class="section-number">2.10. </span>Exercises<a class="headerlink" hre
861
904
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
@@ -870,7 +913,7 @@ <h2><span class="section-number">2.10. </span>Exercises<a class="headerlink" hre
870
913
<p>Following <aclass="reference internal" href="#installable-packages"><spanclass="std std-numref">Section 2.6.2</span></a>, create a <codeclass="file docutils literal notranslate"><spanclass="pre">setup.py</span></code> file in
871
914
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
0 commit comments