77 lines
1.7 KiB
Python
77 lines
1.7 KiB
Python
import math
|
||
from abc import abstractmethod
|
||
from abc import ABC
|
||
|
||
|
||
class Shape(ABC):
|
||
color = ''
|
||
|
||
@abstractmethod
|
||
def area(self):
|
||
pass
|
||
|
||
@abstractmethod
|
||
def perimeter(self):
|
||
pass
|
||
|
||
@staticmethod
|
||
def create_from_json(json):
|
||
if 'type' in json:
|
||
shape_type = json['type']
|
||
|
||
if shape_type == 'rectangle':
|
||
return Rectangle(json['width', 'height'])
|
||
|
||
elif shape_type == 'square':
|
||
return Square(json['side'])
|
||
|
||
elif shape_type == 'circle':
|
||
return Circle(json('radius'))
|
||
|
||
else:
|
||
raise TypeError(f'вот тебе >>>(оIo), а не фигура {shape_type}')
|
||
|
||
@staticmethod
|
||
def covert_to_pro(obj):
|
||
if isinstance(obj, Shape):
|
||
result = obj.__dict__
|
||
result['className'] = obj.__class__.__name__
|
||
return result
|
||
|
||
class Rectangle(Shape):
|
||
def __init__(self, w, h):
|
||
self.w = w
|
||
self.h = h
|
||
|
||
def perimeter(self):
|
||
return 2 * (self.w + self.h)
|
||
|
||
def area(self):
|
||
return self.w * self.h
|
||
|
||
class Square(Shape):
|
||
def __init__(self, a):
|
||
self.a = a
|
||
|
||
def perimeter(self):
|
||
return 4 * self.a
|
||
|
||
def area(self):
|
||
return self.a * 2
|
||
|
||
class Circle(Shape):
|
||
def __init__(self, rad):
|
||
self.radius = rad
|
||
|
||
def perimeter(self):
|
||
return 2 * math.pi * self.radius
|
||
|
||
def area(self):
|
||
return math.pi + (self.radius ** 2)
|
||
|
||
|
||
def json_to_python():
|
||
with open('shapes.json') as shapes:
|
||
data = json.loads(shapes)
|
||
return data
|