I am inserting data from a dict type object into three different tables in the SQL database. I am using Sqlalchemy as my ORM. My code is working but I think it is not very readable, is it any way to make it more readable or maybe simpler? I am using a lot of try except because I don't want my code to fail in the middle and also because all the three tables are related to each other.
My code is below:
def upload_fpb_data(data, project_id):
fpb_count = len(data)
fpb_added_count = 0
fpb_children_count = sum(map(lambda x : x['count'], data))
fpb_children_added_count = 0
fpb_children_base_color_count = sum([sum(map(lambda x: x['count'], i['children'])) for i in data])
fpb_children_base_color_added_count = 0
for parent in data:
parent_data = parent['data']
add_fpb = FPB(stockCode=parent_data[0], description=parent_data[1],
longDescription=parent_data[2], altKey=parent_data[3],
project=project_id)
db.session.add(add_fpb)
try:
db.session.commit()
fpb_added_count += 1
for child1 in parent['children']:
child1_data = child1['data']
add_fpb_children = FpbChildrenAssociation(ParentPart=child1_data['ParentPart'],
Component=child1_data['Component'], qty=child1_data['ComponentQtyPer'])
db.session.add(add_fpb_children)
try:
db.session.commit()
fpb_children_added_count+=1
fpb_children_association_id = add_fpb_children.id
for child2 in child1['children']['data']:
add_fpb_children_base_colors = FpbChildrenBaseColorAssociation(
fpbChildrenAssociationId=fpb_children_association_id,
baseColor=child2,
)
db.session.add(add_fpb_children_base_colors)
try:
db.session.commit()
fpb_children_base_color_added_count +=1
except:
db.session.rollback()
except Exception as e:
db.session.rollback()
except Exception as e:
db.session.rollback()
return {'fpb':f"Rows Appended {fpb_added_count}/{fpb_count}",
'fpbChildrenAssociation':f"Rows Appended {fpb_children_added_count}/{fpb_children_count}",
'fpbChildrenBaseColorAssociation':f"Rows Appended {fpb_children_base_color_added_count}/{fpb_children_base_color_count}"}
if you want to see the data I am working with then here it is:
data = [{'table': 'fpb',
'data': ('XPB.20.001.07.200.01', 'SSLDX STERN BOX', '', '20SX200'),
'children': [{'table': 'fpbChildrenAssociation',
'data': ['XPB.20.001.07.200.01', 'DID-1828A-50-V1.20.001.00', 1.0],
'count': 3,
'children': {'table': 'fpbChildrenBaseColorAssociation',
'data': ['00', '60', '90']}},
{'table': 'fpbChildrenAssociation',
'data': ['XPB.20.001.07.200.01', 'RFH0136SLV', 1.0],
'count': 3,
'children': {'table': 'fpbChildrenBaseColorAssociation',
'data': ['00', '60', '90']}},
{'table': 'fpbChildrenAssociation',
'data': ['XPB.20.001.07.200.01', 'RFN0019', 4.0],
'count': 3,
'children': {'table': 'fpbChildrenBaseColorAssociation',
'data': ['00', '60', '90']}},
{'table': 'fpbChildrenAssociation',
'data': ['XPB.20.001.07.200.01', 'RFN0036', 4.0],
'count': 3,
'children': {'table': 'fpbChildrenBaseColorAssociation',
'data': ['00', '60', '90']}},
{'table': 'fpbChildrenAssociation',
'data': ['XPB.20.001.07.200.01', 'RFN0043', 4.0],
'count': 3,
'children': {'table': 'fpbChildrenBaseColorAssociation',
'data': ['00', '60', '90']}},
{'table': 'fpbChildrenAssociation',
'data': ['XPB.20.001.07.200.01', 'RFN0046B', 4.0],
'count': 3,
'children': {'table': 'fpbChildrenBaseColorAssociation',
'data': ['00', '60', '90']}},
{'table': 'fpbChildrenAssociation',
'data': ['XPB.20.001.07.200.01', 'RFN0091', 4.0],
'count': 3,
'children': {'table': 'fpbChildrenBaseColorAssociation',
'data': ['00', '60', '90']}},
{'table': 'fpbChildrenAssociation',
'data': ['XPB.20.001.07.200.01', 'RFN0126', 1.0],
'count': 3,
'children': {'table': 'fpbChildrenBaseColorAssociation',
'data': ['00', '60', '90']}},
{'table': 'fpbChildrenAssociation',
'data': ['XPB.20.001.07.200.01', 'RPP0134', 4.0],
'count': 3,
'children': {'table': 'fpbChildrenBaseColorAssociation',
'data': ['00', '60', '90']}},
{'table': 'fpbChildrenAssociation',
'data': ['XPB.20.001.07.200.01', 'X.001.07.44579', 1.0],
'count': 1,
'children': {'table': 'fpbChildrenBaseColorAssociation', 'data': ['60']}}],
'count': 10}]