|
| 1 | +from collections.abc import MutableSequence |
| 2 | + |
| 3 | + |
| 4 | +class ShapePoints(MutableSequence): |
| 5 | + MIN_POINTS = 3 |
| 6 | + |
| 7 | + def __init__(self, points): |
| 8 | + self.points = list(points) |
| 9 | + if len(self.points) < self.MIN_POINTS: |
| 10 | + raise ValueError(f"Shape must have at least {self.MIN_POINTS} points") |
| 11 | + if points and self.points[0] != self.points[-1]: |
| 12 | + self.points.append(self.points[0]) |
| 13 | + |
| 14 | + def __repr__(self): |
| 15 | + return f"ShapePoints({self.points})" |
| 16 | + |
| 17 | + def __getitem__(self, index): |
| 18 | + return self.points[index] |
| 19 | + |
| 20 | + def __len__(self): |
| 21 | + if self.points: |
| 22 | + return len(self.points) - 1 |
| 23 | + return 0 |
| 24 | + |
| 25 | + def __iter__(self): |
| 26 | + return iter(self.points) |
| 27 | + |
| 28 | + def __contains__(self, item): |
| 29 | + print("Checking if item is in ShapePoints") |
| 30 | + return item in self.points |
| 31 | + |
| 32 | + def __delitem__(self, index): |
| 33 | + if len(self) < self.MIN_POINTS + 1: |
| 34 | + raise ValueError(f"Shape must have at least {self.MIN_POINTS} points") |
| 35 | + if index in (0, len(self.points) - 1, -1): |
| 36 | + del self.points[0] |
| 37 | + self.points[-1] = self.points[0] |
| 38 | + else: |
| 39 | + del self.points[index] |
| 40 | + |
| 41 | + def __setitem__(self, index, value): |
| 42 | + if index in (0, len(self.points) - 1, -1): |
| 43 | + self.points[0] = value |
| 44 | + self.points[-1] = value |
| 45 | + else: |
| 46 | + self.points[index] = value |
| 47 | + |
| 48 | + def insert(self, index, value): |
| 49 | + if index in (0, len(self.points) - 1, -1): |
| 50 | + self.points.insert(0, value) |
| 51 | + self.points[-1] = value |
| 52 | + else: |
| 53 | + self.points.insert(index, value) |
| 54 | + |
| 55 | + def count(self, value): |
| 56 | + return self.points[:-1].count(value) |
| 57 | + |
| 58 | + def append(self, value): |
| 59 | + self.points.append(self.points[0]) |
| 60 | + self.points[-2] = value |
0 commit comments