当前位置: 首页>>代码示例>>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;未经允许,请勿转载。