-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrectangle.py
executable file
·141 lines (94 loc) · 3.45 KB
/
rectangle.py
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/python3
"""Rectangle Model"""
from models.base import Base
class Rectangle(Base):
"""A class that defines base initialization of rectangle model"""
def __init__(self, width, height, x=0, y=0, id=None):
"""A function that defines initialization of instances"""
super().__init__(id)
self.width = width
self.height = height
self.x = x
self.y = y
@property
def width(self):
"""A function that defines width instance of a rectangle"""
return (self.__width)
@width.setter
def width(self, value):
"""A function that passes the width attribute"""
if type(value) != int:
raise TypeError("width must be an integer")
if value <= 0:
raise ValueError("width must be > 0")
self.__width = value
@property
def height(self):
"""A function that defines height instance of a rectangle"""
return (self.__height)
@height.setter
def height(self, value):
"""A function that passes the height attribute"""
if type(value) != int:
raise TypeError("height must be an integer")
if value <= 0:
raise ValueError("height must be > 0")
self.__height = value
@property
def x(self):
"""A function that defines x attribute of a rectangle"""
return (self.__x)
@x.setter
def x(self, value):
"""A function that passes x attribute"""
if type(value) != int:
raise TypeError("x must be an integer")
if value < 0:
raise ValueError("x must be >= 0")
self.__x = value
@property
def y(self):
"""A function that defines y attribute of a rectangle"""
return (self.__y)
@y.setter
def y(self, value):
"""A function that passes y attribute"""
if type(value) != int:
raise TypeError("y must be an integer")
if value < 0:
raise ValueError("y must be >= 0")
self.__y = value
def area(self):
"""A function that computes the area of a rectangle"""
return (self.__width * self.__height)
def display(self):
"""A function that prints a rectangle with "#" character"""
rectangle = ""
print_symbol = "#"
print("\n" * self.y, end="")
for i in range(self.height):
rectangle += (" " * self.x) + (print_symbol*self.width) + "\n"
print(rectangle, end="")
def __str__(self):
"""A function that returns string format of a rectangle"""
return ("[{}] ({}) {}/{} - {}/{}".format(type(self).__name__, self.id,
self.__x, self.__y, self.__width, self.__height))
def update(self, *args, **kwargs):
"""A function that assigns key/value argument to attributes"""
if len(args) == 0:
for key, val in kwargs.items():
self.__setattr__(key, val)
return
try:
self.id = args[0]
self.width = args[1]
self.height = args[2]
self.x = args[3]
self.y = args[4]
except IndexError:
pass
def to_dictionary(self):
"""A function that returns the dict representation of a rectangle"""
return ({'x': getattr(self, "x"), 'y': getattr(self, "y"),
'id': getattr(self, "id"), 'height': getattr(self, "height"),
'width': getattr(self, "width")})