X-Git-Url: https://scm.cri.mines-paristech.fr/git/linpy.git/blobdiff_plain/b595adab7a332fccf90714d194e70fdf73e458e3..6ec23dc57252ffe01aa60595fc499f580381e4a9:/pypol/coordinates.py?ds=inline diff --git a/pypol/coordinates.py b/pypol/coordinates.py index 7923648..ceab418 100644 --- a/pypol/coordinates.py +++ b/pypol/coordinates.py @@ -2,9 +2,9 @@ import math import numbers import operator -from abc import ABC, abstractmethod from collections import OrderedDict, Mapping +from .geometry import GeometricObject from .linexprs import Symbol @@ -14,15 +14,25 @@ __all__ = [ ] -class Coordinates(ABC): +class 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): @@ -69,25 +79,11 @@ class Coordinates(ABC): yield symbol, func(coordinate1, coordinate2) -class Point(Coordinates): +class Point(Coordinates, GeometricObject): """ This class represents points in space. """ - def __new__(cls, coordinates=None): - 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) @@ -125,20 +121,15 @@ class Vector(Coordinates): This class represents displacements in space. """ - __slots__ = ( - '_coordinates', - ) - def __new__(cls, initial, terminal=None): - self = object().__new__(cls) 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) - 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)