Darksky的异步包装器
darksky-async的Python项目详细描述
Darksky-异步Python 3库
这个libary使用本机异步支持设计,比在run_in_executor
内运行请求更适合那些需要更好的并发支持的人。
由于这是一个基于异步的libary,我们不打算支持python 2。所需的最低python版本是3.5,you can download it here
用法
可以使用多种方法来使用这个库,包括直接调用helper函数forecast
。这不提供任何设置,但由于每次都需要在后台创建客户端,因此速度明显较慢。与其使用此函数,不如创建客户机并将其保存到变量中。
客户端示例:
importasyncioimportdarksky_asyncasyncdefmain():client=darksky_async.Client("DarkSky key goes here")weather=awaitclient.forecast(37.8267,# latitude-122.4233# longitude)print(weather.currently)print(weather.daily.data[0].timeLocal)# time-based items have a local time# version that allows you to convert between the two easilyloop=asyncio.get_event_loop()loop.run_until_complete(client.forecast(latitude,longitude))
快速示例:
importasyncioimportdarksky_asyncasyncdefexample_function():result=awaitdarksky_async.forecast(key,latitude,longitude,exclude=['minutely','hourly'],units='si')loop=asyncio.get_event_loop()loop.run_until_complete(example_function())
额外功能
datapoint的所有基于时间的条目都有一个本地版本。多亏了pytz,我们可以将日期时间转换为本地时间,以便更快、更方便地访问时间。
要访问此本地时间,只需将Local
附加到任何基于时间的数据点:
weather=awaitclient.forecast(37.8267,-122.4233)utc_time=weather.daily.data[0].sunriseTime>>>'2pm'# Abbreviated for simplicitylocal=weather.daily.data[0].sunriseTimeLocal>>>'6am'# -8 hours behind UTC, this time is accurate for Los Angeles, USA
这本诽谤书还记录了该付多少钱。运行预测时,您可以通过执行以下操作来检查当前到期金额:
weather=awaitclient.forecast(lat,lon)print(weather.total_due)
根据服务条款here,退还金额将以美元为单位。 请注意:这只是每日费用。我们无法跟踪actual由于Darksky拥有锁定信息而欠下的金额。