def get_train_positions(): trains = list(CurrentTrainPosition.select()) data = [] for i in trains: data.append(dict(near=_station_fmt(wmata.stations[i.station_near]), far=_station_fmt(wmata.stations[i.station_far]), fraction=i.fraction, line=i.line, dest=i.dest, ttf=i.time_to_far)) return jsonify(data=data)
trains = d ds = [] for train in trains: time = times[train.direction_dest][train.station] + train.time_int stations_on_line = train.line.stations if stations_on_line[0] != train.direction_dest: stations_on_line.reverse() try: stations = list(zip(stations_on_line, [times[train.direction_dest][station] for station in stations_on_line])) except: continue for station, time_from_end in stations: if time_from_end > time: station_far = station break else: continue station_near = stations[[i[0] for i in stations].index(station_far) - 1][0] fraction = 1 - ((times[train.direction_dest][station_far] - time) / times[station_near][station_far]) ds.append(dict(fraction=fraction, station_near=station_near.station_code, station_far=station_far.station_code, line=train.line.friendly_name, dest=train.destination.name, time_to_far=train.time_int)) print("{}% between {} and {}".format(round(fraction * 100, 1), station_near, station_far)) with db.atomic(): CurrentTrainPosition.delete() CurrentTrainPosition.insert_many(ds).execute() import pprint # pprint.pprint(times)