Fix some bugs
This commit is contained in:
parent
722f59a1d3
commit
eded5df712
|
@ -1,11 +1,12 @@
|
||||||
import json
|
import json
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from abc import ABC
|
from abc import ABC
|
||||||
|
from enums import ShapeColor
|
||||||
import math
|
import math
|
||||||
|
|
||||||
|
|
||||||
class Shape(ABC):
|
class Shape(ABC):
|
||||||
color = ''
|
color = ShapeColor.Unknown
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def area(self):
|
def area(self):
|
||||||
|
@ -15,28 +16,14 @@ class Shape(ABC):
|
||||||
def perimetr(self):
|
def perimetr(self):
|
||||||
pass
|
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
|
@staticmethod
|
||||||
def convert_to_json(obj):
|
def convert_to_json(obj):
|
||||||
if isinstance(obj, Shape):
|
if isinstance(obj, Shape):
|
||||||
result = obj.__dict__
|
result = obj.__dict__
|
||||||
result["type"] = obj.__class__.__name__
|
result["type"] = obj.__class__.__name__
|
||||||
result['color'] = obj.color
|
result['color'] = obj.color.value
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def safe_read (shape_type, property, default_value):
|
def safe_read (shape_type, property, default_value):
|
||||||
if property in shape_type:
|
if property in shape_type:
|
||||||
|
|
|
@ -12,7 +12,7 @@ class ShapeType(Enum):
|
||||||
if raw_value:
|
if raw_value:
|
||||||
value = raw_value.lower().capitalize()
|
value = raw_value.lower().capitalize()
|
||||||
if value in ShapeType.__members__:
|
if value in ShapeType.__members__:
|
||||||
return ShapeType.value
|
return ShapeType[value]
|
||||||
return ShapeType.Unknown
|
return ShapeType.Unknown
|
||||||
|
|
||||||
|
|
||||||
|
@ -28,6 +28,6 @@ class ShapeColor(Enum):
|
||||||
def from_string(raw_value):
|
def from_string(raw_value):
|
||||||
if raw_value:
|
if raw_value:
|
||||||
value = raw_value.lower().capitalize()
|
value = raw_value.lower().capitalize()
|
||||||
if value in ShapeColor.__members__:
|
if value in ShapeColor.__members__:
|
||||||
return ShapeColor.value
|
return ShapeColor[value]
|
||||||
return ShapeColor.Unknown
|
return ShapeColor.Unknown
|
||||||
|
|
|
@ -34,8 +34,9 @@ def create_shape(json):
|
||||||
|
|
||||||
def generate_shape():
|
def generate_shape():
|
||||||
max_length = 100
|
max_length = 100
|
||||||
type_index = random.randint(0, len(ShapeType) - 1)
|
type_index = random.randint(0, len(list(ShapeType)) - 2)
|
||||||
shape_type = ShapeType[type_index]
|
shape_type = list(ShapeType)[type_index]
|
||||||
|
|
||||||
if shape_type == ShapeType.Circle:
|
if shape_type == ShapeType.Circle:
|
||||||
obj = Circle(random.randint(1, max_length))
|
obj = Circle(random.randint(1, max_length))
|
||||||
elif shape_type == ShapeType.Square:
|
elif shape_type == ShapeType.Square:
|
||||||
|
@ -44,8 +45,10 @@ def generate_shape():
|
||||||
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(list(ShapeColor)) - 1)
|
||||||
obj.color = ShapeColor[color_index]
|
obj.color = list(ShapeColor)[color_index]
|
||||||
|
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
def json_to_python(filename):
|
def json_to_python(filename):
|
||||||
|
@ -67,8 +70,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')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue