BC Ferries Python库
bcferries的Python项目详细描述
这是用于与信息交互的python客户机库 发表于BC Ferries mobile site。它本质上是一个包装器 围绕着一个漂亮的电动铲运机。更好的文档和测试 仍在工作中;请随意贡献!
这个库用于像 FerryTime。此库的源代码可以 在上的yasyf/bcferries找到 github。
安装
pip install bcferries
设置
有些功能需要与地理编码服务交互;google maps api用于此。为了防止严重的限速, 您需要获取一个api密钥。让bcferries知道这件事 键,将其设置为GOOGLE_MAPS_API_KEY环境变量。 或者,可以将其作为可选关键字参数传递给 构造函数。
frombcferriesimportBCFerriesbc=BCFerries(google_maps_api_key='xxx-xxx-xxx')
用法
bc=BCFerries()
终端
bc.nearest_terminal('Qualicum Beach')# BCFerriesTerminal(Nanaimo (Duke Pt))terminals=bc.terminals()# {u'Horseshoe Bay': BCFerriesTerminal(Horseshoe Bay), u'Tsawwassen': BCFerriesTerminal(Tsawwassen)}t=terminals['Tsawwassen']# BCFerriesTerminal(Tsawwassen)t.updated_at()# datetime.datetime(2014, 12, 29, 0, 4)t.next_crossing()# BCFerriesCrossing(Tsawwassen to Duke Point at 5:15am)t.location().address# u'Ferry Causeway, Delta, BC V4M, Canada'
路线
routes=t.routes()# {u'Tsawwassen to Duke Point': BCFerriesRoute(Tsawwassen to Duke Point)}r=routes['Tsawwassen to Duke Point']# BCFerriesRoute(Tsawwassen to Duke Point)r.from_,r.to# (BCFerriesTerminal(Tsawwassen), BCFerriesTerminal(Nanaimo (Duke Pt)))r.distance()# Distance(61.9591068557)r.car_waits# 0
交叉口
crossing=r.crossings()['10:45pm']# BCFerriesCrossing(Tsawwassen to Duke Point at 5:45pm)crossing.capacity# BCFerriesCapacity(18% Full)
时间表
schedule=r.scheduled('12:45 PM')# BCFerriesScheduledCrossing(Queen of Alberni at 12:45 PM)schedule.status# u'On Time'schedule.sailing_time# datetime.timedelta(0, 7200)schedule.is_late()# Falseschedule.is_departed()# True
模糊结果
所有返回的词典的键上都有模糊字符串匹配。
routes['Tsawwassen to Duke Point']==routes['Tsaw to DP']# True
在表示附近时间的键上也存在模糊时间匹配。
r=routes['HBay to DBay']schedule=r.schedule()schedule['6:12 PM']# BCFerriesScheduledCrossing(Queen of Cowichan at 6:30 PM)
datetime对象也可以用作键。
fromdatetimeimportdatetimedatetime.datetime.now()# datetime.datetime(2014, 12, 28, 10, 42, 35, 630996)schedule[datetime.datetime.now()]# BCFerriesScheduledCrossing(Coastal Renaissance at 10:40 AM)
缓存
bcferries缓存16个最常用的api调用,最多5分钟 默认情况下。您可以如下更改此行为。必须这样做 在创建BCFerries对象之前。
importbcferriesimportdatetimebcferries.set_cache_size(16)bcferries.set_cache_timeout(datetime.timedelta(minutes=5))
也可以将ignore_cache关键字参数传递给任何函数 绕过缓存,或对BCFerries调用flush_cache方法 清除整个缓存。
terminals=bc.terminals()# initial call takes multiple secondsterminals=bc.terminals()# repeated call returns almost instantlyterminals=bc.terminals(ignore_cache=True)# takes multiple seconds to returnbc.flush_cache()# wipes the cache
导出
您可以通过调用to_dicton导出任何信息子集 任何物体。也可以使用to_fuzzy_dict和to_json作为 需要。
默认情况下,需要进一步api调用的复杂对象将不会 创建,只返回其名称。你可以禁用这个 使用shallow关键字参数的行为。导出所有可用的 信息,在BCFerries实例上执行此操作,并准备 等等。
crossing.capacity# BCFerriesCapacity(18% Full)crossing.capacity.to_dict()# {'passenger_filled': 32, 'mixed_filled': 4, 'name': '18% Full', 'filled': 18}bc.to_dict()# quickbc.to_dict(shallow=False)# takes all day