сделал изменения и добавил файлы
This commit is contained in:
parent
e1ac706159
commit
e794f1333e
|
@ -0,0 +1,13 @@
|
||||||
|
import math
|
||||||
|
import Shape
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
|
@ -0,0 +1,13 @@
|
||||||
|
import Shape
|
||||||
|
|
||||||
|
|
||||||
|
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
|
|
@ -0,0 +1,12 @@
|
||||||
|
import Shape
|
||||||
|
|
||||||
|
|
||||||
|
class Square(Shape):
|
||||||
|
def __init__(self, a):
|
||||||
|
self.a = a
|
||||||
|
|
||||||
|
def perimeter(self):
|
||||||
|
return 4 * self.a
|
||||||
|
|
||||||
|
def area(self):
|
||||||
|
return self.a * 2
|
|
@ -0,0 +1,76 @@
|
||||||
|
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
|
|
@ -0,0 +1,17 @@
|
||||||
|
import json
|
||||||
|
from classes import Shapes
|
||||||
|
|
||||||
|
|
||||||
|
def json_to_python():
|
||||||
|
with open('shapes.json') as shapes:
|
||||||
|
data = json.load(shapes)
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
obj = json_to_python()
|
||||||
|
|
||||||
|
for shape in obj['shapes']:
|
||||||
|
sh1 = Shape.create_shape(shape)
|
||||||
|
|
||||||
|
print(sh1.perimeter())
|
||||||
|
print(sh1.area())
|
|
@ -0,0 +1,20 @@
|
||||||
|
{
|
||||||
|
"shapes": [
|
||||||
|
{
|
||||||
|
"type": "rectangle",
|
||||||
|
"width": 5,
|
||||||
|
"height": 10,
|
||||||
|
"color": "red"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "circle",
|
||||||
|
"radius": 7,
|
||||||
|
"color": "blue"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "square",
|
||||||
|
"side": 4,
|
||||||
|
"color": "green"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
Loading…
Reference in New Issue