Skip to content

Commit 64e8bdc

Browse files
committed
ch01: automated tests
1 parent d5be725 commit 64e8bdc

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed

01-data-model/README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# The Python Data Model
2+
3+
Sample code for Chapter 1 of _Fluent Python 2e_ by Luciano Ramalho (O'Reilly, 2020)
4+
5+
## Running the tests
6+
7+
### Doctests
8+
9+
Use Python's standard ``doctest`` module to check stand-alone doctest file:
10+
11+
$ python3 -m doctest frenchdeck.doctest -v
12+
13+
And to check doctests embedded in a module:
14+
15+
$ python3 -m doctest vector2d.py -v
16+
17+
### Jupyter Notebook
18+
19+
Install ``pytest`` and the ``nbval`` plugin:
20+
21+
$ pip install pytest nbval
22+
23+
Run:
24+
25+
$ pytest --nbval

01-data-model/README.rst

-4
This file was deleted.

01-data-model/data-model.ipynb

+3-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
{
141141
"data": {
142142
"text/plain": [
143-
"Card(rank='Q', suit='clubs')"
143+
"Card(rank='6', suit='diamonds')"
144144
]
145145
},
146146
"execution_count": 6,
@@ -149,6 +149,7 @@
149149
}
150150
],
151151
"source": [
152+
"# NBVAL_IGNORE_OUTPUT\n",
152153
"from random import choice\n",
153154
"\n",
154155
"choice(deck)"
@@ -598,7 +599,7 @@
598599
"name": "python",
599600
"nbconvert_exporter": "python",
600601
"pygments_lexer": "ipython3",
601-
"version": "3.7.2"
602+
"version": "3.8.0"
602603
}
603604
},
604605
"nbformat": 4,

01-data-model/test.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/bash
2+
python3 -m doctest frenchdeck.doctest
3+
python3 -m doctest vector2d.py
4+
pytest -q --nbval

01-data-model/vector2d.py

+32
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
1+
"""
2+
vector2d.py: a simplistic class demonstrating some special methods
3+
4+
It is simplistic for didactic reasons. It lacks proper error handling,
5+
especially in the ``__add__`` and ``__mul__`` methods.
6+
7+
This example is greatly expanded later in the book.
8+
9+
Addition::
10+
11+
>>> v1 = Vector(2, 4)
12+
>>> v2 = Vector(2, 1)
13+
>>> v1 + v2
14+
Vector(4, 5)
15+
16+
Absolute value::
17+
18+
>>> v = Vector(3, 4)
19+
>>> abs(v)
20+
5.0
21+
22+
Scalar multiplication::
23+
24+
>>> v * 3
25+
Vector(9, 12)
26+
>>> abs(v * 3)
27+
15.0
28+
29+
30+
"""
31+
32+
133
from math import hypot
234

335
class Vector:

0 commit comments

Comments
 (0)