ch01: automated tests

This commit is contained in:
Luciano Ramalho 2019-11-20 19:46:31 -03:00
parent d5be725318
commit 64e8bdcd79
5 changed files with 64 additions and 6 deletions

25
01-data-model/README.md Normal file
View File

@ -0,0 +1,25 @@
# The Python Data Model
Sample code for Chapter 1 of _Fluent Python 2e_ by Luciano Ramalho (O'Reilly, 2020)
## Running the tests
### Doctests
Use Python's standard ``doctest`` module to check stand-alone doctest file:
$ python3 -m doctest frenchdeck.doctest -v
And to check doctests embedded in a module:
$ python3 -m doctest vector2d.py -v
### Jupyter Notebook
Install ``pytest`` and the ``nbval`` plugin:
$ pip install pytest nbval
Run:
$ pytest --nbval

View File

@ -1,4 +0,0 @@
Sample code for Chapter 1 - "The Python Data Model"
From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015)
http://shop.oreilly.com/product/0636920032519.do

View File

@ -140,7 +140,7 @@
{
"data": {
"text/plain": [
"Card(rank='Q', suit='clubs')"
"Card(rank='6', suit='diamonds')"
]
},
"execution_count": 6,
@ -149,6 +149,7 @@
}
],
"source": [
"# NBVAL_IGNORE_OUTPUT\n",
"from random import choice\n",
"\n",
"choice(deck)"
@ -598,7 +599,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.8.0"
}
},
"nbformat": 4,

4
01-data-model/test.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
python3 -m doctest frenchdeck.doctest
python3 -m doctest vector2d.py
pytest -q --nbval

View File

@ -1,3 +1,35 @@
"""
vector2d.py: a simplistic class demonstrating some special methods
It is simplistic for didactic reasons. It lacks proper error handling,
especially in the ``__add__`` and ``__mul__`` methods.
This example is greatly expanded later in the book.
Addition::
>>> v1 = Vector(2, 4)
>>> v2 = Vector(2, 1)
>>> v1 + v2
Vector(4, 5)
Absolute value::
>>> v = Vector(3, 4)
>>> abs(v)
5.0
Scalar multiplication::
>>> v * 3
Vector(9, 12)
>>> abs(v * 3)
15.0
"""
from math import hypot
class Vector: