Various improvements to utility types:
- Add operations for Vector. - Move some tests into test_utility_types.py. - Add tests for PositionAndLook and Vector.
This commit is contained in:
1 parent
61598eba75
commit
4b6feda1cb
4 files changed
+116
-44
No files matched your search
@@ -259,6 +259,7 @@ class UUID(Type):
|
||||
|
||||
|
||||
class Position(Type, Vector):
|
||||
"""3D position vectors with a specific, compact network representation."""
|
||||
__slots__ = ()
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
"""Minecraft data types that are used by packets, but don't have a specific
|
||||
network representation.
|
||||
"""
|
||||
from __future__ import division
|
||||
from collections import namedtuple
|
||||
|
||||
|
||||
@@ -9,11 +10,43 @@ __all__ = (
|
||||
)
|
||||
|
||||
|
||||
"""An immutable type usually used to represent 3D spatial coordinates.
|
||||
NOTE: subclasses of 'Vector' should have '__slots__ = ()' to avoid the
|
||||
creation of a '__dict__' attribute, which would waste space.
|
||||
"""
|
||||
Vector = namedtuple('Vector', ('x', 'y', 'z'))
|
||||
class Vector(namedtuple('BaseVector', ('x', 'y', 'z'))):
|
||||
"""An immutable type usually used to represent 3D spatial coordinates,
|
||||
supporting elementwise vector addition, subtraction, and negation; and
|
||||
scalar multiplication and (right) division.
|
||||
|
||||
NOTE: subclasses of 'Vector' should have '__slots__ = ()' to avoid the
|
||||
creation of a '__dict__' attribute, which would waste space.
|
||||
"""
|
||||
__slots__ = ()
|
||||
|
||||
def __add__(self, other):
|
||||
return NotImplemented if not isinstance(other, Vector) else \
|
||||
type(self)(self.x + other.x, self.y + other.y, self.z + other.z)
|
||||
|
||||
def __sub__(self, other):
|
||||
return NotImplemented if not isinstance(other, Vector) else \
|
||||
type(self)(self.x - other.x, self.y - other.y, self.z - other.z)
|
||||
|
||||
def __neg__(self):
|
||||
return type(self)(-self.x, -self.y, -self.z)
|
||||
|
||||
def __mul__(self, other):
|
||||
return type(self)(self.x*other, self.y*other, self.z*other)
|
||||
|
||||
def __rmul__(self, other):
|
||||
return type(self)(other*self.x, other*self.y, other*self.z)
|
||||
|
||||
def __truediv__(self, other):
|
||||
return type(self)(self.x/other, self.y/other, self.z/other)
|
||||
|
||||
def __floordiv__(self, other):
|
||||
return type(self)(self.x//other, self.y//other, self.z//other)
|
||||
|
||||
__div__ = __floordiv__
|
||||
|
||||
def __repr__(self):
|
||||
return '%s(%r, %r, %r)' % (type(self).__name__, self.x, self.y, self.z)
|
||||
|
||||
|
||||
class MutableRecord(object):
|
||||
@@ -34,8 +67,12 @@ class MutableRecord(object):
|
||||
return type(self) is type(other) and \
|
||||
all(getattr(self, a) == getattr(other, a) for a in self.__slots__)
|
||||
|
||||
def __neq__(self, other):
|
||||
return not (self == other)
|
||||
|
||||
def __hash__(self):
|
||||
return hash(getattr(self, a) for a in self.__slots__)
|
||||
values = tuple(getattr(self, a, None) for a in self.__slots__)
|
||||
return hash((type(self), values))
|
||||
|
||||
|
||||
class PositionAndLook(MutableRecord):
|
||||
|
||||
Reference in new issue
Block a user