download tasks with Enum

This commit is contained in:
ehermakov 2023-10-02 23:40:21 +03:00
parent fe6cb7b8c1
commit 722f59a1d3
6 changed files with 55 additions and 26 deletions

View File

@ -4,7 +4,7 @@
<content url="file://$MODULE_DIR$"> <content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" /> <excludeFolder url="file://$MODULE_DIR$/venv" />
</content> </content>
<orderEntry type="jdk" jdkName="Python 3.12 (13082023 ДЗ по фигурам JSON)" jdkType="Python SDK" /> <orderEntry type="jdk" jdkName="Python 3.11" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
</component> </component>
</module> </module>

View File

@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (13082023 ДЗ по фигурам JSON)" project-jdk-type="Python SDK" /> <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11" project-jdk-type="Python SDK" />
</project> </project>

View File

@ -1 +1 @@
{"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, {"side": 0, "color": "unknown", "type": "Square"}]} {"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, null]}

View File

@ -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

View File

@ -2,6 +2,8 @@ import json
import random import random
from classes import Shape, Square, Circle, Rectangle from classes import Shape, Square, Circle, Rectangle
from enums import ShapeColor
from enums import ShapeType
def safe_read(obj, property, default_value): def safe_read(obj, property, default_value):
@ -12,43 +14,38 @@ def safe_read(obj, property, default_value):
def create_shape(json): def create_shape(json):
if 'type' in json: if 'type' in json:
shape_type = json['type'].lower() shape_type_raw = json['type']
if shape_type == 'circle': shape_type = ShapeType.from_string(shape_type_raw)
if shape_type == ShapeType.Circle:
radius = safe_read(json, 'radius', 0) radius = safe_read(json, 'radius', 0)
obj = Circle(radius) obj = Circle(radius)
elif shape_type == 'square': elif shape_type == ShapeType.Square:
side = safe_read(json, 'side', 0) side = safe_read(json, 'side', 0)
obj = Square(side) obj = Square(side)
elif shape_type == 'rectangle': elif shape_type == ShapeType.Rectangle:
width = safe_read(json, 'width', 0) width = safe_read(json, 'width', 0)
height = safe_read(json, 'height', 0) height = safe_read(json, 'height', 0)
obj = Rectangle(width, height) obj = Rectangle(width, height)
else: else:
raise TypeError(f'вот тебе >>>(оIo), а не фигура {shape_type}') 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 return obj
def generate_shape(): def generate_shape():
max_length = 100 max_length = 100
types = ['square', 'circle', 'rectangle'] type_index = random.randint(0, len(ShapeType) - 1)
colors = ['red', 'green', 'blue', 'yellow', 'black', 'white'] shape_type = ShapeType[type_index]
if shape_type == ShapeType.Circle:
type_index = random.randint(0, len(types) - 1)
shape_type = types[type_index]
if shape_type == 'circle':
obj = Circle(random.randint(1, max_length)) obj = Circle(random.randint(1, max_length))
elif shape_type == 'square': elif shape_type == ShapeType.Square:
obj = Square(random.randint(1, max_length)) 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)) obj = Rectangle(random.randint(1, 100), random.randint(1, max_length))
else: else:
raise TypeError(f'Происходит что-то непонятное') raise TypeError(f'Происходит что-то непонятное')
color_index = random.randint(0, len(ShapeColor) - 1)
color_index = random.randint(0, len(colors) - 1) obj.color = ShapeColor[color_index]
obj.color = colors[color_index]
def json_to_python(filename): def json_to_python(filename):
@ -70,9 +67,8 @@ def filter_shapes(data, area):
return [shape for shape in data if shape.area() >= area] return [shape for shape in data if shape.area() >= area]
shape_list = json_to_python('shapes.json') #shape_list = json_to_python('shapes.json')
shape_list = filter_shapes(shape_list, 100) #shape_list = filter_shapes(shape_list, 100)
shape_list.append(generate_shape()) #shape_list.append(generate_shape())
#python_to_json(shape_list, 'shape change.json')
python_to_json(shape_list, 'shape change.json')