Загружаю первый рабочий вариант ДЗ по классам (без вызова класса в стат методе

This commit is contained in:
ehermakov 2023-08-28 09:00:52 +03:00
parent f42cb4feda
commit f8df1e5fa0
4 changed files with 14 additions and 13 deletions

View File

@ -1,18 +1,17 @@
import json import json
from shape import Shape from shape import Shape
def json_to_python(): def json_to_python():
with open("shapes.json") as lines: with open("shapes.json") as lines:
data = json.load(lines) data = json.load(lines)
for shape in data["shapes"]: for i in data["shapes"]:
print("Type:", shape["type"])
print("Color:", shape["color"])
print() print()
sh1 = shape.create_shape(shape) print("Type:", i["type"])
print(sh1.perimetr()) print("Color:", i["color"])
print(sh1.area()) sh1 = Shape.create_shape(i)
return True print(sh1)
return "Программа завершена"
print(json_to_python()) print(json_to_python())

View File

@ -17,12 +17,15 @@ class Shape(ABC):
def create_shape(json): def create_shape(json):
if "type" in json: if "type" in json:
shape_type = json["type"] shape_type = json["type"]
if shape_type == "circle": if shape_type == "circle":
return Circle(json("radius")) return json["radius"]
elif shape_type == "square": elif shape_type == "square":
return Square(json("side")) return json["side"]
elif shape_type == "rectangle": elif shape_type == "rectangle":
return Rectangle(json["width"], json["height"]) return json["width"], json["height"]
else: else:
raise TypeError(f'Неизвестная фигура {shape_type}') raise TypeError(f'Неизвестная фигура {shape_type}')
print()

View File

@ -4,7 +4,6 @@ from shape import Shape
class Square(Shape): class Square(Shape):
def __init__(self, side): def __init__(self, side):
self.side = side self.side = side
self.color = color
def area(self): def area(self):
return self.side ** 2 return self.side ** 2