Make Coordinate instantiable
authorVivien Maisonneuve <v.maisonneuve@gmail.com>
Fri, 11 Jul 2014 16:07:57 +0000 (18:07 +0200)
committerVivien Maisonneuve <v.maisonneuve@gmail.com>
Fri, 11 Jul 2014 17:49:23 +0000 (19:49 +0200)
pypol/coordinates.py

index f76b30a..a880178 100644 (file)
@@ -2,7 +2,6 @@ import math
 import numbers
 import operator
 
 import numbers
 import operator
 
-from abc import ABC, abstractmethod
 from collections import OrderedDict, Mapping
 
 from .linexprs import Symbol
 from collections import OrderedDict, Mapping
 
 from .linexprs import Symbol
@@ -14,15 +13,25 @@ __all__ = [
 ]
 
 
 ]
 
 
-class Coordinates(ABC):
+class Coordinates:
 
     __slots__ = (
         '_coordinates',
     )
 
 
     __slots__ = (
         '_coordinates',
     )
 
-    @abstractmethod
-    def __new__(cls):
-        super().__new__(cls)
+    def __new__(cls, coordinates):
+        if isinstance(coordinates, Mapping):
+            coordinates = coordinates.items()
+        self = object().__new__(cls)
+        self._coordinates = OrderedDict()
+        for symbol, coordinate in sorted(coordinates,
+                key=lambda item: item[0].sortkey()):
+            if not isinstance(symbol, Symbol):
+                raise TypeError('symbols must be Symbol instances')
+            if not isinstance(coordinate, numbers.Real):
+                raise TypeError('coordinates must be real numbers')
+            self._coordinates[symbol] = coordinate
+        return self
 
     @property
     def symbols(self):
 
     @property
     def symbols(self):
@@ -74,20 +83,6 @@ class Point(Coordinates):
     This class represents points in space.
     """
 
     This class represents points in space.
     """
 
-    def __new__(cls, coordinates):
-        if isinstance(coordinates, Mapping):
-            coordinates = coordinates.items()
-        self = object().__new__(cls)
-        self._coordinates = OrderedDict()
-        for symbol, coordinate in sorted(coordinates,
-                key=lambda item: item[0].sortkey()):
-            if not isinstance(symbol, Symbol):
-                raise TypeError('symbols must be Symbol instances')
-            if not isinstance(coordinate, numbers.Real):
-                raise TypeError('coordinates must be real numbers')
-            self._coordinates[symbol] = coordinate
-        return self
-
     def isorigin(self):
         return not bool(self)
 
     def isorigin(self):
         return not bool(self)
 
@@ -130,15 +125,14 @@ class Vector(Coordinates):
     )
 
     def __new__(cls, initial, terminal=None):
     )
 
     def __new__(cls, initial, terminal=None):
-        self = object().__new__(cls)
         if not isinstance(initial, Point):
             initial = Point(initial)
         if terminal is None:
         if not isinstance(initial, Point):
             initial = Point(initial)
         if terminal is None:
-            self._coordinates = initial._coordinates
+            coordinates = initial._coordinates
         elif not isinstance(terminal, Point):
             terminal = Point(terminal)
         elif not isinstance(terminal, Point):
             terminal = Point(terminal)
-            self._coordinates = terminal._map2(initial, operator.sub)
-        return self
+            coordinates = terminal._map2(initial, operator.sub)
+        return super().__new__(cls, coordinates)
 
     def isnull(self):
         return not bool(self)
 
     def isnull(self):
         return not bool(self)