ch11-24: clean up by @eumiro & sync with Atlas
This commit is contained in:
4
13-protocol-abc/README.rst
Normal file
4
13-protocol-abc/README.rst
Normal file
@@ -0,0 +1,4 @@
|
||||
Sample code for Chapter 11 - "Interfaces, protocols and ABCs"
|
||||
|
||||
From the book "Fluent Python" by Luciano Ramalho (O'Reilly, 2015)
|
||||
http://shop.oreilly.com/product/0636920032519.do
|
||||
28
13-protocol-abc/bingo.py
Normal file
28
13-protocol-abc/bingo.py
Normal file
@@ -0,0 +1,28 @@
|
||||
# tag::TOMBOLA_BINGO[]
|
||||
|
||||
import random
|
||||
|
||||
from tombola import Tombola
|
||||
|
||||
|
||||
class BingoCage(Tombola): # <1>
|
||||
|
||||
def __init__(self, items):
|
||||
self._randomizer = random.SystemRandom() # <2>
|
||||
self._items = []
|
||||
self.load(items) # <3>
|
||||
|
||||
def load(self, items):
|
||||
self._items.extend(items)
|
||||
self._randomizer.shuffle(self._items) # <4>
|
||||
|
||||
def pick(self): # <5>
|
||||
try:
|
||||
return self._items.pop()
|
||||
except IndexError:
|
||||
raise LookupError('pick from empty BingoCage')
|
||||
|
||||
def __call__(self): # <6>
|
||||
self.pick()
|
||||
|
||||
# end::TOMBOLA_BINGO[]
|
||||
2
13-protocol-abc/double/double_object.py
Normal file
2
13-protocol-abc/double/double_object.py
Normal file
@@ -0,0 +1,2 @@
|
||||
def double(x: object) -> object:
|
||||
return x * 2
|
||||
11
13-protocol-abc/double/double_protocol.py
Normal file
11
13-protocol-abc/double/double_protocol.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from typing import TypeVar, Protocol
|
||||
|
||||
T = TypeVar('T') # <1>
|
||||
|
||||
class Repeatable(Protocol):
|
||||
def __mul__(self: T, repeat_count: int) -> T: ... # <2>
|
||||
|
||||
RT = TypeVar('RT', bound=Repeatable) # <3>
|
||||
|
||||
def double(x: RT) -> RT: # <4>
|
||||
return x * 2
|
||||
6
13-protocol-abc/double/double_sequence.py
Normal file
6
13-protocol-abc/double/double_sequence.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from collections import abc
|
||||
from typing import Any
|
||||
|
||||
def double(x: abc.Sequence) -> Any:
|
||||
return x * 2
|
||||
|
||||
56
13-protocol-abc/double/double_test.py
Normal file
56
13-protocol-abc/double/double_test.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from typing import TYPE_CHECKING
|
||||
import pytest
|
||||
from double_protocol import double
|
||||
|
||||
def test_double_int() -> None:
|
||||
given = 2
|
||||
result = double(given)
|
||||
assert result == given * 2
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(given)
|
||||
reveal_type(result)
|
||||
|
||||
|
||||
def test_double_str() -> None:
|
||||
given = 'A'
|
||||
result = double(given)
|
||||
assert result == given * 2
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(given)
|
||||
reveal_type(result)
|
||||
|
||||
|
||||
def test_double_fraction() -> None:
|
||||
from fractions import Fraction
|
||||
given = Fraction(2, 5)
|
||||
result = double(given)
|
||||
assert result == given * 2
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(given)
|
||||
reveal_type(result)
|
||||
|
||||
|
||||
def test_double_array() -> None:
|
||||
from array import array
|
||||
given = array('d', [1.0, 2.0, 3.14])
|
||||
result = double(given)
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(given)
|
||||
reveal_type(result)
|
||||
|
||||
|
||||
def test_double_nparray() -> None:
|
||||
import numpy as np # type: ignore
|
||||
given = np.array([[1, 2], [3, 4]])
|
||||
result = double(given)
|
||||
comparison = result == given * 2
|
||||
assert comparison.all()
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(given)
|
||||
reveal_type(result)
|
||||
|
||||
|
||||
def test_double_none() -> None:
|
||||
given = None
|
||||
with pytest.raises(TypeError):
|
||||
result = double(given)
|
||||
17
13-protocol-abc/drum.py
Normal file
17
13-protocol-abc/drum.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from random import shuffle
|
||||
|
||||
from tombola import Tombola
|
||||
|
||||
|
||||
class TumblingDrum(Tombola):
|
||||
|
||||
def __init__(self, iterable):
|
||||
self._balls = []
|
||||
self.load(iterable)
|
||||
|
||||
def load(self, iterable):
|
||||
self._balls.extend(iterable)
|
||||
shuffle(self._balls)
|
||||
|
||||
def pick(self):
|
||||
return self._balls.pop()
|
||||
26
13-protocol-abc/frenchdeck2.py
Normal file
26
13-protocol-abc/frenchdeck2.py
Normal file
@@ -0,0 +1,26 @@
|
||||
import collections
|
||||
|
||||
Card = collections.namedtuple('Card', ['rank', 'suit'])
|
||||
|
||||
class FrenchDeck2(collections.MutableSequence):
|
||||
ranks = [str(n) for n in range(2, 11)] + list('JQKA')
|
||||
suits = 'spades diamonds clubs hearts'.split()
|
||||
|
||||
def __init__(self):
|
||||
self._cards = [Card(rank, suit) for suit in self.suits
|
||||
for rank in self.ranks]
|
||||
|
||||
def __len__(self):
|
||||
return len(self._cards)
|
||||
|
||||
def __getitem__(self, position):
|
||||
return self._cards[position]
|
||||
|
||||
def __setitem__(self, position, value): # <1>
|
||||
self._cards[position] = value
|
||||
|
||||
def __delitem__(self, position): # <2>
|
||||
del self._cards[position]
|
||||
|
||||
def insert(self, position, value): # <3>
|
||||
self._cards.insert(position, value)
|
||||
30
13-protocol-abc/lotto.py
Normal file
30
13-protocol-abc/lotto.py
Normal file
@@ -0,0 +1,30 @@
|
||||
# tag::LOTTERY_BLOWER[]
|
||||
|
||||
import random
|
||||
|
||||
from tombola import Tombola
|
||||
|
||||
|
||||
class LotteryBlower(Tombola):
|
||||
|
||||
def __init__(self, iterable):
|
||||
self._balls = list(iterable) # <1>
|
||||
|
||||
def load(self, iterable):
|
||||
self._balls.extend(iterable)
|
||||
|
||||
def pick(self):
|
||||
try:
|
||||
position = random.randrange(len(self._balls)) # <2>
|
||||
except ValueError:
|
||||
raise LookupError('pick from empty BingoCage')
|
||||
return self._balls.pop(position) # <3>
|
||||
|
||||
def loaded(self): # <4>
|
||||
return bool(self._balls)
|
||||
|
||||
def inspect(self): # <5>
|
||||
return tuple(sorted(self._balls))
|
||||
|
||||
|
||||
# end::LOTTERY_BLOWER[]
|
||||
35
13-protocol-abc/tombola.py
Normal file
35
13-protocol-abc/tombola.py
Normal file
@@ -0,0 +1,35 @@
|
||||
# tag::TOMBOLA_ABC[]
|
||||
|
||||
import abc
|
||||
|
||||
class Tombola(abc.ABC): # <1>
|
||||
|
||||
@abc.abstractmethod
|
||||
def load(self, iterable): # <2>
|
||||
"""Add items from an iterable."""
|
||||
|
||||
@abc.abstractmethod
|
||||
def pick(self): # <3>
|
||||
"""Remove item at random, returning it.
|
||||
|
||||
This method should raise `LookupError` when the instance is empty.
|
||||
"""
|
||||
|
||||
def loaded(self): # <4>
|
||||
"""Return `True` if there's at least 1 item, `False` otherwise."""
|
||||
return bool(self.inspect()) # <5>
|
||||
|
||||
|
||||
def inspect(self):
|
||||
"""Return a sorted tuple with the items currently inside."""
|
||||
items = []
|
||||
while True: # <6>
|
||||
try:
|
||||
items.append(self.pick())
|
||||
except LookupError:
|
||||
break
|
||||
self.load(items) # <7>
|
||||
return tuple(sorted(items))
|
||||
|
||||
|
||||
# end::TOMBOLA_ABC[]
|
||||
36
13-protocol-abc/tombola_runner.py
Normal file
36
13-protocol-abc/tombola_runner.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# tag::TOMBOLA_RUNNER[]
|
||||
import doctest
|
||||
|
||||
from tombola import Tombola
|
||||
|
||||
# modules to test
|
||||
import bingo, lotto, tombolist, drum # <1>
|
||||
|
||||
TEST_FILE = 'tombola_tests.rst'
|
||||
TEST_MSG = '{0:16} {1.attempted:2} tests, {1.failed:2} failed - {2}'
|
||||
|
||||
|
||||
def main(argv):
|
||||
verbose = '-v' in argv
|
||||
real_subclasses = Tombola.__subclasses__() # <2>
|
||||
virtual_subclasses = list(Tombola._abc_registry) # <3>
|
||||
|
||||
for cls in real_subclasses + virtual_subclasses: # <4>
|
||||
test(cls, verbose)
|
||||
|
||||
|
||||
def test(cls, verbose=False):
|
||||
|
||||
res = doctest.testfile(
|
||||
TEST_FILE,
|
||||
globs={'ConcreteTombola': cls}, # <5>
|
||||
verbose=verbose,
|
||||
optionflags=doctest.REPORT_ONLY_FIRST_FAILURE)
|
||||
tag = 'FAIL' if res.failed else 'OK'
|
||||
print(TEST_MSG.format(cls.__name__, res, tag)) # <6>
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
import sys
|
||||
main(sys.argv)
|
||||
# end::TOMBOLA_RUNNER[]
|
||||
64
13-protocol-abc/tombola_subhook.py
Normal file
64
13-protocol-abc/tombola_subhook.py
Normal file
@@ -0,0 +1,64 @@
|
||||
"""
|
||||
Variation of ``tombola.Tombola`` implementing ``__subclasshook__``.
|
||||
|
||||
Tests with simple classes::
|
||||
|
||||
>>> Tombola.__subclasshook__(object)
|
||||
NotImplemented
|
||||
>>> class Complete:
|
||||
... def __init__(): pass
|
||||
... def load(): pass
|
||||
... def pick(): pass
|
||||
... def loaded(): pass
|
||||
...
|
||||
>>> Tombola.__subclasshook__(Complete)
|
||||
True
|
||||
>>> issubclass(Complete, Tombola)
|
||||
|
||||
"""
|
||||
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
from inspect import getmembers, isfunction
|
||||
|
||||
|
||||
class Tombola(ABC): # <1>
|
||||
|
||||
@abstractmethod
|
||||
def __init__(self, iterable): # <2>
|
||||
"""New instance is loaded from an iterable."""
|
||||
|
||||
@abstractmethod
|
||||
def load(self, iterable):
|
||||
"""Add items from an iterable."""
|
||||
|
||||
@abstractmethod
|
||||
def pick(self): # <3>
|
||||
"""Remove item at random, returning it.
|
||||
|
||||
This method should raise `LookupError` when the instance is empty.
|
||||
"""
|
||||
|
||||
def loaded(self): # <4>
|
||||
try:
|
||||
item = self.pick()
|
||||
except LookupError:
|
||||
return False
|
||||
else:
|
||||
self.load([item]) # put it back
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def __subclasshook__(cls, other_cls):
|
||||
if cls is Tombola:
|
||||
interface_names = function_names(cls)
|
||||
found_names = set()
|
||||
for a_cls in other_cls.__mro__:
|
||||
found_names |= function_names(a_cls)
|
||||
if found_names >= interface_names:
|
||||
return True
|
||||
return NotImplemented
|
||||
|
||||
|
||||
def function_names(obj):
|
||||
return {name for name, _ in getmembers(obj, isfunction)}
|
||||
82
13-protocol-abc/tombola_tests.rst
Normal file
82
13-protocol-abc/tombola_tests.rst
Normal file
@@ -0,0 +1,82 @@
|
||||
==============
|
||||
Tombola tests
|
||||
==============
|
||||
|
||||
Every concrete subclass of Tombola should pass these tests.
|
||||
|
||||
|
||||
Create and load instance from iterable::
|
||||
|
||||
>>> balls = list(range(3))
|
||||
>>> globe = ConcreteTombola(balls)
|
||||
>>> globe.loaded()
|
||||
True
|
||||
>>> globe.inspect()
|
||||
(0, 1, 2)
|
||||
|
||||
|
||||
Pick and collect balls::
|
||||
|
||||
>>> picks = []
|
||||
>>> picks.append(globe.pick())
|
||||
>>> picks.append(globe.pick())
|
||||
>>> picks.append(globe.pick())
|
||||
|
||||
|
||||
Check state and results::
|
||||
|
||||
>>> globe.loaded()
|
||||
False
|
||||
>>> sorted(picks) == balls
|
||||
True
|
||||
|
||||
|
||||
Reload::
|
||||
|
||||
>>> globe.load(balls)
|
||||
>>> globe.loaded()
|
||||
True
|
||||
>>> picks = [globe.pick() for i in balls]
|
||||
>>> globe.loaded()
|
||||
False
|
||||
|
||||
|
||||
Check that `LookupError` (or a subclass) is the exception
|
||||
thrown when the device is empty::
|
||||
|
||||
>>> globe = ConcreteTombola([])
|
||||
>>> try:
|
||||
... globe.pick()
|
||||
... except LookupError as exc:
|
||||
... print('OK')
|
||||
OK
|
||||
|
||||
|
||||
Load and pick 100 balls to verify that they all come out::
|
||||
|
||||
>>> balls = list(range(100))
|
||||
>>> globe = ConcreteTombola(balls)
|
||||
>>> picks = []
|
||||
>>> while globe.inspect():
|
||||
... picks.append(globe.pick())
|
||||
>>> len(picks) == len(balls)
|
||||
True
|
||||
>>> set(picks) == set(balls)
|
||||
True
|
||||
|
||||
|
||||
Check that the order has changed and is not simply reversed::
|
||||
|
||||
>>> picks != balls
|
||||
True
|
||||
>>> picks[::-1] != balls
|
||||
True
|
||||
|
||||
Note: the previous 2 tests have a *very* small chance of failing
|
||||
even if the implementation is OK. The probability of the 100
|
||||
balls coming out, by chance, in the order they were inspect is
|
||||
1/100!, or approximately 1.07e-158. It's much easier to win the
|
||||
Lotto or to become a billionaire working as a programmer.
|
||||
|
||||
THE END
|
||||
|
||||
23
13-protocol-abc/tombolist.py
Normal file
23
13-protocol-abc/tombolist.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from random import randrange
|
||||
|
||||
from tombola import Tombola
|
||||
|
||||
@Tombola.register # <1>
|
||||
class TomboList(list): # <2>
|
||||
|
||||
def pick(self):
|
||||
if self: # <3>
|
||||
position = randrange(len(self))
|
||||
return self.pop(position) # <4>
|
||||
else:
|
||||
raise LookupError('pop from empty TomboList')
|
||||
|
||||
load = list.extend # <5>
|
||||
|
||||
def loaded(self):
|
||||
return bool(self) # <6>
|
||||
|
||||
def inspect(self):
|
||||
return tuple(sorted(self))
|
||||
|
||||
# Tombola.register(TomboList) # <7>
|
||||
5
13-protocol-abc/typing/randompick.py
Normal file
5
13-protocol-abc/typing/randompick.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from typing import Protocol, runtime_checkable, Any
|
||||
|
||||
@runtime_checkable
|
||||
class RandomPicker(Protocol):
|
||||
def pick(self) -> Any: ...
|
||||
25
13-protocol-abc/typing/randompick_test.py
Normal file
25
13-protocol-abc/typing/randompick_test.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import random
|
||||
from typing import Any, Iterable, TYPE_CHECKING
|
||||
|
||||
from randompick import RandomPicker # <1>
|
||||
|
||||
class SimplePicker(): # <2>
|
||||
def __init__(self, items: Iterable) -> None:
|
||||
self._items = list(items)
|
||||
random.shuffle(self._items)
|
||||
|
||||
def pick(self) -> Any: # <3>
|
||||
return self._items.pop()
|
||||
|
||||
def test_isinstance() -> None: # <4>
|
||||
popper = SimplePicker([1])
|
||||
assert isinstance(popper, RandomPicker)
|
||||
|
||||
def test_item_type() -> None: # <5>
|
||||
items = [1, 2]
|
||||
popper = SimplePicker(items)
|
||||
item = popper.pick()
|
||||
assert item in items
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(item) # <6>
|
||||
assert isinstance(item, int)
|
||||
6
13-protocol-abc/typing/randompickload.py
Normal file
6
13-protocol-abc/typing/randompickload.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from typing import Protocol, runtime_checkable, Any, Iterable
|
||||
from randompick import RandomPicker
|
||||
|
||||
@runtime_checkable # <1>
|
||||
class LoadableRandomPicker(RandomPicker, Protocol): # <2>
|
||||
def load(self, Iterable) -> None: ... # <3>
|
||||
32
13-protocol-abc/typing/randompickload_test.py
Normal file
32
13-protocol-abc/typing/randompickload_test.py
Normal file
@@ -0,0 +1,32 @@
|
||||
import random
|
||||
from typing import Any, Iterable, TYPE_CHECKING
|
||||
|
||||
from randompickload import LoadableRandomPicker
|
||||
|
||||
class SimplePicker():
|
||||
def __init__(self, items: Iterable) -> None:
|
||||
self._items = list(items)
|
||||
random.shuffle(self._items)
|
||||
|
||||
def pick(self) -> Any:
|
||||
return self._items.pop()
|
||||
|
||||
class LoadablePicker(): # <1>
|
||||
def __init__(self, items: Iterable) -> None:
|
||||
self.load(items)
|
||||
|
||||
def pick(self) -> Any: # <2>
|
||||
return self._items.pop()
|
||||
|
||||
def load(self, items: Iterable) -> Any: # <3>
|
||||
self._items = list(items)
|
||||
random.shuffle(self._items)
|
||||
|
||||
def test_isinstance() -> None: # <4>
|
||||
popper = LoadablePicker([1])
|
||||
assert isinstance(popper, LoadableRandomPicker)
|
||||
|
||||
def test_isinstance_not() -> None: # <5>
|
||||
popper = SimplePicker([1])
|
||||
assert not isinstance(popper, LoadableRandomPicker)
|
||||
|
||||
172
13-protocol-abc/typing/vector2d_v4.py
Normal file
172
13-protocol-abc/typing/vector2d_v4.py
Normal file
@@ -0,0 +1,172 @@
|
||||
"""
|
||||
A two-dimensional vector class
|
||||
|
||||
>>> v1 = Vector2d(3, 4)
|
||||
>>> print(v1.x, v1.y)
|
||||
3.0 4.0
|
||||
>>> x, y = v1
|
||||
>>> x, y
|
||||
(3.0, 4.0)
|
||||
>>> v1
|
||||
Vector2d(3.0, 4.0)
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0)
|
||||
>>> octets = bytes(v1)
|
||||
>>> octets
|
||||
b'd\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
|
||||
>>> abs(v1)
|
||||
5.0
|
||||
>>> bool(v1), bool(Vector2d(0, 0))
|
||||
(True, False)
|
||||
|
||||
|
||||
Test of ``.frombytes()`` class method:
|
||||
|
||||
>>> v1_clone = Vector2d.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
Vector2d(3.0, 4.0)
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
|
||||
Tests of ``format()`` with Cartesian coordinates:
|
||||
|
||||
>>> format(v1)
|
||||
'(3.0, 4.0)'
|
||||
>>> format(v1, '.2f')
|
||||
'(3.00, 4.00)'
|
||||
>>> format(v1, '.3e')
|
||||
'(3.000e+00, 4.000e+00)'
|
||||
|
||||
|
||||
Tests of the ``angle`` method::
|
||||
|
||||
>>> Vector2d(0, 0).angle()
|
||||
0.0
|
||||
>>> Vector2d(1, 0).angle()
|
||||
0.0
|
||||
>>> epsilon = 10**-8
|
||||
>>> abs(Vector2d(0, 1).angle() - math.pi/2) < epsilon
|
||||
True
|
||||
>>> abs(Vector2d(1, 1).angle() - math.pi/4) < epsilon
|
||||
True
|
||||
|
||||
|
||||
Tests of ``format()`` with polar coordinates:
|
||||
|
||||
>>> format(Vector2d(1, 1), 'p') # doctest:+ELLIPSIS
|
||||
'<1.414213..., 0.785398...>'
|
||||
>>> format(Vector2d(1, 1), '.3ep')
|
||||
'<1.414e+00, 7.854e-01>'
|
||||
>>> format(Vector2d(1, 1), '0.5fp')
|
||||
'<1.41421, 0.78540>'
|
||||
|
||||
|
||||
Tests of ``x`` and ``y`` read-only properties:
|
||||
|
||||
>>> v1.x, v1.y
|
||||
(3.0, 4.0)
|
||||
>>> v1.x = 123
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: can't set attribute
|
||||
|
||||
|
||||
Tests of hashing:
|
||||
|
||||
>>> v1 = Vector2d(3, 4)
|
||||
>>> v2 = Vector2d(3.1, 4.2)
|
||||
>>> hash(v1), hash(v2)
|
||||
(7, 384307168202284039)
|
||||
>>> len(set([v1, v2]))
|
||||
2
|
||||
|
||||
Converting to/from a ``complex``:
|
||||
# tag::VECTOR2D_V4_DEMO[]
|
||||
>>> from typing import SupportsComplex
|
||||
>>> v3 = Vector2d(1.5, 2.5)
|
||||
>>> isinstance(v3, SupportsComplex) # <1>
|
||||
True
|
||||
>>> complex(v3) # <2>
|
||||
(1.5+2.5j)
|
||||
>>> Vector2d.fromcomplex(4+5j) # <3>
|
||||
Vector2d(4.0, 5.0)
|
||||
|
||||
# end::VECTOR2D_V4_DEMO[]
|
||||
"""
|
||||
|
||||
from array import array
|
||||
import math
|
||||
|
||||
class Vector2d:
|
||||
typecode = 'd'
|
||||
|
||||
def __init__(self, x, y):
|
||||
self.__x = float(x)
|
||||
self.__y = float(y)
|
||||
|
||||
@property
|
||||
def x(self):
|
||||
return self.__x
|
||||
|
||||
@property
|
||||
def y(self):
|
||||
return self.__y
|
||||
|
||||
def __iter__(self):
|
||||
return (i for i in (self.x, self.y))
|
||||
|
||||
def __repr__(self):
|
||||
class_name = type(self).__name__
|
||||
return '{}({!r}, {!r})'.format(class_name, *self)
|
||||
|
||||
def __str__(self):
|
||||
return str(tuple(self))
|
||||
|
||||
def __bytes__(self):
|
||||
return (bytes([ord(self.typecode)]) +
|
||||
bytes(array(self.typecode, self)))
|
||||
|
||||
def __eq__(self, other):
|
||||
return tuple(self) == tuple(other)
|
||||
|
||||
def __hash__(self):
|
||||
return hash(self.x) ^ hash(self.y)
|
||||
|
||||
def __abs__(self):
|
||||
return math.hypot(self.x, self.y)
|
||||
|
||||
def __bool__(self):
|
||||
return bool(abs(self))
|
||||
|
||||
def angle(self):
|
||||
return math.atan2(self.y, self.x)
|
||||
|
||||
def __format__(self, fmt_spec=''):
|
||||
if fmt_spec.endswith('p'):
|
||||
fmt_spec = fmt_spec[:-1]
|
||||
coords = (abs(self), self.angle())
|
||||
outer_fmt = '<{}, {}>'
|
||||
else:
|
||||
coords = self
|
||||
outer_fmt = '({}, {})'
|
||||
components = (format(c, fmt_spec) for c in coords)
|
||||
return outer_fmt.format(*components)
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets):
|
||||
typecode = chr(octets[0])
|
||||
memv = memoryview(octets[1:]).cast(typecode)
|
||||
return cls(*memv)
|
||||
|
||||
# tag::VECTOR2D_V4_COMPLEX[]
|
||||
def __complex__(self):
|
||||
return complex(self.x, self.y)
|
||||
|
||||
@classmethod
|
||||
def fromcomplex(cls, datum):
|
||||
return Vector2d(datum.real, datum.imag) # <1>
|
||||
# end::VECTOR2D_V4_COMPLEX[]
|
||||
56
13-protocol-abc/typing/vector2d_v4_test.py
Normal file
56
13-protocol-abc/typing/vector2d_v4_test.py
Normal file
@@ -0,0 +1,56 @@
|
||||
from typing import SupportsComplex, SupportsAbs, Tuple
|
||||
from typing import TYPE_CHECKING
|
||||
import math
|
||||
import pytest
|
||||
|
||||
from vector2d_v4 import Vector2d
|
||||
|
||||
def test_SupportsComplex_subclass() -> None:
|
||||
assert issubclass(Vector2d, SupportsComplex)
|
||||
|
||||
def test_SupportsComplex_isinstance() -> None:
|
||||
v = Vector2d(3, 4)
|
||||
assert isinstance(v, SupportsComplex)
|
||||
c = complex(v)
|
||||
assert c == 3 + 4j
|
||||
|
||||
def test_SupportsAbs_subclass() -> None:
|
||||
assert issubclass(Vector2d, SupportsAbs)
|
||||
|
||||
def test_SupportsAbs_isinstance() -> None:
|
||||
v = Vector2d(3, 4)
|
||||
assert isinstance(v, SupportsAbs)
|
||||
r = abs(v)
|
||||
assert r == 5.0
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(r) # Revealed type is 'Any'
|
||||
|
||||
def magnitude(v: SupportsAbs) -> float:
|
||||
return abs(v)
|
||||
|
||||
def test_SupportsAbs_Vector2d_argument() -> None:
|
||||
assert magnitude(Vector2d(3, 4)) == 5.0
|
||||
|
||||
def test_SupportsAbs_object_argument() -> None:
|
||||
with pytest.raises(TypeError):
|
||||
magnitude(object())
|
||||
# mypy error:
|
||||
# Argument 1 to "magnitude" has incompatible type "object"; expected "SupportsAbs[Any]"
|
||||
|
||||
def polar(datum: SupportsComplex) -> Tuple[float, float]:
|
||||
c = complex(datum)
|
||||
return abs(c), math.atan2(c.imag, c.real)
|
||||
|
||||
def test_SupportsComplex_Vector2d_argument() -> None:
|
||||
assert polar(Vector2d(2, 0)) == (2, 0)
|
||||
expected = (2, math.pi / 2)
|
||||
result = polar(Vector2d(0, 2))
|
||||
assert math.isclose(result[0], expected[0])
|
||||
assert math.isclose(result[1], expected[1])
|
||||
|
||||
def test_SupportsComplex_complex_argument() -> None:
|
||||
assert polar(complex(2, 0)) == (2, 0)
|
||||
expected = (2, math.pi / 2)
|
||||
result = polar(complex(0, 2))
|
||||
assert math.isclose(result[0], expected[0])
|
||||
assert math.isclose(result[1], expected[1])
|
||||
174
13-protocol-abc/typing/vector2d_v5.py
Normal file
174
13-protocol-abc/typing/vector2d_v5.py
Normal file
@@ -0,0 +1,174 @@
|
||||
from __future__ import annotations
|
||||
|
||||
"""
|
||||
A two-dimensional vector class
|
||||
|
||||
>>> v1 = Vector2d(3, 4)
|
||||
>>> print(v1.x, v1.y)
|
||||
3.0 4.0
|
||||
>>> x, y = v1
|
||||
>>> x, y
|
||||
(3.0, 4.0)
|
||||
>>> v1
|
||||
Vector2d(3.0, 4.0)
|
||||
>>> v1_clone = eval(repr(v1))
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
>>> print(v1)
|
||||
(3.0, 4.0)
|
||||
>>> octets = bytes(v1)
|
||||
>>> octets
|
||||
b'd\\x00\\x00\\x00\\x00\\x00\\x00\\x08@\\x00\\x00\\x00\\x00\\x00\\x00\\x10@'
|
||||
>>> abs(v1)
|
||||
5.0
|
||||
>>> bool(v1), bool(Vector2d(0, 0))
|
||||
(True, False)
|
||||
|
||||
|
||||
Test of ``.frombytes()`` class method:
|
||||
|
||||
>>> v1_clone = Vector2d.frombytes(bytes(v1))
|
||||
>>> v1_clone
|
||||
Vector2d(3.0, 4.0)
|
||||
>>> v1 == v1_clone
|
||||
True
|
||||
|
||||
|
||||
Tests of ``format()`` with Cartesian coordinates:
|
||||
|
||||
>>> format(v1)
|
||||
'(3.0, 4.0)'
|
||||
>>> format(v1, '.2f')
|
||||
'(3.00, 4.00)'
|
||||
>>> format(v1, '.3e')
|
||||
'(3.000e+00, 4.000e+00)'
|
||||
|
||||
|
||||
Tests of the ``angle`` method::
|
||||
|
||||
>>> Vector2d(0, 0).angle()
|
||||
0.0
|
||||
>>> Vector2d(1, 0).angle()
|
||||
0.0
|
||||
>>> epsilon = 10**-8
|
||||
>>> abs(Vector2d(0, 1).angle() - math.pi/2) < epsilon
|
||||
True
|
||||
>>> abs(Vector2d(1, 1).angle() - math.pi/4) < epsilon
|
||||
True
|
||||
|
||||
|
||||
Tests of ``format()`` with polar coordinates:
|
||||
|
||||
>>> format(Vector2d(1, 1), 'p') # doctest:+ELLIPSIS
|
||||
'<1.414213..., 0.785398...>'
|
||||
>>> format(Vector2d(1, 1), '.3ep')
|
||||
'<1.414e+00, 7.854e-01>'
|
||||
>>> format(Vector2d(1, 1), '0.5fp')
|
||||
'<1.41421, 0.78540>'
|
||||
|
||||
|
||||
Tests of ``x`` and ``y`` read-only properties:
|
||||
|
||||
>>> v1.x, v1.y
|
||||
(3.0, 4.0)
|
||||
>>> v1.x = 123
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
AttributeError: can't set attribute
|
||||
|
||||
|
||||
Tests of hashing:
|
||||
|
||||
>>> v1 = Vector2d(3, 4)
|
||||
>>> v2 = Vector2d(3.1, 4.2)
|
||||
>>> hash(v1), hash(v2)
|
||||
(7, 384307168202284039)
|
||||
>>> len(set([v1, v2]))
|
||||
2
|
||||
|
||||
Converting to/from a ``complex``:
|
||||
|
||||
>>> from typing import SupportsComplex
|
||||
>>> v3 = Vector2d(1.5, 2.5)
|
||||
>>> isinstance(v3, SupportsComplex) # <1>
|
||||
True
|
||||
>>> complex(v3) # <2>
|
||||
(1.5+2.5j)
|
||||
>>> Vector2d.fromcomplex(4+5j) # <3>
|
||||
Vector2d(4.0, 5.0)
|
||||
"""
|
||||
|
||||
from array import array
|
||||
import math
|
||||
from typing import SupportsComplex, Iterator
|
||||
|
||||
class Vector2d:
|
||||
typecode = 'd'
|
||||
|
||||
def __init__(self, x, y) -> None:
|
||||
self.__x = float(x)
|
||||
self.__y = float(y)
|
||||
|
||||
@property
|
||||
def x(self) -> float:
|
||||
return self.__x
|
||||
|
||||
@property
|
||||
def y(self) -> float:
|
||||
return self.__y
|
||||
|
||||
def __iter__(self) -> Iterator[float]:
|
||||
return (i for i in (self.x, self.y))
|
||||
|
||||
def __repr__(self) -> str:
|
||||
class_name = type(self).__name__
|
||||
return '{}({!r}, {!r})'.format(class_name, *self)
|
||||
|
||||
def __str__(self) -> str:
|
||||
return str(tuple(self))
|
||||
|
||||
def __bytes__(self) -> bytes:
|
||||
return (bytes([ord(self.typecode)]) +
|
||||
bytes(array(self.typecode, self)))
|
||||
|
||||
def __eq__(self, other) -> bool:
|
||||
return tuple(self) == tuple(other)
|
||||
|
||||
def __hash__(self) -> int:
|
||||
return hash(self.x) ^ hash(self.y)
|
||||
|
||||
def __bool__(self) -> bool:
|
||||
return bool(abs(self))
|
||||
|
||||
def angle(self) -> float:
|
||||
return math.atan2(self.y, self.x)
|
||||
|
||||
def __format__(self, fmt_spec='') -> str:
|
||||
if fmt_spec.endswith('p'):
|
||||
fmt_spec = fmt_spec[:-1]
|
||||
coords = (abs(self), self.angle())
|
||||
outer_fmt = '<{}, {}>'
|
||||
else:
|
||||
coords = self
|
||||
outer_fmt = '({}, {})'
|
||||
components = (format(c, fmt_spec) for c in coords)
|
||||
return outer_fmt.format(*components)
|
||||
|
||||
@classmethod
|
||||
def frombytes(cls, octets) -> Vector2d:
|
||||
typecode = chr(octets[0])
|
||||
memv = memoryview(octets[1:]).cast(typecode)
|
||||
return cls(*memv)
|
||||
|
||||
# tag::VECTOR2D_V5_COMPLEX[]
|
||||
def __abs__(self) -> float: # <1>
|
||||
return math.hypot(self.x, self.y)
|
||||
|
||||
def __complex__(self) -> complex: # <2>
|
||||
return complex(self.x, self.y)
|
||||
|
||||
@classmethod
|
||||
def fromcomplex(cls, datum: SupportsComplex) -> Vector2d: # <3>
|
||||
c = complex(datum) # <4>
|
||||
return Vector2d(c.real, c.imag)
|
||||
# end::VECTOR2D_V5_COMPLEX[]
|
||||
35
13-protocol-abc/typing/vector2d_v5_test.py
Normal file
35
13-protocol-abc/typing/vector2d_v5_test.py
Normal file
@@ -0,0 +1,35 @@
|
||||
from vector2d_v5 import Vector2d
|
||||
from typing import SupportsComplex, SupportsAbs, TYPE_CHECKING
|
||||
|
||||
import pytest
|
||||
|
||||
|
||||
def test_SupportsComplex_subclass() -> None:
|
||||
assert issubclass(Vector2d, SupportsComplex)
|
||||
|
||||
def test_SupportsComplex_isinstance() -> None:
|
||||
v = Vector2d(3, 4)
|
||||
assert isinstance(v, SupportsComplex)
|
||||
c = complex(v)
|
||||
assert c == 3 + 4j
|
||||
|
||||
def test_SupportsAbs_subclass() -> None:
|
||||
assert issubclass(Vector2d, SupportsAbs)
|
||||
|
||||
def test_SupportsAbs_isinstance() -> None:
|
||||
v = Vector2d(3, 4)
|
||||
assert isinstance(v, SupportsAbs)
|
||||
r = abs(v)
|
||||
assert r == 5.0
|
||||
if TYPE_CHECKING:
|
||||
reveal_type(r) # Revealed type is 'builtins.float*'
|
||||
|
||||
def magnitude(v: SupportsAbs) -> float:
|
||||
return abs(v)
|
||||
|
||||
def test_SupportsAbs_Vector2d_argument() -> None:
|
||||
assert 5.0 == magnitude(Vector2d(3, 4))
|
||||
|
||||
def test_SupportsAbs_object_argument() -> None:
|
||||
with pytest.raises(TypeError):
|
||||
assert 5.0 == magnitude(object())
|
||||
Reference in New Issue
Block a user