-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshape.py
More file actions
95 lines (75 loc) · 2.43 KB
/
shape.py
File metadata and controls
95 lines (75 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
"""
Shape module - Abstract base class for geometric 2D shapes.
Provides:
- Common attributes (x, y position)
- Abstract properties for area and perimeter
- Comparison operators based on area and perimeter
- Translation functionality for moving shapes
Source reference:
https://docs.python.org/3/library/abc.html
"""
from abc import ABC, abstractmethod
from utils import validate_number
class Shape(ABC):
"""Abstract base class for 2D shapes."""
def __init__(self, x: float = 0, y: float = 0):
"""Initialize shape with optional position (x, y)."""
self.x = x
self.y = y
@property
def x(self) -> float:
return self._x
@x.setter
def x(self, value: float):
validate_number(value)
self._x = float(value)
@property
def y(self) -> float:
return self._y
@y.setter
def y(self, value: float):
validate_number(value)
self._y = float(value)
"""Abstract properties"""
@property
@abstractmethod
def area(self) -> float:
"""Return the area of the shape."""
pass
@property
@abstractmethod
def perimeter(self) -> float:
"""Return the perimeter of the shape."""
pass
"""Comparison operators"""
def __eq__(self, other):
if not isinstance(other, Shape):
return NotImplemented
return self.area == other.area and self.perimeter == other.perimeter
def __lt__(self, other):
if not isinstance(other, Shape):
return NotImplemented
if self.area != other.area:
return self.area < other.area
return self.perimeter < other.perimeter
def __le__(self, other):
return self == other or self < other
def __gt__(self, other):
if not isinstance(other, Shape):
return NotImplemented
if self.area != other.area:
return self.area > other.area
return self.perimeter > other.perimeter
def __ge__(self, other):
return self == other or self > other
"""Utility methods"""
def translate(self, dx: float, dy: float) -> None:
"""Move the shape by dx and dy."""
validate_number(dx)
validate_number(dy)
self.x += dx
self.y += dy
def __repr__(self) -> str:
return f"{self.__class__.__name__}(x={self.x}, y={self.y})"
def __str__(self) -> str:
return f"{self.__class__.__name__} at position ({self.x}, {self.y})"