Browse Source

refactor: added class _PolygonalCircuit

Fabian Peter Hammerle 5 years ago
parent
commit
6b27567174
2 changed files with 24 additions and 19 deletions
  1. 12 19
      freesurfer_surface/__init__.py
  2. 12 0
      tests/test_triangle.py

+ 12 - 19
freesurfer_surface/__init__.py

@@ -63,7 +63,7 @@ def setlocale(temporary_locale):
 Vertex = collections.namedtuple('Vertex', ['right', 'anterior', 'superior'])
 
 
-class _LineSegment:
+class _PolygonalCircuit:
 
     _VERTEX_INDICES_TYPE = typing.Tuple[int]
 
@@ -78,40 +78,33 @@ class _LineSegment:
 
     @vertex_indices.setter
     def vertex_indices(self, indices: _VERTEX_INDICES_TYPE):
-        assert len(indices) == 2
         self._vertex_indices = indices
 
-    def __eq__(self, other: '_Line') -> bool:
+    def __eq__(self, other: '_PolygonalCircuit') -> bool:
         return self.vertex_indices == other.vertex_indices
 
     def __hash__(self) -> int:
         return hash(self._vertex_indices)
 
-    def __repr__(self) -> str:
-        return '_LineSegment(vertex_indices={})'.format(self.vertex_indices)
 
+class _LineSegment(_PolygonalCircuit):
 
-class Triangle:
+    @_PolygonalCircuit.vertex_indices.setter
+    def vertex_indices(self, indices: _PolygonalCircuit._VERTEX_INDICES_TYPE):
+        assert len(indices) == 2
+        self._vertex_indices = indices
 
-    _VERTEX_INDICES_TYPE = typing.Tuple[int]
+    def __repr__(self) -> str:
+        return '_LineSegment(vertex_indices={})'.format(self.vertex_indices)
 
-    _vertex_indices: _VERTEX_INDICES_TYPE
 
-    def __init__(self, vertex_indices: _VERTEX_INDICES_TYPE):
-        self.vertex_indices = vertex_indices
-
-    @property
-    def vertex_indices(self):
-        return self._vertex_indices
+class Triangle(_PolygonalCircuit):
 
-    @vertex_indices.setter
-    def vertex_indices(self, indices: _VERTEX_INDICES_TYPE):
+    @_PolygonalCircuit.vertex_indices.setter
+    def vertex_indices(self, indices: _PolygonalCircuit._VERTEX_INDICES_TYPE):
         assert len(indices) == 3
         self._vertex_indices = indices
 
-    def __eq__(self, other: 'Triangle') -> bool:
-        return self.vertex_indices == other.vertex_indices
-
     def __repr__(self) -> str:
         return 'Triangle(vertex_indices={})'.format(self.vertex_indices)
 

+ 12 - 0
tests/test_triangle.py

@@ -13,6 +13,18 @@ def test_init_invalid_indices_len():
         Triangle((0, 21, 42, 84))
 
 
+def test_reassign_vertex_indices():
+    triangle = Triangle((0, 21, 42))
+    triangle.vertex_indices = (1, 2, 3)
+    assert triangle.vertex_indices == (1, 2, 3)
+
+
+def test_reassign_vertex_indices_invalid_len():
+    triangle = Triangle((0, 21, 42))
+    with pytest.raises(Exception):
+        triangle.vertex_indices = (1, 2, 3, 4)
+
+
 def test_eq():
     assert Triangle((0, 1, 2)) == Triangle((0, 1, 2))
     # pylint: disable=unneeded-not