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 __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
- 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
- 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
- return isinstance(other, Vector) and \
- self._coordinates == other._coordinates
+ if isinstance(other, Vector):
+ return self._coordinates == other._coordinates
+ return NotImplemented
"""
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')