This commit is contained in:
Hykilpikonna
2021-12-07 22:28:01 -05:00
commit 6fffdf686a
151 changed files with 37985 additions and 0 deletions
+249
View File
@@ -0,0 +1,249 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>B.1 doctest</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="../tufte.css" />
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<div style="display:none">
\(
\newcommand{\NOT}{\neg}
\newcommand{\AND}{\wedge}
\newcommand{\OR}{\vee}
\newcommand{\XOR}{\oplus}
\newcommand{\IMP}{\Rightarrow}
\newcommand{\IFF}{\Leftrightarrow}
\newcommand{\TRUE}{\text{True}\xspace}
\newcommand{\FALSE}{\text{False}\xspace}
\newcommand{\IN}{\,{\in}\,}
\newcommand{\NOTIN}{\,{\notin}\,}
\newcommand{\TO}{\rightarrow}
\newcommand{\DIV}{\mid}
\newcommand{\NDIV}{\nmid}
\newcommand{\MOD}[1]{\pmod{#1}}
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\C}{\mathbb C}
\newcommand{\cA}{\mathcal A}
\newcommand{\cB}{\mathcal B}
\newcommand{\cC}{\mathcal C}
\newcommand{\cD}{\mathcal D}
\newcommand{\cE}{\mathcal E}
\newcommand{\cF}{\mathcal F}
\newcommand{\cG}{\mathcal G}
\newcommand{\cH}{\mathcal H}
\newcommand{\cI}{\mathcal I}
\newcommand{\cJ}{\mathcal J}
\newcommand{\cL}{\mathcal L}
\newcommand{\cK}{\mathcal K}
\newcommand{\cN}{\mathcal N}
\newcommand{\cO}{\mathcal O}
\newcommand{\cP}{\mathcal P}
\newcommand{\cQ}{\mathcal Q}
\newcommand{\cS}{\mathcal S}
\newcommand{\cT}{\mathcal T}
\newcommand{\cV}{\mathcal V}
\newcommand{\cW}{\mathcal W}
\newcommand{\cZ}{\mathcal Z}
\newcommand{\emp}{\emptyset}
\newcommand{\bs}{\backslash}
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
\newcommand{\abs}[1]{\left | #1 \right |}
\newcommand{\xspace}{}
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
\)
</div>
<header id="title-block-header">
<h1 class="title">B.1 <code>doctest</code></h1>
</header>
<section>
<p><em>Adapted from <a href="https://docs.python.org/3.8/library/doctest.html" class="uri">https://docs.python.org/3.8/library/doctest.html</a>.</em></p>
<p>The <code>doctest</code> module searches for pieces of text that look like interactive Python sessions, and then executes those sessions to verify that they work exactly as shown.</p>
<p>Heres a simple standalone example:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">def</span> is_even(value: <span class="bu">int</span>) <span class="op">-&gt;</span> <span class="bu">bool</span>:</span>
<span id="cb1-2"><a href="#cb1-2"></a> <span class="co">&quot;&quot;&quot; Return whether value is divisible by 2.</span></span>
<span id="cb1-3"><a href="#cb1-3"></a></span>
<span id="cb1-4"><a href="#cb1-4"></a><span class="co"> &gt;&gt;&gt; is_even(2)</span></span>
<span id="cb1-5"><a href="#cb1-5"></a><span class="co"> True</span></span>
<span id="cb1-6"><a href="#cb1-6"></a><span class="co"> &gt;&gt;&gt; is_even(17)</span></span>
<span id="cb1-7"><a href="#cb1-7"></a><span class="co"> False</span></span>
<span id="cb1-8"><a href="#cb1-8"></a><span class="co"> &quot;&quot;&quot;</span></span>
<span id="cb1-9"><a href="#cb1-9"></a> <span class="cf">return</span> value <span class="op">%</span> <span class="dv">2</span> <span class="op">==</span> <span class="dv">0</span></span></code></pre></div>
<p>The simplest way to start using <code>doctest</code> is to end each module with:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1"></a><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</span>
<span id="cb2-2"><a href="#cb2-2"></a> <span class="im">import</span> doctest</span>
<span id="cb2-3"><a href="#cb2-3"></a> doctest.testmod()</span></code></pre></div>
<p><code>doctest</code> then examines docstrings in the module.</p>
<p>Running the module as a script causes the examples in the docstrings to get executed and verified.</p>
<p>This wont display anything unless an example fails, in which case the failing example(s) and the cause(s) of the failure(s) are printed, and the final line of output is <code>***Test Failed*** N failures.</code>, where <code>N</code> is the number of examples that failed.</p>
<p>You can force verbose mode by passing <code>verbose=True</code> to <code>testmod()</code>. In this case, a detailed report of all examples tried is printed to standard output, along with assorted summaries at the end.</p>
<h2 id="how-it-works">How it works</h2>
<p>This section examines in detail how doctest works: which docstrings it looks at, how it finds interactive examples, and how it handles exceptions. This is the information that you need to know to write doctest examples; for information about actually running doctest on these examples, see the following sections.</p>
<h2 id="which-docstrings-are-examined">Which docstrings are examined?</h2>
<p>The module docstring, and all function, class and method docstrings are searched. Objects imported into the module are not searched.</p>
<h2 id="how-are-docstring-examples-recognized">How are docstring examples recognized?</h2>
<p>The module docstring, and all function, class and method docstrings are searched. Objects imported into the module are not searched.</p>
<p>In most cases a copy-and-paste of an interactive console session works fine, but doctest isnt trying to do an exact emulation of any specific Python shell.</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1"></a><span class="op">&gt;&gt;&gt;</span> <span class="co"># comments are ignored</span></span>
<span id="cb3-2"><a href="#cb3-2"></a><span class="op">&gt;&gt;&gt;</span> x <span class="op">=</span> <span class="dv">12</span></span>
<span id="cb3-3"><a href="#cb3-3"></a><span class="op">&gt;&gt;&gt;</span> x</span>
<span id="cb3-4"><a href="#cb3-4"></a><span class="dv">12</span></span>
<span id="cb3-5"><a href="#cb3-5"></a><span class="op">&gt;&gt;&gt;</span> <span class="cf">if</span> x <span class="op">==</span> <span class="dv">13</span>:</span>
<span id="cb3-6"><a href="#cb3-6"></a>... <span class="bu">print</span>(<span class="st">&quot;yes&quot;</span>)</span>
<span id="cb3-7"><a href="#cb3-7"></a>... <span class="cf">else</span>:</span>
<span id="cb3-8"><a href="#cb3-8"></a>... <span class="bu">print</span>(<span class="st">&quot;no&quot;</span>)</span>
<span id="cb3-9"><a href="#cb3-9"></a>... <span class="bu">print</span>(<span class="st">&quot;NO&quot;</span>)</span>
<span id="cb3-10"><a href="#cb3-10"></a>... <span class="bu">print</span>(<span class="st">&quot;NO!!!&quot;</span>)</span>
<span id="cb3-11"><a href="#cb3-11"></a>...</span>
<span id="cb3-12"><a href="#cb3-12"></a>no</span>
<span id="cb3-13"><a href="#cb3-13"></a>NO</span>
<span id="cb3-14"><a href="#cb3-14"></a>NO<span class="op">!!!</span></span>
<span id="cb3-15"><a href="#cb3-15"></a><span class="op">&gt;&gt;&gt;</span></span></code></pre></div>
<p>Any expected output must immediately follow the final <code>'&gt;&gt;&gt; '</code> or <code>'... '</code> line containing the code, and the expected output (if any) extends to the next <code>'&gt;&gt;&gt; '</code> or all-whitespace line.</p>
<p>Notes:</p>
<ul>
<li><p>Expected output cannot contain an all-whitespace line, since such a line is taken to signal the end of expected output. If expected output does contain a blank line, put <code>&lt;BLANKLINE&gt;</code> in your doctest example each place a blank line is expected.</p></li>
<li><p>This is an incorrect example because the prompt characters (i.e., <code>&gt;&gt;&gt;</code>) are missing:</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1"></a><span class="co">&quot;&quot;&quot;</span></span>
<span id="cb4-2"><a href="#cb4-2"></a><span class="co">is_even(2)</span></span>
<span id="cb4-3"><a href="#cb4-3"></a><span class="co">True</span></span>
<span id="cb4-4"><a href="#cb4-4"></a><span class="co">&quot;&quot;&quot;</span></span></code></pre></div></li>
<li><p>This is an incorrect example because there is no space between the <code>&gt;&gt;&gt;</code> and the function call:</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb5-1"><a href="#cb5-1"></a><span class="co">&quot;&quot;&quot;</span></span>
<span id="cb5-2"><a href="#cb5-2"></a><span class="co">&gt;&gt;&gt;is_even(2)</span></span>
<span id="cb5-3"><a href="#cb5-3"></a><span class="co">True</span></span>
<span id="cb5-4"><a href="#cb5-4"></a><span class="co">&quot;&quot;&quot;</span></span></code></pre></div></li>
<li><p>This is an incorrect example because the result of the function call (<code>True</code>) is not included:</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb6-1"><a href="#cb6-1"></a><span class="co">&quot;&quot;&quot;</span></span>
<span id="cb6-2"><a href="#cb6-2"></a><span class="co">&gt;&gt;&gt; is_even(2)</span></span>
<span id="cb6-3"><a href="#cb6-3"></a><span class="co">&quot;&quot;&quot;</span></span></code></pre></div></li>
<li><p>This is an incorrect example because the result of the function call (<code>True</code>) is indented:</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb7-1"><a href="#cb7-1"></a><span class="co">&quot;&quot;&quot;</span></span>
<span id="cb7-2"><a href="#cb7-2"></a><span class="co">&gt;&gt;&gt; is_even(2)</span></span>
<span id="cb7-3"><a href="#cb7-3"></a><span class="co"> True</span></span>
<span id="cb7-4"><a href="#cb7-4"></a><span class="co">&quot;&quot;&quot;</span></span></code></pre></div></li>
</ul>
<h3 id="what-about-exceptions">What about exceptions?</h3>
<p>The expected output for an exception must start with a traceback header, which may be either of the following two lines, indented the same as the first line of the example:</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb8-1"><a href="#cb8-1"></a>Traceback (most recent call last):</span>
<span id="cb8-2"><a href="#cb8-2"></a>Traceback (innermost last):</span></code></pre></div>
<p>The traceback header is followed by an optional traceback stack, whose contents are ignored by doctest. The traceback stack is typically omitted, or copied verbatim from an interactive session.</p>
<p>The traceback stack is followed by the most interesting part: the line(s) containing the exception type and detail. This is usually the last line of a traceback, but can extend across multiple lines if the exception has a multi-line detail:</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb9-1"><a href="#cb9-1"></a><span class="co">&quot;&quot;&quot;</span></span>
<span id="cb9-2"><a href="#cb9-2"></a><span class="co">&gt;&gt;&gt; 1 + &#39;hi&#39;</span></span>
<span id="cb9-3"><a href="#cb9-3"></a><span class="co">Traceback (most recent call last):</span></span>
<span id="cb9-4"><a href="#cb9-4"></a><span class="co"> File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;</span></span>
<span id="cb9-5"><a href="#cb9-5"></a><span class="co">TypeError: unsupported operand type(s) for +: &#39;int&#39; and &#39;str&#39;</span></span>
<span id="cb9-6"><a href="#cb9-6"></a><span class="co">&quot;&quot;&quot;</span></span></code></pre></div>
<p>Best practice is to omit the traceback stack, unless it adds significant documentation value to the example. So the last example is probably better as:</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1"></a><span class="co">&quot;&quot;&quot;</span></span>
<span id="cb10-2"><a href="#cb10-2"></a><span class="co">&gt;&gt;&gt; 1 + &#39;hi&#39;</span></span>
<span id="cb10-3"><a href="#cb10-3"></a><span class="co">Traceback (most recent call last):</span></span>
<span id="cb10-4"><a href="#cb10-4"></a><span class="co">TypeError: unsupported operand type(s) for +: &#39;int&#39; and &#39;str&#39;</span></span>
<span id="cb10-5"><a href="#cb10-5"></a><span class="co">&quot;&quot;&quot;</span></span></code></pre></div>
<h2 id="warnings">Warnings</h2>
<p><code>doctest</code> is serious about requiring exact matches in expected output. If even a single character doesnt match, the test fails. This will probably surprise you a few times, as you learn exactly what Python does and doesnt guarantee about output. For example, when printing a set, Python doesnt guarantee that the element is printed in any particular order, so a test like</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1"></a><span class="op">&gt;&gt;&gt;</span> foo()</span>
<span id="cb11-2"><a href="#cb11-2"></a>{<span class="st">&#39;Hermione&#39;</span>, <span class="st">&#39;Harry&#39;</span>}</span></code></pre></div>
<p>is vulnerable! One workaround is to do</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1"></a><span class="op">&gt;&gt;&gt;</span> foo() <span class="op">==</span> {<span class="st">&#39;Hermione&#39;</span>, <span class="st">&#39;Harry&#39;</span>}</span>
<span id="cb12-2"><a href="#cb12-2"></a><span class="va">True</span></span></code></pre></div>
<p>instead. Another is to do</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1"></a><span class="op">&gt;&gt;&gt;</span> d <span class="op">=</span> <span class="bu">sorted</span>(foo())</span>
<span id="cb13-2"><a href="#cb13-2"></a><span class="op">&gt;&gt;&gt;</span> d</span>
<span id="cb13-3"><a href="#cb13-3"></a>[<span class="st">&#39;Harry&#39;</span>, <span class="st">&#39;Hermione&#39;</span>]</span></code></pre></div>
<h2 id="soapbox">Soapbox</h2>
<p>As mentioned in the introduction, <code>doctest</code> has grown to have three primary uses:</p>
<ol type="1">
<li>Checking examples in docstrings.</li>
<li>Regression testing.</li>
<li>Executable documentation / literate testing.</li>
</ol>
<p>These uses have different requirements, and it is important to distinguish them. In particular, filling your docstrings with obscure test cases makes for bad documentation.</p>
<p>When writing a docstring, choose docstring examples with care. Theres an art to this that needs to be learned—it may not be natural at first. Examples should add genuine value to the documentation. A good example can often be worth many words. If done with care, the examples will be invaluable for your users, and will pay back the time it takes to collect them many times over as the years go by and things change. Were still amazed at how often one of our <code>doctest</code> examples stops working after a “harmless” change.</p>
</section>
<footer>
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
</footer>
</body>
</html>
+203
View File
@@ -0,0 +1,203 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>B.2 pytest</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="../tufte.css" />
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<div style="display:none">
\(
\newcommand{\NOT}{\neg}
\newcommand{\AND}{\wedge}
\newcommand{\OR}{\vee}
\newcommand{\XOR}{\oplus}
\newcommand{\IMP}{\Rightarrow}
\newcommand{\IFF}{\Leftrightarrow}
\newcommand{\TRUE}{\text{True}\xspace}
\newcommand{\FALSE}{\text{False}\xspace}
\newcommand{\IN}{\,{\in}\,}
\newcommand{\NOTIN}{\,{\notin}\,}
\newcommand{\TO}{\rightarrow}
\newcommand{\DIV}{\mid}
\newcommand{\NDIV}{\nmid}
\newcommand{\MOD}[1]{\pmod{#1}}
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\C}{\mathbb C}
\newcommand{\cA}{\mathcal A}
\newcommand{\cB}{\mathcal B}
\newcommand{\cC}{\mathcal C}
\newcommand{\cD}{\mathcal D}
\newcommand{\cE}{\mathcal E}
\newcommand{\cF}{\mathcal F}
\newcommand{\cG}{\mathcal G}
\newcommand{\cH}{\mathcal H}
\newcommand{\cI}{\mathcal I}
\newcommand{\cJ}{\mathcal J}
\newcommand{\cL}{\mathcal L}
\newcommand{\cK}{\mathcal K}
\newcommand{\cN}{\mathcal N}
\newcommand{\cO}{\mathcal O}
\newcommand{\cP}{\mathcal P}
\newcommand{\cQ}{\mathcal Q}
\newcommand{\cS}{\mathcal S}
\newcommand{\cT}{\mathcal T}
\newcommand{\cV}{\mathcal V}
\newcommand{\cW}{\mathcal W}
\newcommand{\cZ}{\mathcal Z}
\newcommand{\emp}{\emptyset}
\newcommand{\bs}{\backslash}
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
\newcommand{\abs}[1]{\left | #1 \right |}
\newcommand{\xspace}{}
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
\)
</div>
<header id="title-block-header">
<h1 class="title">B.2 <code>pytest</code></h1>
</header>
<section>
<p><code>pytest</code> is a Python library used to run tests for your code. In this section, well describe how to write tests that are automatically discovered and run by <code>pytest</code>, how to actually run <code>pytest</code> in your code, and some tips and tricks for making the most of <code>pytest</code>.</p>
<h2 id="how-do-you-write-a-pytest-test">How do you write a <code>pytest</code> test?</h2>
<p>A <strong>test</strong> in <code>pytest</code> is a Python function whose name starts with <code>test_</code>. Inside a test function, we use <code>assert</code> statements to verify expected values or behaviours of a function.</p>
<p>For example:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="co"># This is the function to test</span></span>
<span id="cb1-2"><a href="#cb1-2"></a><span class="kw">def</span> has_more_trues(booleans: <span class="bu">list</span>) <span class="op">-&gt;</span> <span class="bu">bool</span>:</span>
<span id="cb1-3"><a href="#cb1-3"></a> <span class="co">&quot;&quot;&quot;Return whether booleans contains more True values than False values.</span></span>
<span id="cb1-4"><a href="#cb1-4"></a></span>
<span id="cb1-5"><a href="#cb1-5"></a><span class="co"> &gt;&gt;&gt; has_more_trues([True, False, True])</span></span>
<span id="cb1-6"><a href="#cb1-6"></a><span class="co"> True</span></span>
<span id="cb1-7"><a href="#cb1-7"></a><span class="co"> &gt;&gt;&gt; has_more_trues([True, False, False])</span></span>
<span id="cb1-8"><a href="#cb1-8"></a><span class="co"> False</span></span>
<span id="cb1-9"><a href="#cb1-9"></a><span class="co"> &quot;&quot;&quot;</span></span>
<span id="cb1-10"><a href="#cb1-10"></a> <span class="co"># Function body omitted</span></span>
<span id="cb1-11"><a href="#cb1-11"></a></span>
<span id="cb1-12"><a href="#cb1-12"></a></span>
<span id="cb1-13"><a href="#cb1-13"></a><span class="co"># This the test</span></span>
<span id="cb1-14"><a href="#cb1-14"></a><span class="kw">def</span> test_mixture_one_more_true() <span class="op">-&gt;</span> <span class="va">None</span>:</span>
<span id="cb1-15"><a href="#cb1-15"></a> <span class="co">&quot;&quot;&quot;Test has_more_trues on a list with a mixture of True and False,</span></span>
<span id="cb1-16"><a href="#cb1-16"></a><span class="co"> with one more True than False.</span></span>
<span id="cb1-17"><a href="#cb1-17"></a><span class="co"> &quot;&quot;&quot;</span></span>
<span id="cb1-18"><a href="#cb1-18"></a> <span class="cf">assert</span> has_more_trues([<span class="va">True</span>, <span class="va">False</span>, <span class="va">True</span>])</span></code></pre></div>
<p>A single test can have multiple <code>assert</code> statements, although it is generally recommended to separate each <code>assert</code> statement into a separate test. A single Python file can have multiple tests; when <code>pytest</code> is run on a file, it (by default) runs all the tests in that file.</p>
<h2 id="running-pytest">Running <code>pytest</code></h2>
<p>The simplest way of running <code>pytest</code> is to add the following <code>if __name__ == '__main__'</code> block to the bottom of a test file:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb2-1"><a href="#cb2-1"></a><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</span>
<span id="cb2-2"><a href="#cb2-2"></a> <span class="im">import</span> pytest</span>
<span id="cb2-3"><a href="#cb2-3"></a> pytest.main()</span></code></pre></div>
<p>When you run this file, <code>pytest.main</code> will run all test functions in the file. <em>Note</em>: by default, <code>pytest.main</code> actually searches through <em>all</em> Python files in the current directory whose name starts with <code>test_</code> or ends with <code>_test</code>, which can be a bit surprising. So our practice will be to explicitly pass in the name of the current test file to <code>pytest.main</code>, wrapped in a list:</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb3-1"><a href="#cb3-1"></a><span class="co"># If we&#39;re in a file test_my_file.py</span></span>
<span id="cb3-2"><a href="#cb3-2"></a></span>
<span id="cb3-3"><a href="#cb3-3"></a><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</span>
<span id="cb3-4"><a href="#cb3-4"></a> <span class="im">import</span> pytest</span>
<span id="cb3-5"><a href="#cb3-5"></a> pytest.main([<span class="st">&#39;test_my_file.py&#39;</span>])</span></code></pre></div>
<h2 id="testing-for-an-exceptions">Testing for an exceptions</h2>
<p>It is possible to write a <code>pytest</code> test that checks whether a function raises a specific error. To do so, use <code>pytest.raises</code>, which takes an error type as an argument, inside a <code>with</code> statement. Here is an example</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb4-1"><a href="#cb4-1"></a><span class="im">import</span> pytest</span>
<span id="cb4-2"><a href="#cb4-2"></a></span>
<span id="cb4-3"><a href="#cb4-3"></a></span>
<span id="cb4-4"><a href="#cb4-4"></a><span class="kw">def</span> add_one(n):</span>
<span id="cb4-5"><a href="#cb4-5"></a> <span class="cf">return</span> n <span class="op">+</span> <span class="dv">1</span></span>
<span id="cb4-6"><a href="#cb4-6"></a></span>
<span id="cb4-7"><a href="#cb4-7"></a></span>
<span id="cb4-8"><a href="#cb4-8"></a><span class="kw">def</span> test_add_one_type_error():</span>
<span id="cb4-9"><a href="#cb4-9"></a> <span class="co">&quot;&quot;&quot;Test add_one when given a non-numeric argument.&quot;&quot;&quot;</span></span>
<span id="cb4-10"><a href="#cb4-10"></a> <span class="cf">with</span> pytest.raises(<span class="pp">TypeError</span>):</span>
<span id="cb4-11"><a href="#cb4-11"></a> add_one(<span class="st">&#39;hello&#39;</span>)</span></code></pre></div>
<h2 id="options-for-pytest.main">Options for <code>pytest.main</code></h2>
<p><code>pytest.main</code> takes a list of strings as an argument because users can add options (as strings) to modify <code>pytest</code>s default behaviour when running tests. The format for this is <code>pytest.main([&lt;option1&gt;, &lt;option2&gt;, ...])</code>.</p>
<p>Here are some useful options:</p>
<ul>
<li><code>'&lt;filename&gt;'</code>: as we saw above, adding a filename restricts <code>pytest</code> to only running the tests in that Python file.</li>
<li><code>'&lt;filename&gt;::&lt;test_name&gt;'</code>: restrict <code>pytest</code> to run a specific test in the given file (e.g., <code>'test_my_file.py::test_1'</code>)</li>
<li><code>'-x'</code>: stop running tests after the first failure (by default, <code>pytest</code> runs all tests, regardless of the number of failures)</li>
<li><code>'--pdb'</code>: start the Python debugger when a test fails</li>
</ul>
<h2 id="references">References</h2>
<p>For the full documentation for the <code>pytest</code> library, check out <a href="https://docs.pytest.org/en/latest/" class="uri">https://docs.pytest.org/en/latest/</a>.</p>
</section>
<footer>
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
</footer>
</body>
</html>
+160
View File
@@ -0,0 +1,160 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>B.3 python_ta</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="../tufte.css" />
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<div style="display:none">
\(
\newcommand{\NOT}{\neg}
\newcommand{\AND}{\wedge}
\newcommand{\OR}{\vee}
\newcommand{\XOR}{\oplus}
\newcommand{\IMP}{\Rightarrow}
\newcommand{\IFF}{\Leftrightarrow}
\newcommand{\TRUE}{\text{True}\xspace}
\newcommand{\FALSE}{\text{False}\xspace}
\newcommand{\IN}{\,{\in}\,}
\newcommand{\NOTIN}{\,{\notin}\,}
\newcommand{\TO}{\rightarrow}
\newcommand{\DIV}{\mid}
\newcommand{\NDIV}{\nmid}
\newcommand{\MOD}[1]{\pmod{#1}}
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\C}{\mathbb C}
\newcommand{\cA}{\mathcal A}
\newcommand{\cB}{\mathcal B}
\newcommand{\cC}{\mathcal C}
\newcommand{\cD}{\mathcal D}
\newcommand{\cE}{\mathcal E}
\newcommand{\cF}{\mathcal F}
\newcommand{\cG}{\mathcal G}
\newcommand{\cH}{\mathcal H}
\newcommand{\cI}{\mathcal I}
\newcommand{\cJ}{\mathcal J}
\newcommand{\cL}{\mathcal L}
\newcommand{\cK}{\mathcal K}
\newcommand{\cN}{\mathcal N}
\newcommand{\cO}{\mathcal O}
\newcommand{\cP}{\mathcal P}
\newcommand{\cQ}{\mathcal Q}
\newcommand{\cS}{\mathcal S}
\newcommand{\cT}{\mathcal T}
\newcommand{\cV}{\mathcal V}
\newcommand{\cW}{\mathcal W}
\newcommand{\cZ}{\mathcal Z}
\newcommand{\emp}{\emptyset}
\newcommand{\bs}{\backslash}
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
\newcommand{\abs}[1]{\left | #1 \right |}
\newcommand{\xspace}{}
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
\)
</div>
<header id="title-block-header">
<h1 class="title">B.3 <code>python_ta</code></h1>
</header>
<section>
<p>PythonTA is a Python program that analyses Python code to help students find and fix common coding and style errors. Unlike testing libraries like <code>doctest</code> or <code>pytest</code>, PythonTA does not actually run your code. Instead, it analyzes the program text directly, looking for common patterns of code that oftne lead to errors.<label for="sn-0" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-0" class="margin-toggle"/><span class="sidenote">PyCharm does something very similar, which is why youll see red or yellow highlighted text in your Python files as youre working, before running the file.</span></p>
<h2 id="running-pythonta">Running PythonTA</h2>
<p>To run PythonTA on a Python file, put the following code at the bottom of the file you want to check:</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="cf">if</span> <span class="va">__name__</span> <span class="op">==</span> <span class="st">&#39;__main__&#39;</span>:</span>
<span id="cb1-2"><a href="#cb1-2"></a> <span class="im">import</span> python_ta</span>
<span id="cb1-3"><a href="#cb1-3"></a> python_ta.check_all()</span></code></pre></div>
<p>When you run this file, youll see a report open up in your web browser that shows any errors that PythonTA detected. These errors are divided into two broad categories:</p>
<ul>
<li>Code Errors or Forbidden Usage: you should fix these immediately, as they point out logical errors in your code, or a part of your code that is using some feature that is not allowed for your current assignment.</li>
<li>Style or Convention Errors: these identify ways to improve the formatting and design of your Python code. Fixing these is a lower priority, but you should still fix them before your final submission.<label for="sn-1" class="margin-toggle sidenote-number"></label><input type="checkbox" id="sn-1" class="margin-toggle"/><span class="sidenote"> Tip: In PyCharm, you can go to the menu Code -&gt; Reformat File… to automatically fix common style errors in your current file.</span></li>
</ul>
<p>We recommend running PythonTA regularly as youre working on an assignment, as it can be a useful way to check your work and improve the quality of your code. If youre ever stuck, try taking a break and running PythonTA and fixing any errors it finds for you! This is a way to develop good programming habits and style, which will come in handy in this course (and all future courses).</p>
<h3 id="cleaning-up">Cleaning up</h3>
<p>When you run PythonTA, it generates a new report file called <code>pyta_report.html</code> in the same folder as the file youre checking. After youre done running PythonTA, you can safely delete this report file.</p>
<h2 id="checking-contracts-using-pythonta">Checking contracts using PythonTA</h2>
</section>
<footer>
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
</footer>
</body>
</html>
+203
View File
@@ -0,0 +1,203 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>B.4 typing</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="../tufte.css" />
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<div style="display:none">
\(
\newcommand{\NOT}{\neg}
\newcommand{\AND}{\wedge}
\newcommand{\OR}{\vee}
\newcommand{\XOR}{\oplus}
\newcommand{\IMP}{\Rightarrow}
\newcommand{\IFF}{\Leftrightarrow}
\newcommand{\TRUE}{\text{True}\xspace}
\newcommand{\FALSE}{\text{False}\xspace}
\newcommand{\IN}{\,{\in}\,}
\newcommand{\NOTIN}{\,{\notin}\,}
\newcommand{\TO}{\rightarrow}
\newcommand{\DIV}{\mid}
\newcommand{\NDIV}{\nmid}
\newcommand{\MOD}[1]{\pmod{#1}}
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\C}{\mathbb C}
\newcommand{\cA}{\mathcal A}
\newcommand{\cB}{\mathcal B}
\newcommand{\cC}{\mathcal C}
\newcommand{\cD}{\mathcal D}
\newcommand{\cE}{\mathcal E}
\newcommand{\cF}{\mathcal F}
\newcommand{\cG}{\mathcal G}
\newcommand{\cH}{\mathcal H}
\newcommand{\cI}{\mathcal I}
\newcommand{\cJ}{\mathcal J}
\newcommand{\cL}{\mathcal L}
\newcommand{\cK}{\mathcal K}
\newcommand{\cN}{\mathcal N}
\newcommand{\cO}{\mathcal O}
\newcommand{\cP}{\mathcal P}
\newcommand{\cQ}{\mathcal Q}
\newcommand{\cS}{\mathcal S}
\newcommand{\cT}{\mathcal T}
\newcommand{\cV}{\mathcal V}
\newcommand{\cW}{\mathcal W}
\newcommand{\cZ}{\mathcal Z}
\newcommand{\emp}{\emptyset}
\newcommand{\bs}{\backslash}
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
\newcommand{\abs}[1]{\left | #1 \right |}
\newcommand{\xspace}{}
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
\)
</div>
<header id="title-block-header">
<h1 class="title">B.4 <code>typing</code></h1>
</header>
<section>
<p><em>Reference: <a href="https://docs.python.org/3.9/library/typing.html" class="uri">https://docs.python.org/3.9/library/typing.html</a>.</em></p>
<div class="fullwidth">
<table>
<colgroup>
<col style="width: 26%" />
<col style="width: 73%" />
</colgroup>
<thead>
<tr class="header">
<th>Type</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>Any</code></td>
<td>A value that could be of any type. (Used as a placeholder when a variable’s type could be anything, or is unknown.)</td>
</tr>
<tr class="even">
<td><code>Callable[[T1, T2, ...], Tr]</code></td>
<td><p>A function whose parameters have type <code>T1</code>, <code>T2</code>, etc., and whose return type is <code>Tr</code>.</p>
<p>Example: the function</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="kw">def</span> f(x: <span class="bu">int</span>, y: <span class="bu">str</span>) <span class="op">-&gt;</span> <span class="bu">bool</span>:</span>
<span id="cb1-2"><a href="#cb1-2"></a> <span class="co"># Body omitted</span></span></code></pre></div>
<p>has type <code>Callable[[int, str], bool]</code>.</p></td>
</tr>
<tr class="odd">
<td><code>dict[T1, T2]</code></td>
<td><p>A dictionary whose keys have type <code>T1</code> and whose values have type <code>T2</code>.</p>
<p>Example: <code>{'a': 1, 'b': 2}</code> has type <code>dict[str, int]</code>.</p></td>
</tr>
<tr class="even">
<td><code>list[T]</code></td>
<td><p>A list whose elements all have type <code>T</code>.</p>
<p>Example: <code>[1, 2, 3]</code> has type <code>list[int]</code>.</p></td>
</tr>
<tr class="odd">
<td><code>Optional[T]</code></td>
<td>Synonym of <code>Union[T, None]</code>.</td>
</tr>
<tr class="even">
<td><code>set[T]</code></td>
<td><p>A set whose elements all have type <code>T</code>.</p>
<p>Example: <code>{'hi', 'bye'}</code> has type <code>set[str]</code>.</p></td>
</tr>
<tr class="odd">
<td><code>tuple[T1, T2, ...]</code></td>
<td><p>A tuple whose first element has type <code>T1</code>, second element has type <code>T2</code>, etc.</p>
<p>Example: <code>('hello', True, 3.4)</code> has type <code>tuple[str, bool, float]</code>.</p></td>
</tr>
<tr class="even">
<td><code>Union[T1, T2, ...]</code></td>
<td><p>A value whose type is one of <code>T1</code>, <code>T2</code>, etc.</p>
<p>Example: both <code>1</code> and <code>'hello'</code> are instance of type <code>Union[int, str]</code>.</p></td>
</tr>
</tbody>
</table>
</div>
</section>
<footer>
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
</footer>
</body>
</html>
+203
View File
@@ -0,0 +1,203 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>B.5 pdb (Python Debugger)</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="../tufte.css" />
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<div style="display:none">
\(
\newcommand{\NOT}{\neg}
\newcommand{\AND}{\wedge}
\newcommand{\OR}{\vee}
\newcommand{\XOR}{\oplus}
\newcommand{\IMP}{\Rightarrow}
\newcommand{\IFF}{\Leftrightarrow}
\newcommand{\TRUE}{\text{True}\xspace}
\newcommand{\FALSE}{\text{False}\xspace}
\newcommand{\IN}{\,{\in}\,}
\newcommand{\NOTIN}{\,{\notin}\,}
\newcommand{\TO}{\rightarrow}
\newcommand{\DIV}{\mid}
\newcommand{\NDIV}{\nmid}
\newcommand{\MOD}[1]{\pmod{#1}}
\newcommand{\MODS}[1]{\ (\text{mod}\ #1)}
\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\C}{\mathbb C}
\newcommand{\cA}{\mathcal A}
\newcommand{\cB}{\mathcal B}
\newcommand{\cC}{\mathcal C}
\newcommand{\cD}{\mathcal D}
\newcommand{\cE}{\mathcal E}
\newcommand{\cF}{\mathcal F}
\newcommand{\cG}{\mathcal G}
\newcommand{\cH}{\mathcal H}
\newcommand{\cI}{\mathcal I}
\newcommand{\cJ}{\mathcal J}
\newcommand{\cL}{\mathcal L}
\newcommand{\cK}{\mathcal K}
\newcommand{\cN}{\mathcal N}
\newcommand{\cO}{\mathcal O}
\newcommand{\cP}{\mathcal P}
\newcommand{\cQ}{\mathcal Q}
\newcommand{\cS}{\mathcal S}
\newcommand{\cT}{\mathcal T}
\newcommand{\cV}{\mathcal V}
\newcommand{\cW}{\mathcal W}
\newcommand{\cZ}{\mathcal Z}
\newcommand{\emp}{\emptyset}
\newcommand{\bs}{\backslash}
\newcommand{\floor}[1]{\left \lfloor #1 \right \rfloor}
\newcommand{\ceil}[1]{\left \lceil #1 \right \rceil}
\newcommand{\abs}[1]{\left | #1 \right |}
\newcommand{\xspace}{}
\newcommand{\proofheader}[1]{\underline{\textbf{#1}}}
\)
</div>
<header id="title-block-header">
<h1 class="title">B.5 <code>pdb</code> (Python Debugger)</h1>
</header>
<section>
<p><em>Adapted from <a href="https://docs.python.org/3/library/pdb.html" class="uri">https://docs.python.org/3/library/pdb.html</a>.</em></p>
<p>The module <code>pdb</code> defines an interactive source code debugger for Python programs.</p>
<p>The typical usage to break into the debugger from a running program is to insert</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb1-1"><a href="#cb1-1"></a><span class="bu">breakpoint</span>()</span></code></pre></div>
<p>at the location you want to break into the debugger. You can then step through the code following this statement, and continue running without the debugger using the continue command.</p>
<h2 id="debugger-commands">Debugger commands</h2>
<p>The commands recognized by the debugger are listed below. Most commands can be abbreviated to one or two letters as indicated; e.g. <code>h(elp)</code> means that either <code>h</code> or <code>help</code> can be used to enter the help command (but not <code>he</code> or <code>hel</code>, nor <code>H</code> or <code>Help</code> or <code>HELP</code>). Arguments to commands must be separated by whitespace (spaces or tabs).</p>
<p>Entering a blank line repeats the last command entered. Exception: if the last command was a list command, the next 11 lines are listed.</p>
<p>Commands that the debugger doesnt recognize are assumed to be Python statements and are executed in the context of the program being debugged. This is a powerful way to inspect the program being debugged; it is even possible to change a variable or call a function. When an exception occurs in such a statement, the exception name is printed but the debuggers state is not changed.</p>
<div class="fullwidth reference-table">
<table>
<colgroup>
<col style="width: 16%" />
<col style="width: 83%" />
</colgroup>
<thead>
<tr class="header">
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>a(rgs)</td>
<td>Print the argument list of the current function.</td>
</tr>
<tr class="even">
<td>c(ont(inue))</td>
<td>Continue execution, only stop when a breakpoint is encountered.</td>
</tr>
<tr class="odd">
<td>h(elp)</td>
<td>Without argument, print the list of available commands. With a <em>command</em> as an argument, print help about that command.</td>
</tr>
<tr class="even">
<td>l(ist)</td>
<td><p>List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing.</p>
<p>The current line in the current frame is indicated by <code>-&gt;</code>.</p></td>
</tr>
<tr class="odd">
<td>ll</td>
<td>List all source code for the current function or frame. (Short for “long list”.)</td>
</tr>
<tr class="even">
<td>n(ext)</td>
<td>Continue execution until the next line in the current function is reached or it returns. (The difference between <code>next</code> and <code>step</code> is that <code>step</code> stops inside a called function, while <code>next</code> executes called functions at (nearly) full speed, only stopping at the next line in the current function.)</td>
</tr>
<tr class="odd">
<td>r(eturn)</td>
<td>Continue execution until the current function returns.</td>
</tr>
<tr class="even">
<td>s(tep)</td>
<td>Execute the current line, stop at the first possible occasion (either in a function that is called or on the next line in the current function.)</td>
</tr>
</tbody>
</table>
</div>
</section>
<footer>
<a href="https://www.teach.cs.toronto.edu/~csc110y/fall/notes/">CSC110 Course Notes Home</a>
</footer>
</body>
</html>