假日的python客户端库-api.de
ferien-api的Python项目详细描述
费里安API
Python client library for ferien-api.de
安装
ferien-api
需要python 3.5+才能正常工作
pip install ferien-api
用法
您可以使用同步实现,它将阻塞直到来自api的响应到达 或者您可以使用异步实现,它不会阻塞您的其他异步内容。
同步:
importferiendefmain():# Get all vacations for all time and statesprint("All vacations:",ferien.all_vacations())# Get all vacations for a specific state (in this case Hamburg - HH) ...print("All vacations for HH:",ferien.state_vacations('HH'))# ... and optionally for a specific yearprint("All vacations for HH in 2019:",ferien.state_vacations('HH',2019))# Fetch all valid statesprint("Valid state codes:",ferien.state_codes())# Get current vacation (None if there is no vacation)print("Current vacation in HH:",ferien.current_vacation('HH'))# Get next vacation (None if there is no next vacation)print("Next vacation in HH:",ferien.next_vacation('HH'))if__name__=='__main__':main()
异步:
importasyncioimportferienasyncdefmain():# Get all vacations for all time and statesprint("All vacations:",awaitferien.all_vacations_async())# Get all vacations for a specific state (in this case Hamburg - HH) ...print("All vacations for HH:",awaitferien.state_vacations_async('HH'))# ... and optionally for a specific yearprint("All vacations for HH in 2019:",awaitferien.state_vacations_async('HH',2019))# Fetch all valid states. This one is _NOT_ asyncprint("Valid state codes:",ferien.state_codes())# Get current vacation (None if there is no vacation)print("Current vacation in HH:",ferien.current_vacation(vacs=awaitferien.state_vacations_async('HH')))# Get next vacation (None if there is no next vacation)print("Next vacation in HH:",ferien.next_vacation(vacs=awaitferien.state_vacations_async('HH')))if__name__=='__main__':loop=asyncio.get_event_loop()loop.run_until_complete(main())
all_vacations
和state_vacations
的实现都将返回Vacation
数据对象的列表。
有关Vacation
对象的定义,请参见下文。
Vacation(start=datetime.datetime(2020,12,21,0,0),end=datetime.datetime(2021,1,5,0,0),year=2020,state_code='HH',name='weihnachtsferien',slug='weihnachtsferien-2020-HH')
请注意:所有datetime对象都在Europe/Berlin (CET/CEST)
时区中
使用异步版本很容易在“并行”中发出多个请求(不是真的……你知道当你 一个异步爱好者)并且节省了很多时间:
importasyncioimportferienasyncdefprint_wrapper(state_code):print("Fetching {}".format(state_code))res=awaitferien.state_vacations_async(state_code,2019)print("Fetched {}".format(state_code))returnresif__name__=='__main__':loop=asyncio.get_event_loop()coros=[print_wrapper('HH'),print_wrapper('SH'),print_wrapper('BE'),print_wrapper('BB')]loop.run_until_complete(asyncio.gather(*coros))
更改日志
0.3.4
- 修正了日期在欧洲/柏林的错误本地化
0.3.3
- 将所有时间戳从naive更改为“europe/berlin”
0.3.2
- 向代码库添加类型提示
- 将mypy添加为短绒
0.3.1
- 加上皮林作为一根短绒,让他高兴!
0.3.0
- 添加
current_vacation
和next_vacation
实现
0.2.0
- 添加
all_vacations
和state_vacations
的异步实现
0.1.0
- 初始版本