@@ -5,7 +5,7 @@ Debugging and testing
5
5
6
6
In :numref: `Chapter %s <errors_and_exceptions >` we learned about
7
7
:term: `exceptions <exception> ` and how to read the :term: `traceback ` that is
8
- printed when an unhandled exception is raised. This week we will look at other
8
+ printed when an unhandled exception is raised. In this chapter we will look at other
9
9
tools and techniques that we can use to understand what is wrong with a piece
10
10
of code, and therefore how to fix it. Before we do that, we'll divert just
11
11
briefly to introduce an important Python module that we'll use in a lot of the
@@ -84,7 +84,7 @@ Observe that the :class:`~pandas.DataFrame` acts as a dictionary of
84
84
one-dimensional data :class: `~pandas.Series `. A :class: `pandas.Series ` can be
85
85
indexed and sliced like any other Python :ref: `sequence type <typesseq >`. This
86
86
very high level introduction is all we'll need to use pandas in demonstrations
87
- this week . Much more documentation is available on the `pandas website <https://pandas.pydata.org/docs/ >`__.
87
+ in this chapter . Much more documentation is available on the `pandas website <https://pandas.pydata.org/docs/ >`__.
88
88
89
89
.. note ::
90
90
@@ -120,7 +120,7 @@ advantage that integrates well with IPython. Another advanced command-line
120
120
debugger is `pdb++
121
121
<https://github.com/pdbpp/pdbpp#pdb-a-drop-in-replacement-for-pdb> `__. The
122
122
distinct advantage of pdb++ is that it replaces the built-in pdb. Among other
123
- things, this means it can be triggered from a failed `pytest
123
+ things, this means it can be triggered from a failed `Pytest
124
124
<https://docs.pytest.org/en/stable/> `__ test.
125
125
126
126
The alternative to a command-line debugger is to use a graphical debugger
@@ -650,15 +650,15 @@ and fails if it is.
650
650
Creating a test command
651
651
~~~~~~~~~~~~~~~~~~~~~~~
652
652
653
- Since pytest provides a framework for creating programs which succeed or fail,
653
+ Since Pytest provides a framework for creating programs which succeed or fail,
654
654
one approach is write the test that we wish had existed at the time the bug
655
655
slipped into our code. The bisection search effectively enables us to
656
656
retrospectively introduce this test into our repository. Because we're going to
657
657
be rolling back the state of our repository to before we created this command,
658
658
this is one exception to the rule that you must always commit all of your work
659
659
to the git repository. Make a copy of this command (for example the Python file
660
- containing the pytest test) outside your repository. For the rest of this
661
- section, we'll assume that you've created a pytest test in a file called
660
+ containing the Pytest test) outside your repository. For the rest of this
661
+ section, we'll assume that you've created a Pytest test in a file called
662
662
:file: `bug_test.py ` which you have placed in the folder containing your
663
663
repository (if you followed the instructions in :numref: `Chapter %s
664
664
<programs_files>` then this folder might be called
@@ -830,49 +830,85 @@ Glossary
830
830
Exercises
831
831
---------
832
832
833
- The exercises work a little differently this week, because the objective is not
834
- to write code but to practice debugging techniques. The quiz is not on
835
- BlackBoard but is instead a Google form, because that offers instant feedback.
836
- You should work through the the exercises and quiz together. For most of exercises,
837
- there are quiz questions which you will be able to answer if you are
838
- successfully able to do the exercise.
833
+ .. only :: not book
839
834
840
- Obtain the `skeleton code for these exercises from GitHub classroom
841
- <https://classroom.github.com/a/mi6I-jcG> `__.
835
+ The exercises work a little differently this week, because the objective is
836
+ not to write code but to practice debugging techniques. The information on
837
+ the `book website
838
+ <https://object-oriented-programming.github.io/edition1/exercises.html> `__
839
+ points not just to the skeleton code but also to an online quiz which will
840
+ provide instant feedback on the questions below. You should access the
841
+ skeleton code and then work through the quiz questions.
842
842
843
- .. panels ::
844
- :card: quiz shadow
843
+ .. only :: book
845
844
846
- .. link-button :: https://forms.gle/cL5eZycNC9Js19uL7
847
- :text: This week's quiz
848
- :classes: stretched-link
845
+ The exercises work a little differently this week, because the objective is
846
+ not to write code but to practice debugging techniques. The information on
847
+ the book website [#exercise_page ]_
848
+ points not just to the skeleton code but also to an online quiz which will
849
+ provide instant feedback on the questions below. You should access the
850
+ skeleton code and then work through exercises, using the online quiz to
851
+ check your answers.
849
852
850
853
.. proof :exercise :: Debugging python code
851
854
852
855
The skeleton code contains a Python script :file: `scripts/tests_report `.
853
- Run this script under the Visual Studio code debugger and answer the quiz
854
- questions about what you find.
856
+ Run this script under the Visual Studio code debugger and answer the
857
+ following questions about what you find. Entering the answers into the
858
+ online quiz will tell you if you are correct.
859
+
860
+ 1. On which line of the file does the exception occur?
861
+ 2. How many stack frames are there on the call stack when the exception occurs?
862
+ 3. What is the exact value of the variable t?
855
863
856
864
.. proof :exercise :: Minimal failing example
857
865
858
866
In the file :file: `scripts/tests_report_mfe.py ` construct a :term: `minimal failing
859
867
example ` which exhibits the error you discovered in the previous section.
860
868
Your minimal failing example should contain one import and one other line
861
- of code. :file: `tests/test_mfe.py ` is a pytest test for this exercise.
869
+ of code. :file: `tests/test_mfe.py ` is a Pytest test for this exercise.
870
+
871
+ .. only :: book
872
+
873
+ .. raw :: latex
874
+
875
+ \clearpage
862
876
863
877
.. proof :exercise :: Bisection
864
878
865
- The Unified Form Language (UFL) is a computer symbolic algebra package used to
866
- represent partial differential equations in software applying a numerical
867
- technique called the finite element method. Clone the `course fork of the
868
- UFL repository <https://github.com/object-oriented-python/ufl> `__. At some
869
- point in the past, the following code worked:
879
+ .. only :: not book
880
+
881
+ The Unified Form Language (UFL) is a computer symbolic algebra package
882
+ used to represent partial differential equations in software applying a
883
+ numerical technique called the finite element method. Clone the `course
884
+ fork of the UFL repository
885
+ <https://github.com/object-oriented-python/ufl> `__. At some point in
886
+ the past, the following code worked:
887
+
888
+ .. only :: book
889
+
890
+ The Unified Form Language (UFL) is a computer symbolic algebra package
891
+ used to represent partial differential equations in software applying a
892
+ numerical technique called the finite element method. Clone the course
893
+ fork of the UFL repository [#ufl ]_. At some point in
894
+ the past, the following code worked:
870
895
871
896
.. code-block :: python3
872
897
873
898
import ufl
874
899
argyris = ufl.FiniteElement("Argyris", degree=6, cell=ufl.triangle)
875
900
876
901
Use `git bisect ` to identify the first commit at which this code failed,
877
- and the last commit at which it worked, and answer the corresponding quiz
878
- questions.
902
+ and the last commit at which it worked, and answer the following questions.
903
+ The online quiz will tell you if you are correct.
904
+
905
+ 1. What is the commit ID of the first bad commit?
906
+ 2. What is the commit ID of the last good commit?
907
+
908
+ .. rubric :: Footnotes
909
+
910
+ .. [#ufl ] `https://github.com/object-oriented-python/ufl
911
+ <https://github.com/object-oriented-python/ufl> `__
912
+
913
+ .. [#exercise_page ] `https://object-oriented-programming.github.io/edition1/exercises.html
914
+ <https://object-oriented-programming.github.io/edition1/exercises.html> `__
0 commit comments