Skip to content

Commit 4bb5a56

Browse files
author
Quarto GHA Workflow Runner
committed
Built site for gh-pages
1 parent ebf324f commit 4bb5a56

4 files changed

Lines changed: 180 additions & 38 deletions

File tree

.nojekyll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7d8e3ae9
1+
417f008a

111_debugging.html

Lines changed: 139 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,8 @@ <h2 id="toc-title">Table of contents</h2>
376376
<li><a href="#what-is-a-debugger" id="toc-what-is-a-debugger" class="nav-link active" data-scroll-target="#what-is-a-debugger"><span class="header-section-number">13.1</span> What is a debugger?</a></li>
377377
<li><a href="#python-debugger-breakpoints" id="toc-python-debugger-breakpoints" class="nav-link" data-scroll-target="#python-debugger-breakpoints"><span class="header-section-number">13.2</span> Python debugger, breakpoints</a></li>
378378
<li><a href="#ipython-debugger" id="toc-ipython-debugger" class="nav-link" data-scroll-target="#ipython-debugger"><span class="header-section-number">13.3</span> IPython debugger</a></li>
379-
<li><a href="#notebook-debug" id="toc-notebook-debug" class="nav-link" data-scroll-target="#notebook-debug"><span class="header-section-number">13.4</span> Notebook %%debug</a></li>
379+
<li><a href="#notebook-debug" id="toc-notebook-debug" class="nav-link" data-scroll-target="#notebook-debug"><span class="header-section-number">13.4</span> Notebook %debug</a></li>
380+
<li><a href="#exercises" id="toc-exercises" class="nav-link" data-scroll-target="#exercises"><span class="header-section-number">13.5</span> Exercises</a></li>
380381
</ul>
381382
</nav>
382383
</div>
@@ -402,24 +403,155 @@ <h1 class="title"><span class="chapter-number">13</span>&nbsp; <span class="chap
402403
</header>
403404

404405

405-
<p>We are humans, we make mistakes – that’s very much true for coding. Finding errors in our code is fundamental skill to train.</p>
406-
<p>We will cover here a few tools that help us find and fix errors more quickly.</p>
406+
<p>We are humans, we make mistakes – that’s very much true for coding.</p>
407+
<p>Finding errors in our code is fundamental skill to train so we will cover here a few tools that help us find and fix errors more quickly.</p>
407408
<section id="what-is-a-debugger" class="level2" data-number="13.1">
408409
<h2 data-number="13.1" class="anchored" data-anchor-id="what-is-a-debugger"><span class="header-section-number">13.1</span> What is a debugger?</h2>
409410
<p>A debugger is a device that allows us to interrupt code execution and jump into the execution context in a interactive mode, so that we can inspect and run code to find out what’s going on.<br>
410411
We called that to “set a breakpoint” or “set a trace”.</p>
411-
<p>There are a few options to do that natively in python.</p>
412+
<p>There are a few options to do that in python.</p>
412413
</section>
413414
<section id="python-debugger-breakpoints" class="level2" data-number="13.2">
414415
<h2 data-number="13.2" class="anchored" data-anchor-id="python-debugger-breakpoints"><span class="header-section-number">13.2</span> Python debugger, breakpoints</h2>
415-
<p>The python standard library includes <code>pdb</code> module.</p>
416-
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pdb<span class="op">;</span> pdb.set_trace()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
416+
<p>The python standard library includes <code>pdb</code> module. If you call this function in your code you will be put into an interactive session exactly at that point.</p>
417+
<p>Any of the following options would work:</p>
418+
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb1"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="co"># your code</span></span>
419+
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a><span class="co"># your code</span></span>
420+
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pdb</span>
421+
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>pdb.set_trace()</span>
422+
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a><span class="co"># your code</span></span>
423+
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a><span class="co"># your code</span></span>
424+
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a><span class="bu">breakpoint</span>()</span>
425+
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a><span class="co"># your code</span></span>
426+
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a><span class="co"># your code</span></span>
427+
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a><span class="co"># your code</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
428+
<p>Once in the debugger, we have several functions to proceed with code execution with more granular control.</p>
429+
<p>There are several options, but the most commonly used methods are:</p>
430+
<ul>
431+
<li>n (next): Continue execution one line, stay in the current function (step over)</li>
432+
<li>s (step): Execute current line and stop in a foreign function if one is called (step into)</li>
433+
<li>c (continue): Continue whole code execution until a new breakpoint is found</li>
434+
</ul>
417435
</section>
418436
<section id="ipython-debugger" class="level2" data-number="13.3">
419437
<h2 data-number="13.3" class="anchored" data-anchor-id="ipython-debugger"><span class="header-section-number">13.3</span> IPython debugger</h2>
438+
<p>The standard python debugger is fine but a bit basic, so sometimes the IPython debugger is a friendlier option.</p>
439+
<p>We need to install it:</p>
440+
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb2"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="ex">uv</span> add ipdb</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
441+
<p>Then we can do:</p>
442+
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb3"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> ipdb</span>
443+
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a>ipdb.set_trace()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
420444
</section>
421445
<section id="notebook-debug" class="level2" data-number="13.4">
422-
<h2 data-number="13.4" class="anchored" data-anchor-id="notebook-debug"><span class="header-section-number">13.4</span> Notebook %%debug</h2>
446+
<h2 data-number="13.4" class="anchored" data-anchor-id="notebook-debug"><span class="header-section-number">13.4</span> Notebook %debug</h2>
447+
<p>Inside the jupyter notebook we can directly jump into a debugger when there is an error.</p>
448+
<p>If a cell throws an error, you can type this “ipython magic method” in the following cell:</p>
449+
<div class="code-copy-outer-scaffold"><div class="sourceCode" id="cb4"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a><span class="op">%</span>debug</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
450+
<div id="76c6445c-3cc0-4bc6-b602-97cd5707feee" class="cell" data-execution_count="1">
451+
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb5"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> wrong_func():</span>
452+
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> a <span class="op">=</span> <span class="dv">1</span></span>
453+
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a> <span class="cf">assert</span> a <span class="op">==</span> <span class="dv">0</span></span>
454+
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a></span>
455+
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a>wrong_func()</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
456+
<div class="cell-output cell-output-error">
457+
<div class="ansi-escaped-output">
458+
<pre><span class="ansi-red-fg">---------------------------------------------------------------------------</span>
459+
<span class="ansi-red-fg">AssertionError</span> Traceback (most recent call last)
460+
<span class="ansi-cyan-fg">Cell</span><span class="ansi-cyan-fg"> </span><span class="ansi-green-fg">In[1]</span><span class="ansi-green-fg">, line 5</span>
461+
<span class="ansi-green-fg"> 2</span> a = <span class="ansi-green-fg">1</span>
462+
<span class="ansi-green-fg"> 3</span> <span style="font-weight:bold;color:rgb(0,135,0)">assert</span> a == <span class="ansi-green-fg">0</span>
463+
<span class="ansi-green-fg">----&gt; </span><span class="ansi-green-fg">5</span> <span class="ansi-yellow-bg">wrong_func</span><span class="ansi-yellow-bg">(</span><span class="ansi-yellow-bg">)</span>
464+
465+
<span class="ansi-cyan-fg">Cell</span><span class="ansi-cyan-fg"> </span><span class="ansi-green-fg">In[1]</span><span class="ansi-green-fg">, line 3</span>, in <span class="ansi-cyan-fg">wrong_func</span><span class="ansi-blue-fg">()</span>
466+
<span class="ansi-green-fg"> 1</span> <span style="font-weight:bold;color:rgb(0,135,0)">def</span><span style="color:rgb(188,188,188)"> </span><span class="ansi-blue-fg">wrong_func</span>():
467+
<span class="ansi-green-fg"> 2</span> a = <span class="ansi-green-fg">1</span>
468+
<span class="ansi-green-fg">----&gt; </span><span class="ansi-green-fg">3</span> <span style="font-weight:bold;color:rgb(0,135,0)">assert</span> a == <span class="ansi-green-fg">0</span>
469+
470+
<span class="ansi-red-fg">AssertionError</span>: </pre>
471+
</div>
472+
</div>
473+
</div>
474+
<div id="9225666f-7cce-4bec-96ac-c5fc1f167968" class="cell" data-execution_count="2">
475+
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb6"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a><span class="op">%</span>debug</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
476+
<div class="cell-output cell-output-stdout">
477+
<div class="ansi-escaped-output">
478+
<pre>&gt; <span class="ansi-green-fg">/tmp/ipykernel_81865/3667203143.py</span>(<span class="ansi-bright-green-fg">3</span>)<span class="ansi-cyan-fg">wrong_func</span><span class="ansi-blue-fg">()</span>
479+
480+
<span class="ansi-green-fg"> 1</span> <span style="font-weight:bold;color:rgb(0,135,0)">def</span> wrong_func():
481+
482+
<span class="ansi-green-fg"> 2</span> a = <span class="ansi-green-fg">1</span>
483+
484+
<span class="ansi-green-fg">----&gt; 3</span> <span style="font-weight:bold;color:rgb(0,135,0)">assert</span> a == <span class="ansi-green-fg">0</span>
485+
486+
<span class="ansi-green-fg"> 4</span>
487+
488+
<span class="ansi-green-fg"> 5</span> wrong_func()
489+
490+
491+
</pre>
492+
</div>
493+
</div>
494+
<div class="cell-output cell-output-stdin">
495+
<pre><code>ipdb&gt; print(a)</code></pre>
496+
</div>
497+
<div class="cell-output cell-output-stdout">
498+
<pre><code>1</code></pre>
499+
</div>
500+
<div class="cell-output cell-output-stdin">
501+
<pre><code>ipdb&gt; exit</code></pre>
502+
</div>
503+
</div>
504+
<p>Notice that we typed “exit” to get out of the debugger.</p>
505+
</section>
506+
<section id="exercises" class="level2" data-number="13.5">
507+
<h2 data-number="13.5" class="anchored" data-anchor-id="exercises"><span class="header-section-number">13.5</span> Exercises</h2>
508+
<p>Here’s a piece of code that will fail at run-time:</p>
509+
<div id="abb2830e-8f39-4bad-9f1c-cc0dd697f09c" class="cell" data-execution_count="8">
510+
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb10"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> f(p):</span>
511+
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a> <span class="cf">assert</span> p <span class="op">==</span> <span class="dv">0</span></span>
512+
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a> </span>
513+
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="kw">def</span> main():</span>
514+
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a> a <span class="op">=</span> <span class="dv">0</span></span>
515+
<span id="cb10-6"><a href="#cb10-6" aria-hidden="true" tabindex="-1"></a> f(a)</span>
516+
<span id="cb10-7"><a href="#cb10-7" aria-hidden="true" tabindex="-1"></a> b <span class="op">=</span> <span class="dv">1</span> </span>
517+
<span id="cb10-8"><a href="#cb10-8" aria-hidden="true" tabindex="-1"></a> f(b)</span>
518+
<span id="cb10-9"><a href="#cb10-9" aria-hidden="true" tabindex="-1"></a> c <span class="op">=</span> <span class="dv">0</span></span>
519+
<span id="cb10-10"><a href="#cb10-10" aria-hidden="true" tabindex="-1"></a> f(c)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
520+
</div>
521+
<ol type="1">
522+
<li>Run the code to see the error</li>
523+
<li>Set a breakpoint inside <code>main</code> to use the debugger</li>
524+
<li>Step through the code using <code>n (next)</code> and another time using <code>s (step)</code></li>
525+
<li>Set a second breakpoint inside <code>main</code>and run again the code but this time use <code>c (continue)</code></li>
526+
<li>Download <a href="https://github.com/fabridamicelli/ds005588/archive/refs/heads/broken-data.zip">this public dataset</a> into the folder <code>/pycourse/data/</code> (create it if you don’t yet have it).</li>
527+
</ol>
528+
<p>This dataset was modified and has some problems apparently. Here’s a bit of code to unzip it and read through the files.</p>
529+
<div id="bbe54d2f-f5a4-44f1-a015-8a4d19e1097a" class="cell" data-execution_count="6">
530+
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb11"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>path <span class="op">=</span> <span class="st">"/home/fdamicel/projects/pycourse/data/ds005588-broken-data.zip"</span></span>
531+
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>target <span class="op">=</span> <span class="st">"/home/fdamicel/projects/pycourse/data"</span></span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
532+
</div>
533+
<div id="e7ad9b30-c041-4edc-83d4-2cfdf984dac7" class="cell" data-execution_count="7">
534+
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb12"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> zipfile</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
535+
</div>
536+
<div id="123149ab-4410-4444-9348-099cbf067f91" class="cell" data-execution_count="8">
537+
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb13"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> zipfile.ZipFile(path) <span class="im">as</span> <span class="bu">file</span>:</span>
538+
<span id="cb13-2"><a href="#cb13-2" aria-hidden="true" tabindex="-1"></a> <span class="bu">file</span>.extractall(target)</span></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
539+
</div>
540+
<div id="4fbd3efe-bd52-4414-99a5-42058984a3c8" class="cell" data-execution_count="15">
541+
<div class="code-copy-outer-scaffold"><div class="sourceCode cell-code" id="cb14"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"></code></pre></div><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></div>
542+
<div class="cell-output cell-output-error">
543+
<div class="ansi-escaped-output">
544+
<pre><span class="ansi-red-fg">---------------------------------------------------------------------------</span>
545+
<span class="ansi-red-fg">UnicodeDecodeError</span> Traceback (most recent call last)
546+
<span class="ansi-cyan-fg">Cell</span><span class="ansi-cyan-fg"> </span><span class="ansi-green-fg">In[15]</span><span class="ansi-green-fg">, line 1</span>
547+
<span class="ansi-green-fg">----&gt; </span><span class="ansi-green-fg">1</span> lines = <span class="ansi-yellow-bg">f</span><span class="ansi-yellow-bg">.</span><span class="ansi-yellow-bg">readlines</span><span class="ansi-yellow-bg">(</span><span class="ansi-yellow-bg">)</span>
548+
549+
<span class="ansi-cyan-fg">File </span><span class="ansi-green-fg">&lt;frozen codecs&gt;:325</span>, in <span class="ansi-cyan-fg">decode</span><span class="ansi-blue-fg">(self, input, final)</span>
550+
551+
<span class="ansi-red-fg">UnicodeDecodeError</span>: 'utf-8' codec can't decode byte 0xb9 in position 10: invalid start byte</pre>
552+
</div>
553+
</div>
554+
</div>
423555

424556

425557
</section>

0 commit comments

Comments
 (0)