218 lines
4.7 KiB
Python
218 lines
4.7 KiB
Python
"""
|
|
A multi-dimensional ``Vector`` class, take 4
|
|
|
|
A ``Vector`` is built from an iterable of numbers::
|
|
|
|
>>> Vector([3.1, 4.2])
|
|
Vector([3.1, 4.2])
|
|
>>> Vector((3, 4, 5))
|
|
Vector([3.0, 4.0, 5.0])
|
|
>>> Vector(range(10))
|
|
Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
|
|
|
|
|
|
Tests with 2-dimensions (same results as ``vector2d_v1.py``)::
|
|
|
|
>>> v1 = Vector([3, 4])
|
|
>>> x, y = v1
|
|
>>> x, y
|
|
(3.0, 4.0)
|
|
>>> v1
|
|
Vector([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(Vector([0, 0]))
|
|
(True, False)
|
|
|
|
|
|
Test of ``.frombytes()`` class method:
|
|
|
|
>>> v1_clone = Vector.frombytes(bytes(v1))
|
|
>>> v1_clone
|
|
Vector([3.0, 4.0])
|
|
>>> v1 == v1_clone
|
|
True
|
|
|
|
|
|
Tests with 3-dimensions::
|
|
|
|
>>> v1 = Vector([3, 4, 5])
|
|
>>> x, y, z = v1
|
|
>>> x, y, z
|
|
(3.0, 4.0, 5.0)
|
|
>>> v1
|
|
Vector([3.0, 4.0, 5.0])
|
|
>>> v1_clone = eval(repr(v1))
|
|
>>> v1 == v1_clone
|
|
True
|
|
>>> print(v1)
|
|
(3.0, 4.0, 5.0)
|
|
>>> abs(v1) # doctest:+ELLIPSIS
|
|
7.071067811...
|
|
>>> bool(v1), bool(Vector([0, 0, 0]))
|
|
(True, False)
|
|
|
|
|
|
Tests with many dimensions::
|
|
|
|
>>> v7 = Vector(range(7))
|
|
>>> v7
|
|
Vector([0.0, 1.0, 2.0, 3.0, 4.0, ...])
|
|
>>> abs(v7) # doctest:+ELLIPSIS
|
|
9.53939201...
|
|
|
|
|
|
Test of ``.__bytes__`` and ``.frombytes()`` methods::
|
|
|
|
>>> v1 = Vector([3, 4, 5])
|
|
>>> v1_clone = Vector.frombytes(bytes(v1))
|
|
>>> v1_clone
|
|
Vector([3.0, 4.0, 5.0])
|
|
>>> v1 == v1_clone
|
|
True
|
|
|
|
|
|
Tests of sequence behavior::
|
|
|
|
>>> v1 = Vector([3, 4, 5])
|
|
>>> len(v1)
|
|
3
|
|
>>> v1[0], v1[len(v1)-1], v1[-1]
|
|
(3.0, 5.0, 5.0)
|
|
|
|
|
|
Test of slicing::
|
|
|
|
>>> v7 = Vector(range(7))
|
|
>>> v7[-1]
|
|
6.0
|
|
>>> v7[1:4]
|
|
Vector([1.0, 2.0, 3.0])
|
|
>>> v7[-1:]
|
|
Vector([6.0])
|
|
>>> v7[1,2]
|
|
Traceback (most recent call last):
|
|
...
|
|
TypeError: 'tuple' object cannot be interpreted as an integer
|
|
|
|
|
|
Tests of dynamic attribute access::
|
|
|
|
>>> v7 = Vector(range(10))
|
|
>>> v7.x
|
|
0.0
|
|
>>> v7.y, v7.z, v7.t
|
|
(1.0, 2.0, 3.0)
|
|
|
|
Dynamic attribute lookup failures::
|
|
|
|
>>> v7.k
|
|
Traceback (most recent call last):
|
|
...
|
|
AttributeError: 'Vector' object has no attribute 'k'
|
|
>>> v3 = Vector(range(3))
|
|
>>> v3.t
|
|
Traceback (most recent call last):
|
|
...
|
|
AttributeError: 'Vector' object has no attribute 't'
|
|
>>> v3.spam
|
|
Traceback (most recent call last):
|
|
...
|
|
AttributeError: 'Vector' object has no attribute 'spam'
|
|
|
|
|
|
Tests of hashing::
|
|
|
|
>>> v1 = Vector([3, 4])
|
|
>>> v2 = Vector([3.1, 4.2])
|
|
>>> v3 = Vector([3, 4, 5])
|
|
>>> v6 = Vector(range(6))
|
|
>>> hash(v1), hash(v3), hash(v6)
|
|
(7, 2, 1)
|
|
|
|
|
|
Most hash codes of non-integers vary from a 32-bit to 64-bit CPython build::
|
|
|
|
>>> import sys
|
|
>>> hash(v2) == (384307168202284039 if sys.maxsize > 2**32 else 357915986)
|
|
True
|
|
|
|
"""
|
|
|
|
from array import array
|
|
import reprlib
|
|
import math
|
|
import functools
|
|
import operator
|
|
|
|
|
|
class Vector:
|
|
typecode = 'd'
|
|
|
|
def __init__(self, components):
|
|
self._components = array(self.typecode, components)
|
|
|
|
def __iter__(self):
|
|
return iter(self._components)
|
|
|
|
def __repr__(self):
|
|
components = reprlib.repr(self._components)
|
|
components = components[components.find('['):-1]
|
|
return f'Vector({components})'
|
|
|
|
def __str__(self):
|
|
return str(tuple(self))
|
|
|
|
def __bytes__(self):
|
|
return (bytes([ord(self.typecode)]) +
|
|
bytes(self._components))
|
|
|
|
def __eq__(self, other):
|
|
return (len(self) == len(other) and
|
|
all(a == b for a, b in zip(self, other)))
|
|
|
|
def __hash__(self):
|
|
hashes = (hash(x) for x in self)
|
|
return functools.reduce(operator.xor, hashes, 0)
|
|
|
|
def __abs__(self):
|
|
return math.hypot(*self)
|
|
|
|
def __bool__(self):
|
|
return bool(abs(self))
|
|
|
|
def __len__(self):
|
|
return len(self._components)
|
|
|
|
def __getitem__(self, key):
|
|
if isinstance(key, slice):
|
|
cls = type(self)
|
|
return cls(self._components[key])
|
|
index = operator.index(key)
|
|
return self._components[index]
|
|
|
|
shortcut_names = 'xyzt'
|
|
|
|
def __getattr__(self, name):
|
|
cls = type(self)
|
|
if len(name) == 1:
|
|
pos = cls.shortcut_names.find(name)
|
|
if 0 <= pos < len(self._components):
|
|
return self._components[pos]
|
|
msg = f'{cls.__name__!r} object has no attribute {name!r}'
|
|
raise AttributeError(msg)
|
|
|
|
@classmethod
|
|
def frombytes(cls, octets):
|
|
typecode = chr(octets[0])
|
|
memv = memoryview(octets[1:]).cast(typecode)
|
|
return cls(memv)
|