Add labels to documentation sections
[linpy.git] / linpy / geometry.py
index 5b5ed89..cc19a72 100644 (file)
@@ -85,14 +85,15 @@ class Coordinates:
         if isinstance(coordinates, Mapping):
             coordinates = coordinates.items()
         self = object().__new__(cls)
         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()):
+        self._coordinates = []
+        for symbol, coordinate in coordinates:
             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')
             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
+            self._coordinates.append((symbol, coordinate))
+        self._coordinates.sort(key=lambda item: item[0].sortkey())
+        self._coordinates = OrderedDict(self._coordinates)
         return self
 
     @property
         return self
 
     @property
@@ -185,10 +186,10 @@ class Point(Coordinates, GeometricObject):
         """
         Translate the point by a Vector object and return the resulting point.
         """
         """
         Translate the point by a Vector object and return the resulting point.
         """
-        if not isinstance(other, Vector):
-            return NotImplemented
-        coordinates = self._map2(other, operator.add)
-        return Point(coordinates)
+        if isinstance(other, Vector):
+            coordinates = self._map2(other, operator.add)
+            return Point(coordinates)
+        return NotImplemented
 
     def __sub__(self, other):
         """
 
     def __sub__(self, other):
         """
@@ -203,15 +204,15 @@ class Point(Coordinates, GeometricObject):
         elif isinstance(other, Vector):
             coordinates = self._map2(other, operator.sub)
             return Point(coordinates)
         elif isinstance(other, Vector):
             coordinates = self._map2(other, operator.sub)
             return Point(coordinates)
-        else:
-            return NotImplemented
+        return NotImplemented
 
     def __eq__(self, other):
         """
         Test whether two points are equal.
         """
 
     def __eq__(self, other):
         """
         Test whether two points are equal.
         """
-        return isinstance(other, Point) and \
-            self._coordinates == other._coordinates
+        if isinstance(other, Point):
+            return self._coordinates == other._coordinates
+        return NotImplemented
 
     def aspolyhedron(self):
         from .polyhedra import Polyhedron
 
     def aspolyhedron(self):
         from .polyhedra import Polyhedron
@@ -231,7 +232,7 @@ class Vector(Coordinates):
     def __new__(cls, initial, terminal=None):
         """
         Create a vector from a dictionary or a sequence that maps the symbols to
     def __new__(cls, initial, terminal=None):
         """
         Create a vector from a dictionary or a sequence that maps the symbols to
-        their coordinates, or as the difference between two points.
+        their coordinates, or as the displacement between two points.
         """
         if not isinstance(initial, Point):
             initial = Point(initial)
         """
         if not isinstance(initial, Point):
             initial = Point(initial)
@@ -283,10 +284,10 @@ class Vector(Coordinates):
         """
         Multiplies a Vector by a scalar value.
         """
         """
         Multiplies a Vector by a scalar value.
         """
-        if not isinstance(other, numbers.Real):
-            return NotImplemented
-        coordinates = self._map(lambda coordinate: other * coordinate)
-        return Vector(coordinates)
+        if isinstance(other, numbers.Real):
+            coordinates = self._map(lambda coordinate: other * coordinate)
+            return Vector(coordinates)
+        return NotImplemented
 
     __rmul__ = __mul__
 
 
     __rmul__ = __mul__
 
@@ -295,17 +296,18 @@ class Vector(Coordinates):
         Divide the vector by the specified scalar and returns the result as a
         vector.
         """
         Divide the vector by the specified scalar and returns the result as a
         vector.
         """
-        if not isinstance(other, numbers.Real):
-            return NotImplemented
-        coordinates = self._map(lambda coordinate: coordinate / other)
-        return Vector(coordinates)
+        if isinstance(other, numbers.Real):
+            coordinates = self._map(lambda coordinate: coordinate / other)
+            return Vector(coordinates)
+        return NotImplemented
 
     def __eq__(self, other):
         """
         Test whether two vectors are equal.
         """
 
     def __eq__(self, other):
         """
         Test whether two vectors are equal.
         """
-        return isinstance(other, Vector) and \
-            self._coordinates == other._coordinates
+        if isinstance(other, Vector):
+            return self._coordinates == other._coordinates
+        return NotImplemented
 
     def angle(self, other):
         """
 
     def angle(self, other):
         """
@@ -321,7 +323,7 @@ class Vector(Coordinates):
     def cross(self, other):
         """
         Compute the cross product of two 3D vectors. If either one of the
     def cross(self, other):
         """
         Compute the cross product of two 3D vectors. If either one of the
-        vectors is not tridimensional, a ValueError exception is raised.
+        vectors is not three-dimensional, a ValueError exception is raised.
         """
         if not isinstance(other, Vector):
             raise TypeError('other must be a Vector instance')
         """
         if not isinstance(other, Vector):
             raise TypeError('other must be a Vector instance')
@@ -348,7 +350,7 @@ class Vector(Coordinates):
         return result
 
     def __hash__(self):
         return result
 
     def __hash__(self):
-        return hash(tuple(self.coordinates()))
+        return super().__hash__()
 
     def norm(self):
         """
 
     def norm(self):
         """