ch01: automated tests
This commit is contained in:
25
01-data-model/README.md
Normal file
25
01-data-model/README.md
Normal 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
|
||||||
@@ -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
|
|
||||||
@@ -140,7 +140,7 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Card(rank='Q', suit='clubs')"
|
"Card(rank='6', suit='diamonds')"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 6,
|
"execution_count": 6,
|
||||||
@@ -149,6 +149,7 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
|
"# NBVAL_IGNORE_OUTPUT\n",
|
||||||
"from random import choice\n",
|
"from random import choice\n",
|
||||||
"\n",
|
"\n",
|
||||||
"choice(deck)"
|
"choice(deck)"
|
||||||
@@ -598,7 +599,7 @@
|
|||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.7.2"
|
"version": "3.8.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
4
01-data-model/test.sh
Executable file
4
01-data-model/test.sh
Executable file
@@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
python3 -m doctest frenchdeck.doctest
|
||||||
|
python3 -m doctest vector2d.py
|
||||||
|
pytest -q --nbval
|
||||||
@@ -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
|
from math import hypot
|
||||||
|
|
||||||
class Vector:
|
class Vector:
|
||||||
|
|||||||
Reference in New Issue
Block a user