загружаю итоговый вариант дз

This commit is contained in:
ehermakov 2023-09-25 15:39:15 +03:00
parent 09336926c5
commit bca8f3814e
4 changed files with 66 additions and 42 deletions

View File

@ -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"}]}

View File

@ -20,10 +20,9 @@ class Shape(ABC):
if "type" in json:
shape_type = json["type"].lower()
if shape_type == "circle":
radius =
return Circle(json["radius"])
radius = 'radius'
elif shape_type == "square":
return Square(json["side"])
side = 'side'
elif shape_type == "rectangle":
return Rectangle(json["width"], json["height"])
else:
@ -33,10 +32,10 @@ class Shape(ABC):
@staticmethod
def convert_to_json(obj):
global result
if isinstance(obj, Shape):
result = obj.__dict__
result["type"] = obj.__class__.__name__
result['color'] = obj.color
return result
def safe_read (shape_type, property, default_value):

View File

@ -1,29 +1,73 @@
import json
import random
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 = []
with open("shapes.json") as lines:
with open(filename) as lines:
data = json.load(lines)
for i in data["shapes"]:
print()
print("Type:", i["type"])
print("Color:", i["color"])
sh1 = Shape.create_shape(i)
print(f'area = {sh1.area()}')
print(f'perimetr = {sh1.perimetr()}')
sh1 = create_shape(i)
data_list.append(sh1)
for j in 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)
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)
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')

View File

@ -1,20 +1 @@
{
"shapes": [
{
"type": "rectangle",
"width": 5,
"height": 10,
"color": "red"
},
{
"type": "circle",
"radius": 7,
"color": "blue"
},
{
"type": "square",
"side": 4,
"color": "green"
}
]
}
{"shapes": [{"radius": 7, "color": "blue", "type": "Circle"}, {"width": 2, "height": 2, "color": "unknown", "type": "Rectangle"}, {"side": 0, "color": "unknown", "type": "Square"}]}