From 722f59a1d3e3254ae09a93da9bfc358a917a579c Mon Sep 17 00:00:00 2001 From: ehermakov Date: Mon, 2 Oct 2023 23:40:21 +0300 Subject: [PATCH] download tasks with Enum --- .../.idea/13082023 ДЗ по фигурам JSON.iml | 2 +- .../.idea/misc.xml | 2 +- .../Shape change.json | 2 +- .../Shapechange.json | 0 .../13082023 ДЗ по фигурам JSON/enums.py | 33 +++++++++++++++ .../13082023 ДЗ по фигурам JSON/main.py | 42 +++++++++---------- 6 files changed, 55 insertions(+), 26 deletions(-) delete mode 100644 HNS/Excercises/13082023 ДЗ по фигурам JSON/Shapechange.json create mode 100644 HNS/Excercises/13082023 ДЗ по фигурам JSON/enums.py diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/.idea/13082023 ДЗ по фигурам JSON.iml b/HNS/Excercises/13082023 ДЗ по фигурам JSON/.idea/13082023 ДЗ по фигурам JSON.iml index a293491..aee9ef2 100644 --- a/HNS/Excercises/13082023 ДЗ по фигурам JSON/.idea/13082023 ДЗ по фигурам JSON.iml +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/.idea/13082023 ДЗ по фигурам JSON.iml @@ -4,7 +4,7 @@ - + \ No newline at end of file diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/.idea/misc.xml b/HNS/Excercises/13082023 ДЗ по фигурам JSON/.idea/misc.xml index 0f77601..a971a2c 100644 --- a/HNS/Excercises/13082023 ДЗ по фигурам JSON/.idea/misc.xml +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/.idea/misc.xml @@ -1,4 +1,4 @@ - + \ No newline at end of file diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/Shape change.json b/HNS/Excercises/13082023 ДЗ по фигурам JSON/Shape change.json index 2adc9f7..2cc5485 100644 --- a/HNS/Excercises/13082023 ДЗ по фигурам JSON/Shape change.json +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/Shape change.json @@ -1 +1 @@ -{"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, {"side": 0, "color": "unknown", "type": "Square"}]} \ No newline at end of file +{"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, null]} \ No newline at end of file diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/Shapechange.json b/HNS/Excercises/13082023 ДЗ по фигурам JSON/Shapechange.json deleted file mode 100644 index e69de29..0000000 diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/enums.py b/HNS/Excercises/13082023 ДЗ по фигурам JSON/enums.py new file mode 100644 index 0000000..3daf2ab --- /dev/null +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/enums.py @@ -0,0 +1,33 @@ +from enum import Enum + + +class ShapeType(Enum): + Rectangle = 'rectangle' + Circle = 'circle' + Square = 'square' + Unknown = 'unknown' + + @staticmethod + def from_string(raw_value): + if raw_value: + value = raw_value.lower().capitalize() + if value in ShapeType.__members__: + return ShapeType.value + return ShapeType.Unknown + + +class ShapeColor(Enum): + Red = 'red' + Yellow = 'yellow' + Blue = 'blue' + Green = 'green' + Black = 'black' + Unknown = 'unknown' + + @staticmethod + def from_string(raw_value): + if raw_value: + value = raw_value.lower().capitalize() + if value in ShapeColor.__members__: + return ShapeColor.value + return ShapeColor.Unknown diff --git a/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py b/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py index 28afee8..656b396 100755 --- a/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py +++ b/HNS/Excercises/13082023 ДЗ по фигурам JSON/main.py @@ -2,6 +2,8 @@ import json import random from classes import Shape, Square, Circle, Rectangle +from enums import ShapeColor +from enums import ShapeType def safe_read(obj, property, default_value): @@ -12,43 +14,38 @@ def safe_read(obj, property, default_value): def create_shape(json): if 'type' in json: - shape_type = json['type'].lower() - if shape_type == 'circle': + shape_type_raw = json['type'] + shape_type = ShapeType.from_string(shape_type_raw) + if shape_type == ShapeType.Circle: radius = safe_read(json, 'radius', 0) obj = Circle(radius) - elif shape_type == 'square': + elif shape_type == ShapeType.Square: side = safe_read(json, 'side', 0) obj = Square(side) - elif shape_type == 'rectangle': + elif shape_type == ShapeType.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') - + obj.color = ShapeColor.from_string(safe_read(json, 'color', 'unknown')) return obj def generate_shape(): max_length = 100 - types = ['square', 'circle', 'rectangle'] - colors = ['red', 'green', 'blue', 'yellow', 'black', 'white'] - - type_index = random.randint(0, len(types) - 1) - shape_type = types[type_index] - - if shape_type == 'circle': + type_index = random.randint(0, len(ShapeType) - 1) + shape_type = ShapeType[type_index] + if shape_type == ShapeType.Circle: obj = Circle(random.randint(1, max_length)) - elif shape_type == 'square': + elif shape_type == ShapeType.Square: obj = Square(random.randint(1, max_length)) - elif shape_type == 'rectangle': + elif shape_type == ShapeType.Rectangle: obj = Rectangle(random.randint(1, 100), random.randint(1, max_length)) else: raise TypeError(f'Происходит что-то непонятное') - - color_index = random.randint(0, len(colors) - 1) - obj.color = colors[color_index] + color_index = random.randint(0, len(ShapeColor) - 1) + obj.color = ShapeColor[color_index] def json_to_python(filename): @@ -70,9 +67,8 @@ 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') +#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')