Skip to content

Commit f32e4b1

Browse files
committed
fix custom magic methods and add tests
1 parent 0c234a2 commit f32e4b1

File tree

7 files changed

+126
-7
lines changed

7 files changed

+126
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1919
* Fixed bug where mimic joints were considered configurable.
2020
* Fixed bug where `!=` gave incorrect results in Rhino for some compas objects.
2121
* Fixed bug where `compas_rhino.BaseArtist.redraw` did not trigger a redraw.
22+
* Fixed minor bugs in `compas.geometry.Polyline` and `compas.geometry.Polygon`.
23+
* Fixed very minor bugs in `compas.geometry.Frame` and `compas.geometry.Quaternion`.
2224

2325
### Removed
2426

src/compas/geometry/primitives/frame.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ def __iter__(self):
180180
return iter([self.point, self.xaxis, self.yaxis])
181181

182182
def __eq__(self, other, tol=1e-05):
183+
if not hasattr(other, '__iter__') or not hasattr(other, '__len__') or len(self) != len(other):
184+
return False
183185
for v1, v2 in zip(self, other):
184186
for a, b in zip(v1, v2):
185187
if math.fabs(a - b) > tol:

src/compas/geometry/primitives/polygon.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def area(self):
147147
# ==========================================================================
148148

149149
def __repr__(self):
150-
return "Polygon({})".format(", ".join(["{}".format(point) for point in self.points]))
150+
return "Polygon([{}])".format(", ".join(["{}".format(point) for point in self.points]))
151151

152152
def __len__(self):
153153
return len(self.points)
@@ -156,12 +156,15 @@ def __getitem__(self, key):
156156
return self.points[key]
157157

158158
def __setitem__(self, key, value):
159-
self.points[key] = value
159+
self.points[key] = Point(*value)
160+
self._lines = None
160161

161162
def __iter__(self):
162163
return iter(self.points)
163164

164165
def __eq__(self, other):
166+
if not hasattr(other, '__iter__') or not hasattr(other, '__len__') or len(self) != len(other):
167+
return False
165168
return all(a == b for a, b in zip(self, other))
166169

167170
# ==========================================================================

src/compas/geometry/primitives/polyline.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def length(self):
109109
# ==========================================================================
110110

111111
def __repr__(self):
112-
return "Polyline({})".format(", ".join(["{}".format(point) for point in self.points]))
112+
return "Polyline([{}])".format(", ".join(["{}".format(point) for point in self.points]))
113113

114114
def __len__(self):
115115
return len(self.points)
@@ -118,12 +118,15 @@ def __getitem__(self, key):
118118
return self.points[key]
119119

120120
def __setitem__(self, key, value):
121-
self.points[key] = value
121+
self.points[key] = Point(*value)
122+
self._lines = None
122123

123124
def __iter__(self):
124125
return iter(self.points)
125126

126127
def __eq__(self, other):
128+
if not hasattr(other, '__iter__') or not hasattr(other, '__len__') or len(self) != len(other):
129+
return False
127130
return all(a == b for a, b in zip(self, other))
128131

129132
# ==========================================================================

src/compas/geometry/primitives/quaternion.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ def __setitem__(self, key, value):
217217
raise KeyError
218218

219219
def __eq__(self, other, tol=1e-05):
220+
if not hasattr(other, '__iter__') or not hasattr(other, '__len__') or len(self) != len(other):
221+
return False
220222
for v1, v2 in zip(self, other):
221223
if math.fabs(v1 - v2) > tol:
222224
return False
@@ -225,6 +227,9 @@ def __eq__(self, other, tol=1e-05):
225227
def __iter__(self):
226228
return iter(self.wxyz)
227229

230+
def __len__(self):
231+
return 4
232+
228233
def __repr__(self):
229234
return 'Quaternion({:.{prec}f}, {:.{prec}f}, {:.{prec}f}, {:.{prec}f})'.format(self.w, self.x, self.y, self.z, prec=3)
230235

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import pytest
2+
3+
from compas.geometry import Point
4+
from compas.geometry import Polygon
5+
from compas.utilities import pairwise
6+
7+
8+
def test_polygon():
9+
points = [[0, 0, x] for x in range(5)]
10+
polygon = Polygon(points)
11+
assert polygon.points == points
12+
assert polygon.lines == [(a, b) for a, b in pairwise(points + points[:1])]
13+
14+
15+
def test_equality():
16+
points1 = [[0, 0, x] for x in range(5)]
17+
polygon1 = Polygon(points1)
18+
points2 = [[0, 0, x] for x in range(6)]
19+
polygon2 = Polygon(points2)
20+
points3 = [[0, 0, x] for x in range(5)] + [[0, 0, 0]]
21+
polygon3 = Polygon(points3)
22+
assert polygon1 == polygon1
23+
assert polygon1 == points1
24+
assert points1 == polygon1
25+
assert polygon1 != polygon2
26+
assert polygon2 != polygon1
27+
assert polygon1 != points2
28+
assert points2 != polygon1
29+
assert polygon1 != 1
30+
assert polygon1 == polygon3
31+
32+
33+
def test___repr__():
34+
points = [[0, 0, x] for x in range(5)]
35+
polygon = Polygon(points)
36+
assert polygon == eval(repr(polygon))
37+
38+
39+
def test___getitem__():
40+
points = [[0, 0, x] for x in range(5)]
41+
polygon = Polygon(points)
42+
for x in range(5):
43+
assert polygon[x] == [0, 0, x]
44+
with pytest.raises(IndexError):
45+
polygon[6] = [0, 0, 6]
46+
47+
48+
def test___setitem__():
49+
points = [[0, 0, x] for x in range(5)]
50+
polygon = Polygon(points)
51+
point = [1, 1, 4]
52+
polygon[4] = point
53+
assert polygon[4] == point
54+
assert isinstance(polygon[4], Point)
55+
assert polygon.lines[-2].end == point

tests/compas/geometry/test_polyline.py

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,56 @@
1-
import compas
2-
import pytest
3-
from compas.geometry import Polyline, Point
41
import math
2+
import pytest
3+
4+
from compas.geometry import Point
5+
from compas.geometry import Polyline
6+
from compas.utilities import pairwise
7+
8+
9+
def test_polyline():
10+
points = [[0, 0, x] for x in range(5)]
11+
polyline = Polyline(points)
12+
assert polyline.points == points
13+
assert polyline.lines == [(a, b) for a, b in pairwise(points)]
14+
15+
16+
def test_equality():
17+
points1 = [[0, 0, x] for x in range(5)]
18+
polyline1 = Polyline(points1)
19+
points2 = [[0, 0, x] for x in range(6)]
20+
polyline2 = Polyline(points2)
21+
assert polyline1 == polyline1
22+
assert polyline1 == points1
23+
assert points1 == polyline1
24+
assert polyline1 != polyline2
25+
assert polyline2 != polyline1
26+
assert polyline1 != points2
27+
assert points2 != polyline1
28+
assert polyline1 != 1
29+
30+
31+
def test___repr__():
32+
points = [[0, 0, x] for x in range(5)]
33+
polyline = Polyline(points)
34+
assert polyline == eval(repr(polyline))
35+
36+
37+
def test___getitem__():
38+
points = [[0, 0, x] for x in range(5)]
39+
polyline = Polyline(points)
40+
for x in range(5):
41+
assert polyline[x] == [0, 0, x]
42+
with pytest.raises(IndexError):
43+
polyline[6] = [0, 0, 6]
44+
45+
46+
def test___setitem__():
47+
points = [[0, 0, x] for x in range(5)]
48+
polyline = Polyline(points)
49+
point = [1, 1, 4]
50+
polyline[4] = point
51+
assert polyline[4] == point
52+
assert isinstance(polyline[4], Point)
53+
assert polyline.lines[-1].end == point
554

655

756
@pytest.mark.parametrize('coords,expected', [

0 commit comments

Comments
 (0)