當前位置: 首頁>>代碼示例>>Python>>正文


Python Box.read_short方法代碼示例

本文整理匯總了Python中box.Box.read_short方法的典型用法代碼示例。如果您正苦於以下問題:Python Box.read_short方法的具體用法?Python Box.read_short怎麽用?Python Box.read_short使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在box.Box的用法示例。


在下文中一共展示了Box.read_short方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: print_updates

# 需要導入模塊: from box import Box [as 別名]
# 或者: from box.Box import read_short [as 別名]
def print_updates(args):
    data = yield getPage("http://developer.mbta.com/lib/gtrtfs/Passages.pb")
    update = gtfs_realtime_pb2.FeedMessage()
    update.ParseFromString(data)

    trip_id_to_delays = defaultdict(list)
    trip_ids = set()
    for entity in update.entity:
        trip_id = str(entity.trip_update.trip.trip_id)
        trip_ids.add(trip_id)
        for stop_time_update in entity.trip_update.stop_time_update:
            # TODO: error-check for lack of arrival.delay
            tup = stop_time_update.stop_sequence, stop_time_update.arrival.delay
            trip_id_to_delays[trip_id].append(tup)
        if trip_id in trip_id_to_delays:
            trip_id_to_delays[trip_id] = sorted(trip_id_to_delays[trip_id], key=itemgetter(0))



    con = sqlite3.connect(args.db)
    cur = con.cursor()
    # injection risk here! SQLite can't handle this many parameters, though
    trip_ids_str = ", ".join(("'%s'" % escaped(x)) for x in trip_ids)

    query = ('SELECT stop_times.offset, arrivals.blob, '
             'trip_ids.route_id, trip_ids.trip_id, trip_stops.blob '
             'FROM trip_ids '
             'JOIN stop_times ON stop_times.trip_id = trip_ids.id AND trip_ids.trip_id IN (%s) '
             'JOIN arrivals ON arrivals.id = stop_times.arrival_id '
             'JOIN trip_stops ON trip_stops.id = stop_times.stop_list_id ') % trip_ids_str
    results = cur.execute(query)

    stop_results = defaultdict(list)

    for offset, arrivals_blob_str, route_id, trip_id, stop_list_blob_str in results:
        trip_id = str(trip_id)

        arrivals_blob = Box(arrivals_blob_str)
        arrivals_len = arrivals_blob.read_short()

        stop_list_blob = Box(stop_list_blob_str)
        stop_list_len = stop_list_blob.read_short()

        for i in xrange(arrivals_len):
            current_delay = 0
            stop_id = stop_list_blob.read_string()
            sequence_id = stop_list_blob.read_short()
            arrival_minutes = arrivals_blob.read_short()

            if trip_id in trip_id_to_delays[trip_id]:
                for stop_sequence, delay in trip_id_to_delays[trip_id]:
                    if stop_sequence > sequence_id:
                        break
                    current_delay = delay

            arrival_seconds = arrival_minutes * 60
            tup = (offset + arrival_seconds, route_id, current_delay, trip_id, sequence_id)
            stop_results[stop_id].append(tup)


    now = datetime.now()
    midnight = now.replace(hour=0, minute=0, second=0, microsecond=0)
    now_seconds = (now - midnight).seconds

    if args.stop_id in stop_results:
        lst = stop_results[args.stop_id]
        new_lst = [(seconds, route_id, delay, trip_id, sequence_id)
                   for (seconds, route_id, delay, trip_id, sequence_id) in lst
                   if (seconds + delay) > now_seconds]

        new_lst = sorted(new_lst, key=lambda x: x[0] + x[2])

        if len(new_lst) == 0:
            print("No arrivals for %s" % args.stop_id)
        else:
            for seconds, route_id, delay, trip_id, sequence_id in new_lst:
                print("Next arrival for stop %s (sequence %d) on"
                      " route %s is at %s with delay %d (total %s) on trip %s" % (args.stop_id,
                                                                       sequence_id,
                                                                       route_id,
                                                                       time_to_string(seconds),
                                                                       delay,
                                                                       time_to_string(seconds + delay),
                                                                       trip_id))
    else:
        print("%s is not in any of the trips specified by the SQL query" % args.stop_id)
開發者ID:noisecapella,項目名稱:gtfs-to-schedule,代碼行數:88,代碼來源:print_updates.py


注:本文中的box.Box.read_short方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。