-from abc import ABC, abstractproperty, abstractmethod
-from collections import OrderedDict, Mapping
+from abc import ABC, abstractmethod, abstractproperty
+from collections import Mapping, OrderedDict
- Create a coordinate system from a dictionary or a sequence that maps the
- symbols to their coordinates. Coordinates must be rational numbers.
+ Create a coordinate system from a dictionary or a sequence that maps
+ the symbols to their coordinates. Coordinates must be rational numbers.
"""
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)
+ def __eq__(self, other):
+ """
+ Return True if two coordinate systems are equal.
+ """
+ if isinstance(other, self.__class__):
+ return self._coordinates == other._coordinates
+ return NotImplemented
+
def __hash__(self):
return hash(tuple(self.coordinates()))
def __repr__(self):
string = ', '.join(['{!r}: {!r}'.format(symbol, coordinate)
def __hash__(self):
return hash(tuple(self.coordinates()))
def __repr__(self):
string = ', '.join(['{!r}: {!r}'.format(symbol, coordinate)
def __sub__(self, other):
"""
If other is a point, substract it from self and return the resulting
def __sub__(self, other):
"""
If other is a point, substract it from self and return the resulting
- vector. If other is a vector, translate the point by the opposite vector
- and returns the resulting point.
+ vector. If other is a vector, translate the point by the opposite
+ vector and returns the resulting point.
- def __eq__(self, other):
- """
- Test whether two points are equal.
- """
- if isinstance(other, Point):
- return self._coordinates == other._coordinates
- return NotImplemented
-
- Create a vector from a dictionary or a sequence that maps the symbols to
- their coordinates, or as the displacement between two points.
+ Create a vector from a dictionary or a sequence that maps the symbols
+ to their coordinates, or as the displacement between two points.
- def __eq__(self, other):
- """
- Test whether two vectors are equal.
- """
- if isinstance(other, Vector):
- return self._coordinates == other._coordinates
- return NotImplemented
-