diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/Shape change.json b/HNS/Excercises/13082023 ДЗ по фигурам JSON/Shape change.json index feaa329..2adc9f7 100644 --- a/HNS/Excercises/13082023 ДЗ по фигурам JSON/Shape change.json +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/Shape change.json @@ -1 +1 @@ -{"shapes": [{"width": 5, "height": 10, "type": "Rectangle"}, {"radius": 7, "type": "Circle"}, {"side": 4, "type": "Square"}]} \ No newline at end of file +{"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, {"side": 0, "color": "unknown", "type": "Square"}]} \ No newline at end of file diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/classes.py b/HNS/Excercises/13082023 ДЗ по фигурам JSON/classes.py index 1bb95ae..60d0723 100644 --- a/HNS/Excercises/13082023 ДЗ по фигурам JSON/classes.py +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/classes.py @@ -20,10 +20,9 @@ class Shape(ABC): if "type" in json: shape_type = json["type"].lower() if shape_type == "circle": - radius = - return Circle(json["radius"]) + radius = 'radius' elif shape_type == "square": - return Square(json["side"]) + side = 'side' elif shape_type == "rectangle": return Rectangle(json["width"], json["height"]) else: @@ -33,10 +32,10 @@ class Shape(ABC): @staticmethod def convert_to_json(obj): - global result if isinstance(obj, Shape): result = obj.__dict__ result["type"] = obj.__class__.__name__ + result['color'] = obj.color return result def safe_read (shape_type, property, default_value): diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py b/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py index 620344f..a73f17e 100755 --- a/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py @@ -1,29 +1,73 @@ import json +import random + from classes import Shape, Square, Circle, Rectangle -def json_to_python(): +def safe_read(obj, property, default_value): + if property in obj: + return obj[property] + return default_value + + +def create_shape(json): + if 'type' in json: + shape_type = json['type'].lower() + if shape_type == 'circle': + radius = safe_read(json, 'radius', 0) + obj = Circle(radius) + elif shape_type == 'square': + side = safe_read(json, 'side', 0) + obj = Square(side) + elif shape_type == 'rectangle': + width = safe_read(json, 'width', 0) + height = safe_read(json, 'height', 0) + obj = Rectangle(width, height) + else: + raise TypeError(f'вот тебе >>>(оIo), а не фигура {shape_type}') + obj.color = safe_read(json, 'color', 'unknown') + + return obj + + +def generate_shape(): + types = ['square', 'circle', 'rectangle'] + rnd = random.randint(0, len(types) - 1) + shape_type = types[rnd] + if shape_type == 'circle': + obj = Circle(rnd) + elif shape_type == 'square': + obj = Square(rnd) + elif shape_type == 'rectangle': + obj = Rectangle(rnd, rnd) + else: + raise TypeError(f'Происходит что-то непонятное') + obj.color = 'unknown' + return obj + + +def json_to_python(filename): data_list = [] - with open("shapes.json") as lines: + with open(filename) as lines: data = json.load(lines) for i in data["shapes"]: - print() - print("Type:", i["type"]) - print("Color:", i["color"]) - sh1 = Shape.create_shape(i) - print(f'area = {sh1.area()}') - print(f'perimetr = {sh1.perimetr()}') + sh1 = create_shape(i) data_list.append(sh1) - for j in data_list: - print(j.convert_to_json(j)) - - data_list2 = ("{\"shapes\": " + json.dumps(data_list, default=Shape.convert_to_json) + "}") - print(data_list2) - - with open('Shape change.json', 'w') as f: - f.write(data_list2) + return data_list +def python_to_json(data, filename): + with open(filename, 'w') as f: + json.dump({'shapes': data}, f, default=Shape.convert_to_json) -json_to_python() +def filter_shapes(data, area): + return [shape for shape in data if shape.area() >= area] + + +shape_list = json_to_python('shapes.json') +shape_list = filter_shapes(shape_list, 100) +shape_list.append(generate_shape()) + +python_to_json(shape_list, 'shape change.json') + diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/shapes.json b/HNS/Excercises/13082023 ДЗ по фигурам JSON/shapes.json index 1c316ff..fbda6d0 100755 --- a/HNS/Excercises/13082023 ДЗ по фигурам JSON/shapes.json +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/shapes.json @@ -1,20 +1 @@ -{ - "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 +{"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, {"width": 2, "height": 2, "color": "unknown", "type": "Rectangle"}, {"side": 0, "color": "unknown", "type": "Square"}]} \ No newline at end of file