File tree Expand file tree Collapse file tree 5 files changed +64
-6
lines changed
Expand file tree Collapse file tree 5 files changed +64
-6
lines changed Original file line number Diff line number Diff line change 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
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 140140 {
141141 "data" : {
142142 "text/plain" : [
143- " Card(rank='Q ', suit='clubs ')"
143+ " Card(rank='6 ', suit='diamonds ')"
144144 ]
145145 },
146146 "execution_count" : 6 ,
149149 }
150150 ],
151151 "source" : [
152+ " # NBVAL_IGNORE_OUTPUT\n " ,
152153 " from random import choice\n " ,
153154 " \n " ,
154155 " choice(deck)"
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 ,
Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+ python3 -m doctest frenchdeck.doctest
3+ python3 -m doctest vector2d.py
4+ pytest -q --nbval
Original file line number Diff line number Diff line change 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+
133from math import hypot
234
335class Vector :
You can’t perform that action at this time.
0 commit comments