Skip to content

Commit 98940d7

Browse files
committed
builds html
1 parent e6c0e11 commit 98940d7

21 files changed

+351
-608
lines changed

02-assertions.html

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<h2 class="subtitle">Assertions</h2>
3131
<section class="objectives panel panel-warning">
3232
<div class="panel-heading">
33-
<h2><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
33+
<h2 id="learning-objectives"><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
3434
</div>
3535
<div class="panel-body">
3636
<ul>
@@ -41,29 +41,29 @@ <h2><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2
4141
</div>
4242
</section>
4343
<p>Assertions are the simplest type of test. They are used as a tool for bounding acceptable behavior during runtime. The assert keyword in python has the following behavior:</p>
44-
<pre class="sourceCode python"><code class="sourceCode python">&gt;&gt;&gt; <span class="kw">assert</span> <span class="ot">True</span> == <span class="ot">False</span></code></pre>
44+
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="op">&gt;&gt;&gt;</span> <span class="cf">assert</span> <span class="va">True</span> <span class="op">==</span> <span class="va">False</span></code></pre></div>
4545
<pre class="output"><code>Traceback (most recent call last):
4646
File &quot;&lt;stdin&gt;&quot;, line 1, in &lt;module&gt;
4747
AssertionError</code></pre>
48-
<pre class="sourceCode python"><code class="sourceCode python"> &gt;&gt;&gt; <span class="kw">assert</span> <span class="ot">True</span> == <span class="ot">True</span></code></pre>
48+
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"> <span class="op">&gt;&gt;&gt;</span> <span class="cf">assert</span> <span class="va">True</span> <span class="op">==</span> <span class="va">True</span></code></pre></div>
4949
<pre class="output"><code> &gt;&gt;&gt;</code></pre>
5050
<p>That is, assertions halt code execution instantly if the comparison is false. It does nothing at all if the comparison is true. These are therefore a very good tool for guarding the function against foolish (e.g. human) input:</p>
51-
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list):
52-
<span class="kw">assert</span> <span class="dt">len</span>(num_list) != <span class="dv">0</span>
53-
<span class="kw">return</span> <span class="dt">sum</span>(num_list)/<span class="dt">len</span>(num_list)</code></pre>
51+
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list):
52+
<span class="cf">assert</span> <span class="bu">len</span>(num_list) <span class="op">!=</span> <span class="dv">0</span>
53+
<span class="cf">return</span> <span class="bu">sum</span>(num_list)<span class="op">/</span><span class="bu">len</span>(num_list)</code></pre></div>
5454
<p>The advantage of assertions is their ease of use. They are rarely more than one line of code. The disadvantage is that assertions halt execution indiscriminately and the helpfulness of the resulting error message is usually quite limited.</p>
5555
<p>Also, input checking may require decending a rabbit hole of exceptional cases. What happens when the input provided to the mean function is a string, rather than a list of numbers?</p>
5656
<section class="challenge panel panel-success">
5757
<div class="panel-heading">
58-
<h2><span class="glyphicon glyphicon-pencil"></span>Insert an Assertion</h2>
58+
<h2 id="insert-an-assertion"><span class="glyphicon glyphicon-pencil"></span>Insert an Assertion</h2>
5959
</div>
6060
<div class="panel-body">
6161
<ol style="list-style-type: decimal">
6262
<li>Open an IPython Notebook</li>
6363
<li>Create the following function:</li>
6464
</ol>
65-
<pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list):
66-
<span class="kw">return</span> <span class="dt">sum</span>(num_list)/<span class="dt">len</span>(num_list)</code></pre>
65+
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> mean(num_list):
66+
<span class="cf">return</span> <span class="bu">sum</span>(num_list)<span class="op">/</span><span class="bu">len</span>(num_list)</code></pre></div>
6767
<ol start="3" style="list-style-type: decimal">
6868
<li>In the function, insert an assertion that checks whether the input is actually a list.</li>
6969
</ol>
@@ -73,30 +73,25 @@ <h2><span class="glyphicon glyphicon-pencil"></span>Insert an Assertion</h2>
7373
<p>Assertions are also helpful for catching abnormal behaviors, such as those that arise with floating point arithmetic.</p>
7474
<section class="challenge panel panel-success">
7575
<div class="panel-heading">
76-
<h2><span class="glyphicon glyphicon-pencil"></span>Almost Equal</h2>
76+
<h2 id="almost-equal"><span class="glyphicon glyphicon-pencil"></span>Almost Equal</h2>
7777
</div>
7878
<div class="panel-body">
7979
<p>Assertions are also helpful for catching abnormal behaviors, such as those that arise with floating point arithmetic. Using the assert keyword, how could you test whether some value is almost the same as another value?</p>
8080
<ul>
8181
<li>My package, mynum, provides the number a.</li>
8282
<li>Use the <code>assert</code> keyword to check whether the number a is greater than 2.</li>
83-
<li>Use the <code>assert</code> keyword to check whether a is equal to 2 to within 2 decimal places.</li>
8483
<li>Use the <code>assert</code> keyword to check that a is equal to 2 within an error of 0.003.</li>
8584
</ul>
86-
<pre class="sourceCode python"><code class="sourceCode python"><span class="ch">from</span> mynum <span class="ch">import</span> a
85+
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">from</span> mynum <span class="im">import</span> a
8786
<span class="co"># greater than 2 assertion here</span>
88-
<span class="co"># 2 decimal places assertion here</span>
89-
<span class="co"># 0.003 assertion here</span></code></pre>
87+
<span class="co"># 0.003 assertion here</span></code></pre></div>
9088
</div>
9189
</section>
92-
<p>To help with situations such as those above, there are classes of more helpful assertions that we will use often in later parts of this testing lesson as the building blocks of our tests. The nose testing package contains many of them.</p>
93-
<h2 id="nose">Nose</h2>
94-
<p>The nose testing framework has built-in assertion types implementing <code>assert_almost_equal</code>, <code>assert_true</code>, <code>assert_false</code>, <code>assert_raises</code>, <code>assert_is_instance</code>, and others.</p>
95-
<pre class="sourceCode python"><code class="sourceCode python"><span class="ch">from</span> nose.tools <span class="ch">import</span> assert_almost_equal
96-
<span class="ch">from</span> mynum <span class="ch">import</span> a
97-
assert_almost_equal(a, <span class="dv">2</span>, places=<span class="dv">2</span>)
98-
assert_almost_equal(a, <span class="dv">2</span>, delta=<span class="fl">0.003</span>)</code></pre>
99-
<p>These assertions give much more helpful error messages and have much more powerful features than the simple assert keyword. An even more powerful sibling of the assertion is the <em>exception</em>. We’ll learn about those in the next lesson.</p>
90+
<h2 id="numpy">NumPy</h2>
91+
<p>The NumPy numerical computing library has a built-in function <code>assert_allclose</code> for comparing numbers within a tolerance:</p>
92+
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">from</span> numpy.testing <span class="im">import</span> assert_allclose
93+
<span class="im">from</span> mynum <span class="im">import</span> a
94+
assert_allclose(a, <span class="dv">2</span>, atol<span class="op">=</span><span class="fl">0.003</span>, rtol<span class="op">=</span><span class="dv">0</span>)</code></pre></div>
10095
</div>
10196
</div>
10297
</article>

02-assertions.ipynb

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,10 @@
312312
"cell_type": "markdown",
313313
"metadata": {},
314314
"source": [
315-
"To help with situations such as those above, there are classes of more helpful assertions that we will use often in later parts of this testing lesson as the building blocks of our tests. The nose testing package contains many of them.\n",
315+
"### NumPy\n",
316316
"\n",
317-
"___\n",
318-
"\n",
319-
"### Nose\n",
320-
"\n",
321-
"The nose testing framework has built-in assertion types implementing `assert_almost_equal`, `assert_true`, `assert_false`, `assert_raises`, `assert_is_instance`, and others.\n"
317+
"The NumPy numerical computing library has a built-in function `assert_allclose`\n",
318+
"for comparing numbers within a tolerance:"
322319
]
323320
},
324321
{
@@ -329,24 +326,21 @@
329326
},
330327
"outputs": [],
331328
"source": [
332-
"from nose.tools import assert_almost_equal\n",
329+
"from numpy.testing import assert_allclose\n",
333330
"from mynum import a\n",
334-
"assert_almost_equal(a, 2, places=2)\n",
335-
"assert_almost_equal(a, 2, delta=0.003)"
331+
"assert_allclose(a, 2, atol=0.003, rtol=0)"
336332
]
337333
},
338334
{
339335
"cell_type": "markdown",
340336
"metadata": {},
341337
"source": [
342-
"These assertions give much more helpful error messages and have much more powerful features than the simple assert keyword. An even more powerful sibling of the assertion is the exception. We’ll learn about those in the next lesson.\n",
343-
"\n",
344338
"## Key Points\n",
345339
"- Assertions are one line tests embedded in code.\n",
346340
"- The assert keyword is used to set an assertion.\n",
347341
"- Assertions halt execution if the argument is false.\n",
348342
"- Assertions do nothing if the argument is true.\n",
349-
"- The nose.tools package provides more informative assertions.\n",
343+
"- The `numpy.testing` module provides tools for numerical testing.\n",
350344
"- Assertions are the building blocks of tests."
351345
]
352346
},
@@ -376,7 +370,7 @@
376370
"name": "python",
377371
"nbconvert_exporter": "python",
378372
"pygments_lexer": "ipython3",
379-
"version": "3.3.5"
373+
"version": "3.5.1"
380374
}
381375
},
382376
"nbformat": 4,

02-assertions.md

Lines changed: 20 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ subtitle: Assertions
55
minutes: 10
66
---
77
> ## Learning Objectives {.objectives}
8-
>
8+
>
99
> * Assertions are one line tests embedded in code.
1010
> * Assertions can halt execution if something unexpected happens.
1111
> * Assertions are the building blocks of tests.
1212
13-
Assertions are the simplest type of test. They are used as a tool for bounding
14-
acceptable behavior during runtime. The assert keyword in python has the
13+
Assertions are the simplest type of test. They are used as a tool for bounding
14+
acceptable behavior during runtime. The assert keyword in python has the
1515
following behavior:
1616

1717
~~~ {.python}
@@ -29,8 +29,8 @@ Traceback (most recent call last):
2929
>>>
3030
~~~
3131

32-
That is, assertions halt code execution instantly if the comparison is false.
33-
It does nothing at all if the comparison is true. These are therefore a very
32+
That is, assertions halt code execution instantly if the comparison is false.
33+
It does nothing at all if the comparison is true. These are therefore a very
3434
good tool for guarding the function against foolish (e.g. human) input:
3535

3636
~~~ {.python}
@@ -42,10 +42,10 @@ def mean(num_list):
4242
The advantage of assertions is their ease of use. They are rarely more than one
4343
line of code. The disadvantage is that assertions halt execution
4444
indiscriminately and the helpfulness of the resulting error message is usually
45-
quite limited.
45+
quite limited.
4646

47-
Also, input checking may require decending a rabbit hole of exceptional cases.
48-
What happens when the input provided to the mean function is a string, rather
47+
Also, input checking may require decending a rabbit hole of exceptional cases.
48+
What happens when the input provided to the mean function is a string, rather
4949
than a list of numbers?
5050

5151
> ## Insert an Assertion {.challenge}
@@ -58,14 +58,14 @@ than a list of numbers?
5858
> return sum(num_list)/len(num_list)
5959
> ~~~
6060
>
61-
> 3. In the function, insert an assertion that checks whether the input is
61+
> 3. In the function, insert an assertion that checks whether the input is
6262
> actually a list.
63-
>
64-
> Hint: Use the [isinstance function](https://docs.python.org/2/library/functions.html#isinstance).
65-
>
63+
>
64+
> Hint: Use the [isinstance function](https://docs.python.org/2/library/functions.html#isinstance).
65+
>
6666
6767
68-
Assertions are also helpful for catching abnormal behaviors, such as those that
68+
Assertions are also helpful for catching abnormal behaviors, such as those that
6969
arise with floating point arithmetic.
7070
7171
> ## Almost Equal {.challenge}
@@ -74,36 +74,23 @@ arise with floating point arithmetic.
7474
> that arise with floating point arithmetic. Using the assert keyword, how could
7575
> you test whether some value is almost the same as another value?
7676
>
77-
> - My package, mynum, provides the number a.
77+
> - My package, mynum, provides the number a.
7878
> - Use the `assert` keyword to check whether the number a is greater than 2.
79-
> - Use the `assert` keyword to check whether a is equal to 2 to within 2 decimal places.
8079
> - Use the `assert` keyword to check that a is equal to 2 within an error of 0.003.
81-
>
80+
>
8281
> ~~~ {.python}
8382
> from mynum import a
8483
> # greater than 2 assertion here
85-
> # 2 decimal places assertion here
8684
> # 0.003 assertion here
8785
> ~~~
8886
89-
To help with situations such as those above, there are classes of more helpful
90-
assertions that we will use often in later parts of this testing lesson as the
91-
building blocks of our tests. The nose testing package contains many of them.
92-
93-
## Nose
87+
## NumPy
9488
95-
The nose testing framework has built-in assertion types implementing
96-
`assert_almost_equal`, `assert_true`, `assert_false`, `assert_raises`,
97-
`assert_is_instance`, and others.
89+
The NumPy numerical computing library has a built-in function `assert_allclose`
90+
for comparing numbers within a tolerance:
9891
9992
~~~ {.python}
100-
from nose.tools import assert_almost_equal
93+
from numpy.testing import assert_allclose
10194
from mynum import a
102-
assert_almost_equal(a, 2, places=2)
103-
assert_almost_equal(a, 2, delta=0.003)
95+
assert_allclose(a, 2, atol=0.003, rtol=0)
10496
~~~
105-
106-
These assertions give much more helpful error messages and have much more
107-
powerful features than the simple assert keyword. An even more powerful sibling
108-
of the assertion is the _exception_. We'll learn about those in the next
109-
lesson.

0 commit comments

Comments
 (0)