From 74e980176d75c52d5c0016ff50635aece45e04b2 Mon Sep 17 00:00:00 2001 From: ehermakov Date: Sun, 3 Sep 2023 11:53:21 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B6=D0=B0?= =?UTF-8?q?=D1=8E=20=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=BD?= =?UTF-8?q?=D0=BD=D0=BE=D0=B5=20=D0=94=D0=97=20=D0=BF=D0=BE=20json?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../13082023 ДЗ по фигурам JSON/classes.py | 75 +++++++++++++++++++ .../13082023 ДЗ по фигурам JSON/main.py | 11 ++- 2 files changed, 83 insertions(+), 3 deletions(-) create mode 100644 HNS/Excercises/13082023 ДЗ по фигурам JSON/classes.py diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/classes.py b/HNS/Excercises/13082023 ДЗ по фигурам JSON/classes.py new file mode 100644 index 0000000..346f514 --- /dev/null +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/classes.py @@ -0,0 +1,75 @@ +import json +from abc import abstractmethod +from abc import ABC +import math + + +class Shape(ABC): + color = '' + + @abstractmethod + def area(self): + pass + + @abstractmethod + def perimetr(self): + pass + + @staticmethod + def create_shape(json): + if "type" in json: + shape_type = json["type"] + if shape_type == "circle": + return Circle(json["radius"]) + elif shape_type == "square": + return Square(json["side"]) + elif shape_type == "rectangle": + return Rectangle(json["width"], json["height"]) + else: + raise TypeError(f'Неизвестная фигура {shape_type}') + + @staticmethod + def convert_to_json(obj): + if isinstance(obj, Shape): + result = obj.__dict__ + result["type"] = obj.__class__.__name__ + return json.dumps(result) + + +class Rectangle(Shape): + def __init__(self, width, height): + self.width = width + self.height = height + + def area(self): + return self.width * self.height + + def perimetr(self): + return (self.width + self.height) * 2 + + +class Square(Shape): + def __init__(self, side): + self.side = side + + def area(self): + return self.side ** 2 + + def perimetr(self): + return 4 * self.side + + +class Circle(Shape): + def __init__(self, radius): + self.radius = radius + + def area(self): + return math.pi * (self.radius ** 2) + + def perimetr(self): + return 2 * math.pi * self.radius + + +r1 = Rectangle(10, 20) +s1 = Square(22) +c1 = Circle(10) diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py b/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py index 62592d9..fbc7537 100755 --- a/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py @@ -1,7 +1,9 @@ import json -from shape import Shape +from classes import Shape, Square, Circle, Rectangle + def json_to_python(): + data_list = [] with open("shapes.json") as lines: data = json.load(lines) for i in data["shapes"]: @@ -9,8 +11,11 @@ def json_to_python(): print("Type:", i["type"]) print("Color:", i["color"]) sh1 = Shape.create_shape(i) - print(sh1) - + print(f'area = {sh1.area()}') + print(f'perimetr = {sh1.perimetr()}') + data_list.append(sh1) + for j in data_list: + print(j.convert_to_json(j)) return "Программа завершена"