From fa39aa508e470deb5336db63a2afb75a1d7ab0b1 Mon Sep 17 00:00:00 2001 From: vitalii Malcov Date: Tue, 12 Sep 2023 19:17:10 +0200 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20?= =?UTF-8?q?=D0=BF=D0=B0=D0=BF=D0=BA=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HNS/Excercises/Json/Circle.py | 13 +++++ HNS/Excercises/Json/Rectangle.py | 13 +++++ HNS/Excercises/Json/Shape.py | 23 +++++++++ HNS/Excercises/Json/Shapes zwei.json | 1 + HNS/Excercises/Json/Shapes zwei.json.json | 0 HNS/Excercises/Json/Square.py | 12 +++++ HNS/Excercises/Json/main.py | 61 +++++++++++++++++++++++ HNS/Excercises/Json/shapes.json | 20 ++++++++ 8 files changed, 143 insertions(+) create mode 100644 HNS/Excercises/Json/Circle.py create mode 100644 HNS/Excercises/Json/Rectangle.py create mode 100644 HNS/Excercises/Json/Shape.py create mode 100644 HNS/Excercises/Json/Shapes zwei.json create mode 100644 HNS/Excercises/Json/Shapes zwei.json.json create mode 100644 HNS/Excercises/Json/Square.py create mode 100644 HNS/Excercises/Json/main.py create mode 100644 HNS/Excercises/Json/shapes.json diff --git a/HNS/Excercises/Json/Circle.py b/HNS/Excercises/Json/Circle.py new file mode 100644 index 0000000..ab888b8 --- /dev/null +++ b/HNS/Excercises/Json/Circle.py @@ -0,0 +1,13 @@ +import math +from Shape import Shape + + +class Circle(Shape): + def __init__(self, radius): + self.radius = radius + + def perimeter(self): + return 2 * math.pi * self.radius + + def area(self): + return math.pi + (self.radius ** 2) diff --git a/HNS/Excercises/Json/Rectangle.py b/HNS/Excercises/Json/Rectangle.py new file mode 100644 index 0000000..99e5e29 --- /dev/null +++ b/HNS/Excercises/Json/Rectangle.py @@ -0,0 +1,13 @@ +from Shape import Shape + + +class Rectangle(Shape): + def __init__(self, width, height): + self.width = width + self.height = height + + def perimeter(self): + return 2 * (self.width + self.height) + + def area(self): + return self.width * self.height diff --git a/HNS/Excercises/Json/Shape.py b/HNS/Excercises/Json/Shape.py new file mode 100644 index 0000000..8990f48 --- /dev/null +++ b/HNS/Excercises/Json/Shape.py @@ -0,0 +1,23 @@ +import math +from abc import abstractmethod + + +class Shape: + color = '' + + @abstractmethod + def area(self): + pass + + @abstractmethod + def perimeter(self): + pass + + @staticmethod + def convert_to_json(obj): + if isinstance(obj, Shape): + result = obj.__dict__ + result['type'] = obj.__class__.__name__ + result['color'] = obj.color + return result + diff --git a/HNS/Excercises/Json/Shapes zwei.json b/HNS/Excercises/Json/Shapes zwei.json new file mode 100644 index 0000000..92be808 --- /dev/null +++ b/HNS/Excercises/Json/Shapes zwei.json @@ -0,0 +1 @@ +"{'shapes': [{\"width\": 5, \"height\": 10, \"color\": \"red\", \"type\": \"Rectangle\"}, {\"radius\": 7, \"color\": \"blue\", \"type\": \"Circle\"}, {\"side\": 4, \"color\": \"green\", \"type\": \"Square\"}]}" \ No newline at end of file diff --git a/HNS/Excercises/Json/Shapes zwei.json.json b/HNS/Excercises/Json/Shapes zwei.json.json new file mode 100644 index 0000000..e69de29 diff --git a/HNS/Excercises/Json/Square.py b/HNS/Excercises/Json/Square.py new file mode 100644 index 0000000..beb4947 --- /dev/null +++ b/HNS/Excercises/Json/Square.py @@ -0,0 +1,12 @@ +from Shape import Shape + + +class Square(Shape): + def __init__(self, side): + self.side = side + + def perimeter(self): + return 4 * self.side + + def area(self): + return self.side * 2 diff --git a/HNS/Excercises/Json/main.py b/HNS/Excercises/Json/main.py new file mode 100644 index 0000000..a1566b7 --- /dev/null +++ b/HNS/Excercises/Json/main.py @@ -0,0 +1,61 @@ +import json +from Shape import Shape +from Circle import Circle +from Rectangle import Rectangle +from Square import Square + + +def safe_read(obj, property, default_value): + obj == property + + +def create_shape(json): + if 'type' in json: + shape_type = json['type'].lower() + + if shape_type == 'circle': + if 'radius' in json: + obj = Circle(json['radius']) + else: + obj = Circle(0) + + elif shape_type == 'square': + if 'side' in json: + obj = Square(json['side']) + else: + obj = Square(0) + + elif shape_type == 'rectangle': + obj = Rectangle(json['width'], json['height']) + else: + raise TypeError(f'вот тебе >>>(оIo), а не фигура {shape_type}') + + if 'color' in json: + obj.color = json['color'] + else: + obj.color = 'unknown' + + return obj + + +def json_to_python(): + data_list = [] + with open('shapes.json') as lines: + data = json.load(lines) + for i in data['shapes']: + sh1 = create_shape(i) + data_list.append(sh1) + + data2 = '{\'shapes\': ' + json.dumps(data_list, default=Shape.convert_to_json) + '}' + with open('Shapes zwei.json', 'w') as f: + json.dump(data2, f) + + print('{\'shapes\': ' + json.dumps(data_list, default=Shape.convert_to_json) + '}') + print(json.dumps({'shapes': data_list}, default=Shape.convert_to_json)) + +json_to_python() + + + + + diff --git a/HNS/Excercises/Json/shapes.json b/HNS/Excercises/Json/shapes.json new file mode 100644 index 0000000..7661371 --- /dev/null +++ b/HNS/Excercises/Json/shapes.json @@ -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" + } + ] +} \ No newline at end of file