2023-08-18 19:27:51 +02:00
|
|
|
|
import json
|
2023-09-25 14:39:15 +02:00
|
|
|
|
import random
|
|
|
|
|
|
2023-09-03 10:53:21 +02:00
|
|
|
|
from classes import Shape, Square, Circle, Rectangle
|
|
|
|
|
|
2023-08-18 19:27:51 +02:00
|
|
|
|
|
2023-09-25 14:39:15 +02:00
|
|
|
|
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():
|
2023-10-01 22:22:53 +02:00
|
|
|
|
max_length = 100
|
2023-09-25 14:39:15 +02:00
|
|
|
|
types = ['square', 'circle', 'rectangle']
|
2023-10-01 22:22:53 +02:00
|
|
|
|
colors = ['red', 'green', 'blue', 'yellow', 'black', 'white']
|
|
|
|
|
|
|
|
|
|
type_index = random.randint(0, len(types) - 1)
|
|
|
|
|
shape_type = types[type_index]
|
|
|
|
|
|
2023-09-25 14:39:15 +02:00
|
|
|
|
if shape_type == 'circle':
|
2023-10-01 22:22:53 +02:00
|
|
|
|
obj = Circle(random.randint(1, max_length))
|
2023-09-25 14:39:15 +02:00
|
|
|
|
elif shape_type == 'square':
|
2023-10-01 22:22:53 +02:00
|
|
|
|
obj = Square(random.randint(1, max_length))
|
2023-09-25 14:39:15 +02:00
|
|
|
|
elif shape_type == 'rectangle':
|
2023-10-01 22:22:53 +02:00
|
|
|
|
obj = Rectangle(random.randint(1, 100), random.randint(1, max_length))
|
2023-09-25 14:39:15 +02:00
|
|
|
|
else:
|
|
|
|
|
raise TypeError(f'Происходит что-то непонятное')
|
2023-10-01 22:22:53 +02:00
|
|
|
|
|
|
|
|
|
color_index = random.randint(0, len(colors) - 1)
|
|
|
|
|
obj.color = colors[color_index]
|
2023-09-25 14:39:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def json_to_python(filename):
|
2023-09-03 10:53:21 +02:00
|
|
|
|
data_list = []
|
2023-09-25 14:39:15 +02:00
|
|
|
|
with open(filename) as lines:
|
2023-08-21 18:54:05 +02:00
|
|
|
|
data = json.load(lines)
|
2023-08-28 08:00:52 +02:00
|
|
|
|
for i in data["shapes"]:
|
2023-09-25 14:39:15 +02:00
|
|
|
|
sh1 = create_shape(i)
|
2023-09-03 10:53:21 +02:00
|
|
|
|
data_list.append(sh1)
|
2023-09-25 14:39:15 +02:00
|
|
|
|
return data_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def python_to_json(data, filename):
|
|
|
|
|
with open(filename, 'w') as f:
|
|
|
|
|
json.dump({'shapes': data}, f, default=Shape.convert_to_json)
|
2023-08-18 19:27:51 +02:00
|
|
|
|
|
|
|
|
|
|
2023-09-25 14:39:15 +02:00
|
|
|
|
def filter_shapes(data, area):
|
|
|
|
|
return [shape for shape in data if shape.area() >= area]
|
2023-09-18 21:20:56 +02:00
|
|
|
|
|
|
|
|
|
|
2023-09-25 14:39:15 +02:00
|
|
|
|
shape_list = json_to_python('shapes.json')
|
|
|
|
|
shape_list = filter_shapes(shape_list, 100)
|
|
|
|
|
shape_list.append(generate_shape())
|
2023-09-18 21:20:56 +02:00
|
|
|
|
|
2023-09-25 14:39:15 +02:00
|
|
|
|
python_to_json(shape_list, 'shape change.json')
|
2023-09-18 21:20:56 +02:00
|
|
|
|
|