Derive Point and Vector from Coordinates abstract class
authorVivien Maisonneuve <v.maisonneuve@gmail.com>
Fri, 11 Jul 2014 12:47:15 +0000 (14:47 +0200)
committerVivien Maisonneuve <v.maisonneuve@gmail.com>
Fri, 11 Jul 2014 12:52:19 +0000 (14:52 +0200)
pypol/coordinates.py

index b5c23c9..eb8e78c 100644 (file)
@@ -2,6 +2,7 @@ import math
 import numbers
 import operator
 
 import numbers
 import operator
 
+from abc import ABC, abstractmethod
 from collections import OrderedDict
 
 from .linexprs import Symbol
 from collections import OrderedDict
 
 from .linexprs import Symbol
@@ -13,23 +14,62 @@ __all__ = [
 ]
 
 
 ]
 
 
-def _map(obj, func):
-    for symbol, coordinate in obj.coordinates():
-        yield symbol, func(coordinate)
+class Coordinates(ABC):
 
 
-def _iter2(obj1, obj2):
-    if obj1.symbols != obj2.symbols:
-        raise ValueError('arguments must belong to the same space')
-    coordinates1 = obj1._coordinates.values()
-    coordinates2 = obj2._coordinates.values()
-    yield from zip(obj1.symbols, coordinates1, coordinates2)
+    __slots__ = (
+        '_coordinates',
+    )
+
+    @abstractmethod
+    def __new__(cls):
+        super().__new__(cls)
+
+    @property
+    def symbols(self):
+        return tuple(self._coordinates)
+
+    @property
+    def dimension(self):
+        return len(self.symbols)
+
+    def coordinates(self):
+        yield from self._coordinates.items()
+
+    def coordinate(self, symbol):
+        if not isinstance(symbol, Symbol):
+            raise TypeError('symbol must be a Symbol instance')
+        return self._coordinates[symbol]
+
+    __getitem__ = coordinate
+
+    def __bool__(self):
+        return any(self._coordinates.values())
+
+    def __hash__(self):
+        return hash(tuple(self.coordinates()))
+
+    def __repr__(self):
+        string = ', '.join(['{!r}: {!r}'.format(symbol, coordinate)
+            for symbol, coordinate in self.coordinates()])
+        return '{}({{{}}})'.format(self.__class__.__name__, string)
 
 
-def _map2(obj1, obj2, func):
-    for symbol, coordinate1, coordinate2 in _iter2(obj1, obj2):
-        yield symbol, func(coordinate1, coordinate2)
+    def _map(self, func):
+        for symbol, coordinate in self.coordinates():
+            yield symbol, func(coordinate)
 
 
+    def _iter2(self, other):
+        if self.symbols != other.symbols:
+            raise ValueError('arguments must belong to the same space')
+        coordinates1 = self._coordinates.values()
+        coordinates2 = other._coordinates.values()
+        yield from zip(self.symbols, coordinates1, coordinates2)
+
+    def _map2(self, other, func):
+        for symbol, coordinate1, coordinate2 in self._iter2(other):
+            yield symbol, func(coordinate1, coordinate2)
 
 
-class Point:
+
+class Point(Coordinates):
     """
     This class represents points in space.
     """
     """
     This class represents points in space.
     """
@@ -48,43 +88,22 @@ class Point:
             self._coordinates[symbol] = coordinate
         return self
 
             self._coordinates[symbol] = coordinate
         return self
 
-    @property
-    def symbols(self):
-        return tuple(self._coordinates)
-
-    @property
-    def dimension(self):
-        return len(self.symbols)
-
-    def coordinates(self):
-        yield from self._coordinates.items()
-
-    def coordinate(self, symbol):
-        if not isinstance(symbol, Symbol):
-            raise TypeError('symbol must be a Symbol instance')
-        return self._coordinates[symbol]
-
-    __getitem__ = coordinate
-
     def isorigin(self):
         return not bool(self)
 
     def isorigin(self):
         return not bool(self)
 
-    def __bool__(self):
-        return any(self._coordinates.values())
-
     def __add__(self, other):
         if not isinstance(other, Vector):
             return NotImplemented
     def __add__(self, other):
         if not isinstance(other, Vector):
             return NotImplemented
-        coordinates = _map2(self, other, operator.add)
+        coordinates = self._map2(other, operator.add)
         return Point(coordinates)
 
     def __sub__(self, other):
         coordinates = []
         if isinstance(other, Point):
         return Point(coordinates)
 
     def __sub__(self, other):
         coordinates = []
         if isinstance(other, Point):
-            coordinates = _map2(self, other, operator.sub)
+            coordinates = self._map2(other, operator.sub)
             return Vector(coordinates)
         elif isinstance(other, Vector):
             return Vector(coordinates)
         elif isinstance(other, Vector):
-            coordinates = _map2(self, other, operator.sub)
+            coordinates = self._map2(other, operator.sub)
             return Point(coordinates)
         else:
             return NotImplemented
             return Point(coordinates)
         else:
             return NotImplemented
@@ -93,16 +112,8 @@ class Point:
         return isinstance(other, Point) and \
             self._coordinates == other._coordinates
 
         return isinstance(other, Point) and \
             self._coordinates == other._coordinates
 
-    def __hash__(self):
-        return hash(tuple(self.coordinates()))
-
-    def __repr__(self):
-        string = ', '.join(['{!r}: {!r}'.format(symbol, coordinate)
-            for symbol, coordinate in self.coordinates()])
-        return '{}({{{}}})'.format(self.__class__.__name__, string)
-
 
 
-class Vector:
+class Vector(Coordinates):
     """
     This class represents displacements in space.
     """
     """
     This class represents displacements in space.
     """
@@ -119,7 +130,7 @@ class Vector:
             self._coordinates = initial._coordinates
         elif not isinstance(terminal, Point):
             terminal = Point(terminal)
             self._coordinates = initial._coordinates
         elif not isinstance(terminal, Point):
             terminal = Point(terminal)
-            self._coordinates = _map2(terminal, initial, operator.sub)
+            self._coordinates = terminal._map2(initial, operator.sub)
         return self
 
     @property
         return self
 
     @property
@@ -148,7 +159,7 @@ class Vector:
 
     def __add__(self, other):
         if isinstance(other, (Point, Vector)):
 
     def __add__(self, other):
         if isinstance(other, (Point, Vector)):
-            coordinates = _map2(self, other, operator.add)
+            coordinates = self._map2(other, operator.add)
             return other.__class__(coordinates)
         return NotImplemented
 
             return other.__class__(coordinates)
         return NotImplemented
 
@@ -187,7 +198,7 @@ class Vector:
         """
         if not isinstance(other, numbers.Real):
             return NotImplemented
         """
         if not isinstance(other, numbers.Real):
             return NotImplemented
-        coordinates = _map(self, lambda coordinate: coordinate / other)
+        coordinates = self._map(lambda coordinate: coordinate / other)
         return Vector(coordinates)
 
     def dot(self, other):
         return Vector(coordinates)
 
     def dot(self, other):
@@ -197,7 +208,7 @@ class Vector:
         if not isinstance(other, Vector):
             raise TypeError('argument must be a Vector instance')
         result = 0
         if not isinstance(other, Vector):
             raise TypeError('argument must be a Vector instance')
         result = 0
-        for symbol, coordinate1, coordinate2 in _iter2(self, other):
+        for symbol, coordinate1, coordinate2 in self._iter2(other):
             result += coordinate1 * coordinate2
         return result
 
             result += coordinate1 * coordinate2
         return result
 
@@ -211,13 +222,13 @@ class Vector:
     def __mul__(self, other):
         if not isinstance(other, numbers.Real):
             return NotImplemented
     def __mul__(self, other):
         if not isinstance(other, numbers.Real):
             return NotImplemented
-        coordinates = _map(self, lambda coordinate: other * coordinate)
+        coordinates = self._map(lambda coordinate: other * coordinate)
         return Vector(coordinates)
 
     __rmul__ = __mul__
 
     def __neg__(self):
         return Vector(coordinates)
 
     __rmul__ = __mul__
 
     def __neg__(self):
-        coordinates = _map(self, operator.neg)
+        coordinates = self._map(operator.neg)
         return Vector(coordinates)
 
     def norm(self):
         return Vector(coordinates)
 
     def norm(self):
@@ -234,7 +245,7 @@ class Vector:
 
     def __sub__(self, other):
         if isinstance(other, (Point, Vector)):
 
     def __sub__(self, other):
         if isinstance(other, (Point, Vector)):
-            coordinates = _map2(self, other, operator.sub)
+            coordinates = self._map2(other, operator.sub)
             return other.__class__(coordinates)
         return NotImplemented
 
             return other.__class__(coordinates)
         return NotImplemented