So, I have a JSON file, with some elements in it. The end goal of the code is to generate 3 elements for each element in the JSON file, with some modified properties, but not all.
The way I went about this is to run a for loop and have 3 different parts of code block generating the required elements, and adding it to an empty list. But for some reason, I the elements I am generating aren't the ones being added to the final list.
Specifically, the "label" sub element gets messed up. Can someone please explain why's that happening?
Here's the code:
from math import sqrt
import json
import gc
with open("your-path here\\inclined-conveyors.json") as f:
conv_json = json.load(f)
#print("conv_json['conv'] object is: \n\n\n", conv_json['conv'])
modified_conv = list()
for item in conv_json["conv"]:
new_x_coordinate_delta = (item['height']) / (2*sqrt(3))
new_y_coordinate_delta = item['height'] / 6
addendum1 = item.copy()
addendum1['name'] = addendum1['name'][:6] + "A"
addendum1['displayName'] = addendum1['name']
new_x_coordinate = addendum1['x'] + new_x_coordinate_delta
new_y_coordinate = addendum1['y'] - new_y_coordinate_delta
addendum1['x'] = round(new_x_coordinate, 6)
addendum1['y'] = round(new_y_coordinate, 6)
addendum1['label']['x'] = round(new_x_coordinate, 6)
addendum1['label']['y'] = round(new_y_coordinate, 6)
addendum1['height'] = addendum1['height'] / 3
print("\n addendum1 is: ", addendum1)
empty_list = []
empty_list.append(addendum1)
modified_conv.extend(empty_list)
# modified_conv.append(addendum)
del addendum1
del empty_list
gc.collect()
# print("added item from x == 1")
# print("\n modified conv is: ", modified_conv)
addendum2 = item.copy()
addendum2['name'] = addendum2['name'][:6] + "B"
addendum2['displayName'] = addendum2['name']
addendum2['label']['x'] = addendum2['x']
addendum2['label']['y'] = addendum2['y']
addendum2['height'] = addendum2['height'] / 3
print("\n addendum2 is: ", addendum2)
empty_list = []
empty_list.append(addendum2)
modified_conv.extend(empty_list)
# modified_conv.append(addendum)
# modified_conv = modified_conv + addendum
del addendum2
del empty_list
gc.collect()
# print("added item from x == 2")
# print("\n modified conv is: ", modified_conv)
addendum3 = item.copy()
addendum3['name'] = addendum3['name'][:6] + "C"
addendum3['displayName'] = addendum3['name']
#addendum3['displayName'] = ""
new_x_coordinate = addendum3['x'] - new_x_coordinate_delta
new_y_coordinate = addendum3['y'] + new_y_coordinate_delta
addendum3['x'] = round(new_x_coordinate, 6)
addendum3['y'] = round(new_y_coordinate, 6)
addendum3['label']['x'] = round(new_x_coordinate, 6)
addendum3['label']['y'] = round(new_y_coordinate, 6)
addendum3['height'] = addendum3['height'] / 3
print("\n addendum3 is: ", addendum3)
empty_list = []
empty_list.append(addendum3)
modified_conv.extend(empty_list)
# modified_conv.append(addendum)
# modified_conv = modified_conv + addendum
del addendum3
del empty_list
gc.collect()
# print("added item from x == 3")
# print("\n modified conv is: ", modified_conv)
# modified_conv["conv"].append(item)
print("\n\n\n modified conv is: \n\n\n", modified_conv)
print("\n\n\ number of items in json is: ", len(conv_json['conv']))
print("\n\n number of items in modified conv is: ", len(modified_conv))
Here is a sample JSON:
{
"conv": [
{
"className": "ConveyorStraight",
"name": "P42700B",
"displayName": "P42700",
"type": "Straight",
"x": 1511,
"y": 2891.5,
"label": {
"x": 1511,
"y": 2891.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42600B",
"displayName": "P42600",
"type": "Straight",
"x": 1621,
"y": 2891.5,
"label": {
"x": 1621,
"y": 2891.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42500B",
"displayName": "P42500",
"type": "Straight",
"x": 1731,
"y": 2891.5,
"label": {
"x": 1731,
"y": 2891.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42400B",
"displayName": "P42400",
"type": "Straight",
"x": 1861,
"y": 2892.5,
"label": {
"x": 1861,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42300B",
"displayName": "P42300",
"type": "Straight",
"x": 1971,
"y": 2892.5,
"label": {
"x": 1971,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42200B",
"displayName": "P42200",
"type": "Straight",
"x": 2081,
"y": 2892.5,
"label": {
"x": 2081,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42100B",
"displayName": "P42100",
"type": "Straight",
"x": 2211,
"y": 2892.5,
"label": {
"x": 2211,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P42000B",
"displayName": "P42000",
"type": "Straight",
"x": 2321,
"y": 2892.5,
"label": {
"x": 2321,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41900B",
"displayName": "P41900",
"type": "Straight",
"x": 2431,
"y": 2892.5,
"label": {
"x": 2431,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41800B",
"displayName": "P41800",
"type": "Straight",
"x": 2561,
"y": 2892.5,
"label": {
"x": 2561,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41700B",
"displayName": "P41700",
"type": "Straight",
"x": 2671,
"y": 2892.5,
"label": {
"x": 2671,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41600B",
"displayName": "P41600",
"type": "Straight",
"x": 2781,
"y": 2892.5,
"label": {
"x": 2781,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41500B",
"displayName": "P41500",
"type": "Straight",
"x": 2911,
"y": 2892.5,
"label": {
"x": 2911,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41400B",
"displayName": "P41400",
"type": "Straight",
"x": 3021,
"y": 2892.5,
"label": {
"x": 3021,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41300B",
"displayName": "P41300",
"type": "Straight",
"x": 3131,
"y": 2892.5,
"label": {
"x": 3131,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41200B",
"displayName": "P41200",
"type": "Straight",
"x": 3261,
"y": 2892.5,
"label": {
"x": 3261,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
},
{
"className": "ConveyorStraight",
"name": "P41100B",
"displayName": "P41100",
"type": "Straight",
"x": 3371,
"y": 2892.5,
"label": {
"x": 3371,
"y": 2892.5,
"size": 20,
"rotation": 0
},
"height": 360,
"width": 50,
"rotation": 60,
"searchable": true,
"controlled": true
}
]
}