|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +from array import array |
| 4 | +import reprlib |
| 5 | +import math |
| 6 | +import numbers |
| 7 | +import functools |
| 8 | +import operator |
| 9 | +import itertools |
| 10 | + |
| 11 | + |
| 12 | +class Vector: |
| 13 | + typecode = 'd' |
| 14 | + |
| 15 | + def __init__(self, components): |
| 16 | + self._components = array(self.typecode, components) |
| 17 | + |
| 18 | + def __iter__(self): |
| 19 | + return iter(self._components) |
| 20 | + |
| 21 | + def __repr__(self): |
| 22 | + components = reprlib.repr(self._components) |
| 23 | + components = components[components.find('['):-1] |
| 24 | + return f'Vector({components})' |
| 25 | + |
| 26 | + def __str__(self): |
| 27 | + return str(tuple(self)) |
| 28 | + |
| 29 | + def __bytes__(self): |
| 30 | + return (bytes([ord(self.typecode)]) + bytes(self._components)) |
| 31 | + |
| 32 | + def __eq__(self, other): |
| 33 | + return (len(self) == len(other)) and all(a == b for a, b in zip(self, other)) |
| 34 | + |
| 35 | + def __hash__(self): |
| 36 | + hashes = map(hash, self._components) |
| 37 | + return functools.reduce(operator.xor, hashes, 0) |
| 38 | + |
| 39 | + def __abs__(self): |
| 40 | + return math.sqrt(sum(x * x for x in self)) |
| 41 | + |
| 42 | + def __bool__(self): |
| 43 | + return bool(abs(self)) |
| 44 | + |
| 45 | + def __len__(self): |
| 46 | + return len(self._components) |
| 47 | + |
| 48 | + def __getitem__(self, index): |
| 49 | + cls = type(self) |
| 50 | + if isinstance(index, slice): |
| 51 | + return cls(self._components[index]) |
| 52 | + elif isinstance(index, numbers.Integral): |
| 53 | + return self._components[index] |
| 54 | + else: |
| 55 | + msg = f'{cls.__name__} indices must be integers' |
| 56 | + raise TypeError(msg) |
| 57 | + |
| 58 | + shortcut_names = 'xyzt' |
| 59 | + |
| 60 | + def __getattr__(self, name): |
| 61 | + cls = type(self) |
| 62 | + if len(name) == 1: |
| 63 | + pos = cls.shortcut_names.find(name) |
| 64 | + if 0 <= pos < len(self._components): |
| 65 | + return self._components[pos] |
| 66 | + msg = f'{cls.__name__} object has no attribute {name}' |
| 67 | + raise AttributeError(msg) |
| 68 | + |
| 69 | + def __setattr__(self, name, value): |
| 70 | + cls = type(self) |
| 71 | + if len(name) == 1: |
| 72 | + if name in cls.shortcut_names: |
| 73 | + error = f'readonly attribute {name}' |
| 74 | + elif name.islower(): |
| 75 | + error = f"can't set attributes 'a' to 'z' in {cls.__name__}" |
| 76 | + else: |
| 77 | + error = '' |
| 78 | + if error: |
| 79 | + raise AttributeError(error) |
| 80 | + super().__setattr__(name, value) |
| 81 | + |
| 82 | + def angle(self, n): |
| 83 | + r = math.sqrt(sum(x * x for x in self[n:])) |
| 84 | + a = math.atan2(r, self[n - 1]) |
| 85 | + if (n == len(self) - 1) and (self[-1] < 0): |
| 86 | + return math.pi * 2 - a |
| 87 | + else: |
| 88 | + return a |
| 89 | + |
| 90 | + def angles(self): |
| 91 | + return (self.angle(n) for n in range(1, len(self))) |
| 92 | + |
| 93 | + def __format__(self, fmt_spec=''): |
| 94 | + if fmt_spec.endswith('h'): |
| 95 | + fmt_spec = fmt_spec[:-1] |
| 96 | + coords = itertools.chain([abs(self)], self.angles()) |
| 97 | + out_fmt = '<{}>' |
| 98 | + else: |
| 99 | + coords = self |
| 100 | + out_fmt = '({})' |
| 101 | + components = (format(c, fmt_spec) for c in coords) |
| 102 | + return out_fmt.format(', '.join(components)) |
| 103 | + |
| 104 | + @classmethod |
| 105 | + def frombytes(cls, octets): |
| 106 | + typecode = chr(octets[0]) |
| 107 | + memv = memoryview(octets[1:]).cast(typecode) |
| 108 | + return cls(memv) |
0 commit comments