I have written this code where per entity in feed.entity, if that entity has a field named trip_update and it has a matching route_id then I must iterate through each trip_update.stop_time_update in order to make sure the proper metra transit (using stop_id comparison) is selected. Then, I calculate some data; then iterate through each trip in metraTripIds. Then run a basic if statement to make sure the proper location is returned (which isn't a big deal). I just feel like having 3 for statements within each other can be refactored to be performed faster too. I just don't see how in this situation, would love to hear what everyone else thinks!
for entity in feed.entity:
if (entity.HasField('trip_update')) and (str(entity.trip_update.trip.route_id) == metraRouteId):
for stoptime in entity.trip_update.stop_time_update:
if (str(stoptime.stop_id) == metraStopId):
delayedTime = int((int(stoptime.arrival.time) - currentTime) / 60)
tripID = str(entity.trip_update.trip.trip_id).split('_')[1][2:]
for metraTrip in metraTripIds:
if (entity.trip_update.trip.trip_id == metraTrip["trip_id"]):
if (metraTrip["trip_headsign"] == metraTripHeadsignInbound):
metraLocation = metraInboundView
else:
metraLocation = metraOutboundView
predictionArray.append({'time': delayedTime, 'location': metraLocation, 'transitNumber': tripID, 'key': _ignoreThis})
return predictionArray