загружаю итоговый вариант дз
This commit is contained in:
parent
09336926c5
commit
bca8f3814e
|
@ -1 +1 @@
|
||||||
{"shapes": [{"width": 5, "height": 10, "type": "Rectangle"}, {"radius": 7, "type": "Circle"}, {"side": 4, "type": "Square"}]}
|
{"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, {"side": 0, "color": "unknown", "type": "Square"}]}
|
|
@ -20,10 +20,9 @@ class Shape(ABC):
|
||||||
if "type" in json:
|
if "type" in json:
|
||||||
shape_type = json["type"].lower()
|
shape_type = json["type"].lower()
|
||||||
if shape_type == "circle":
|
if shape_type == "circle":
|
||||||
radius =
|
radius = 'radius'
|
||||||
return Circle(json["radius"])
|
|
||||||
elif shape_type == "square":
|
elif shape_type == "square":
|
||||||
return Square(json["side"])
|
side = 'side'
|
||||||
elif shape_type == "rectangle":
|
elif shape_type == "rectangle":
|
||||||
return Rectangle(json["width"], json["height"])
|
return Rectangle(json["width"], json["height"])
|
||||||
else:
|
else:
|
||||||
|
@ -33,10 +32,10 @@ class Shape(ABC):
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def convert_to_json(obj):
|
def convert_to_json(obj):
|
||||||
global result
|
|
||||||
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
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def safe_read (shape_type, property, default_value):
|
def safe_read (shape_type, property, default_value):
|
||||||
|
|
|
@ -1,29 +1,73 @@
|
||||||
import json
|
import json
|
||||||
|
import random
|
||||||
|
|
||||||
from classes import Shape, Square, Circle, Rectangle
|
from classes import Shape, Square, Circle, Rectangle
|
||||||
|
|
||||||
|
|
||||||
def json_to_python():
|
def safe_read(obj, property, default_value):
|
||||||
|
if property in obj:
|
||||||
|
return obj[property]
|
||||||
|
return default_value
|
||||||
|
|
||||||
|
|
||||||
|
def create_shape(json):
|
||||||
|
if 'type' in json:
|
||||||
|
shape_type = json['type'].lower()
|
||||||
|
if shape_type == 'circle':
|
||||||
|
radius = safe_read(json, 'radius', 0)
|
||||||
|
obj = Circle(radius)
|
||||||
|
elif shape_type == 'square':
|
||||||
|
side = safe_read(json, 'side', 0)
|
||||||
|
obj = Square(side)
|
||||||
|
elif shape_type == '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')
|
||||||
|
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
def generate_shape():
|
||||||
|
types = ['square', 'circle', 'rectangle']
|
||||||
|
rnd = random.randint(0, len(types) - 1)
|
||||||
|
shape_type = types[rnd]
|
||||||
|
if shape_type == 'circle':
|
||||||
|
obj = Circle(rnd)
|
||||||
|
elif shape_type == 'square':
|
||||||
|
obj = Square(rnd)
|
||||||
|
elif shape_type == 'rectangle':
|
||||||
|
obj = Rectangle(rnd, rnd)
|
||||||
|
else:
|
||||||
|
raise TypeError(f'Происходит что-то непонятное')
|
||||||
|
obj.color = 'unknown'
|
||||||
|
return obj
|
||||||
|
|
||||||
|
|
||||||
|
def json_to_python(filename):
|
||||||
data_list = []
|
data_list = []
|
||||||
with open("shapes.json") as lines:
|
with open(filename) as lines:
|
||||||
data = json.load(lines)
|
data = json.load(lines)
|
||||||
for i in data["shapes"]:
|
for i in data["shapes"]:
|
||||||
print()
|
sh1 = create_shape(i)
|
||||||
print("Type:", i["type"])
|
|
||||||
print("Color:", i["color"])
|
|
||||||
sh1 = Shape.create_shape(i)
|
|
||||||
print(f'area = {sh1.area()}')
|
|
||||||
print(f'perimetr = {sh1.perimetr()}')
|
|
||||||
data_list.append(sh1)
|
data_list.append(sh1)
|
||||||
for j in data_list:
|
return data_list
|
||||||
print(j.convert_to_json(j))
|
|
||||||
|
|
||||||
data_list2 = ("{\"shapes\": " + json.dumps(data_list, default=Shape.convert_to_json) + "}")
|
|
||||||
print(data_list2)
|
|
||||||
|
|
||||||
with open('Shape change.json', 'w') as f:
|
|
||||||
f.write(data_list2)
|
|
||||||
|
|
||||||
|
|
||||||
|
def python_to_json(data, filename):
|
||||||
|
with open(filename, 'w') as f:
|
||||||
|
json.dump({'shapes': data}, f, default=Shape.convert_to_json)
|
||||||
|
|
||||||
|
|
||||||
json_to_python()
|
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')
|
||||||
|
|
||||||
|
|
|
@ -1,20 +1 @@
|
||||||
{
|
{"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, {"width": 2, "height": 2, "color": "unknown", "type": "Rectangle"}, {"side": 0, "color": "unknown", "type": "Square"}]}
|
||||||
"shapes": [
|
|
||||||
{
|
|
||||||
"type": "rectangle",
|
|
||||||
"width": 5,
|
|
||||||
"height": 10,
|
|
||||||
"color": "red"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "circle",
|
|
||||||
"radius": 7,
|
|
||||||
"color": "blue"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"type": "square",
|
|
||||||
"side": 4,
|
|
||||||
"color": "green"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
Loading…
Reference in New Issue