download tasks with Enum
This commit is contained in:
parent
fe6cb7b8c1
commit
722f59a1d3
|
@ -4,7 +4,7 @@
|
|||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/venv" />
|
||||
</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" />
|
||||
</component>
|
||||
</module>
|
|
@ -1,4 +1,4 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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>
|
|
@ -1 +1 @@
|
|||
{"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, {"side": 0, "color": "unknown", "type": "Square"}]}
|
||||
{"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, null]}
|
|
@ -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
|
|
@ -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')
|
||||
|
||||
|
|
Loading…
Reference in New Issue