-
Notifications
You must be signed in to change notification settings - Fork 0
/
square.py
47 lines (39 loc) · 1.68 KB
/
square.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
#!/usr/bin/python3
""" Module with Square class that inherates from Rectangle class """
from models.base import Base
from models.rectangle import Rectangle
class Square(Rectangle):
""" class inherated from Rectangle from Base """
def __init__(self, size, x=0, y=0, id=None):
""" Initilize by sending args to Base cls & validating them """
super().__init__(size, size, x, y, id)
def __str__(self):
""" Overrides default print behavior for Square class """
# format that get value by calling self
return "[Square] ({0.id}) {0.x}/{0.y} - {0.width}".format(self)
@property
def size(self):
""" Size setter and getter returns width """
return self.width
@size.setter
def size(self, value):
self.width = value
self.height = value
def update(self, *args, **kwargs):
""" Updates attributes in class Square"""
if args: # if args is passed
SaqAttr = iter(['id', 'size', 'x', 'y']) # iter for next()
# calls respective setter method
[setattr(self, next(SaqAttr), arg) for arg in args]
if kwargs: # if kwargs is passed
for key, value in kwargs.items():
if hasattr(self, key): # key validation
# calls respective setter method
setattr(self, key, value)
else: # if key doesnt exist dont create attribute
continue
def to_dictionary(self):
""" Returns: dict of instances attributes """
SaqAttr = ['id', 'size', 'x', 'y'] # get following attr
newdict = {attr: getattr(self, attr) for attr in SaqAttr}
return newdict