Fix some bugs
This commit is contained in:
parent
722f59a1d3
commit
eded5df712
|
@ -1,11 +1,12 @@
|
|||
import json
|
||||
from abc import abstractmethod
|
||||
from abc import ABC
|
||||
from enums import ShapeColor
|
||||
import math
|
||||
|
||||
|
||||
class Shape(ABC):
|
||||
color = ''
|
||||
color = ShapeColor.Unknown
|
||||
|
||||
@abstractmethod
|
||||
def area(self):
|
||||
|
@ -15,28 +16,14 @@ class Shape(ABC):
|
|||
def perimetr(self):
|
||||
pass
|
||||
|
||||
@staticmethod
|
||||
def create_shape(json):
|
||||
if "type" in json:
|
||||
shape_type = json["type"].lower()
|
||||
if shape_type == "circle":
|
||||
radius = 'radius'
|
||||
elif shape_type == "square":
|
||||
side = 'side'
|
||||
elif shape_type == "rectangle":
|
||||
return Rectangle(json["width"], json["height"])
|
||||
else:
|
||||
raise TypeError(f'Неизвестная фигура {shape_type}')
|
||||
shape_type.color = safe_read(json, "color", "unknown")
|
||||
|
||||
|
||||
@staticmethod
|
||||
def convert_to_json(obj):
|
||||
if isinstance(obj, Shape):
|
||||
result = obj.__dict__
|
||||
result["type"] = obj.__class__.__name__
|
||||
result['color'] = obj.color
|
||||
return result
|
||||
result['color'] = obj.color.value
|
||||
return result
|
||||
|
||||
|
||||
def safe_read (shape_type, property, default_value):
|
||||
if property in shape_type:
|
||||
|
|
|
@ -12,7 +12,7 @@ class ShapeType(Enum):
|
|||
if raw_value:
|
||||
value = raw_value.lower().capitalize()
|
||||
if value in ShapeType.__members__:
|
||||
return ShapeType.value
|
||||
return ShapeType[value]
|
||||
return ShapeType.Unknown
|
||||
|
||||
|
||||
|
@ -28,6 +28,6 @@ class ShapeColor(Enum):
|
|||
def from_string(raw_value):
|
||||
if raw_value:
|
||||
value = raw_value.lower().capitalize()
|
||||
if value in ShapeColor.__members__:
|
||||
return ShapeColor.value
|
||||
if value in ShapeColor.__members__:
|
||||
return ShapeColor[value]
|
||||
return ShapeColor.Unknown
|
||||
|
|
|
@ -34,8 +34,9 @@ def create_shape(json):
|
|||
|
||||
def generate_shape():
|
||||
max_length = 100
|
||||
type_index = random.randint(0, len(ShapeType) - 1)
|
||||
shape_type = ShapeType[type_index]
|
||||
type_index = random.randint(0, len(list(ShapeType)) - 2)
|
||||
shape_type = list(ShapeType)[type_index]
|
||||
|
||||
if shape_type == ShapeType.Circle:
|
||||
obj = Circle(random.randint(1, max_length))
|
||||
elif shape_type == ShapeType.Square:
|
||||
|
@ -44,8 +45,10 @@ def generate_shape():
|
|||
obj = Rectangle(random.randint(1, 100), random.randint(1, max_length))
|
||||
else:
|
||||
raise TypeError(f'Происходит что-то непонятное')
|
||||
color_index = random.randint(0, len(ShapeColor) - 1)
|
||||
obj.color = ShapeColor[color_index]
|
||||
color_index = random.randint(0, len(list(ShapeColor)) - 1)
|
||||
obj.color = list(ShapeColor)[color_index]
|
||||
|
||||
return obj
|
||||
|
||||
|
||||
def json_to_python(filename):
|
||||
|
@ -67,8 +70,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