q-ctrl python api客户端
qctrl-api-client的Python项目详细描述
q-ctrl python api客户端
qctrlpythonapi客户端为q ctrl的 restful http api。
目录
安装
pip install qctrl-api-client
用法
设置
fromqctrlapiclientimportApiapi=Api("YOUR_API_KEY")
检索数据
Api
类附带一些函数,这些函数将帮助您查找数据和
过滤它。最常用的方法是find()
。
查找
find(type,options=None)
find是所有数据检索功能的多功能工作台。"type"
可以是"all"
或"first"
。
options
用于将所有选项传递给各种类型的find()
,并且
以下可能的键(每个键都是可选的):
options={# Find resources based on a list of filter conditions"filter":{"field_1":"value_1","field_2":"value_2",},# Improve performance by including related resources at the same time"include":["related_resource_1","related_resource_2",],# Sort the results by a list of fields# By default, results are sorted in ascending order.# To sort in descending order, prefix fields with a minus (-)."sort":["field_1","-field_2"]
查找(“第一个”)
find("first", options={})
将返回一个结果。不管怎样你都会用这个
只有一个结果。
使用find('first')的示例
# Find the first systemsystem=api.systems.find("first")# Find the first control with a `type` equal to "shift" and# include its related system and pulsecontrol=api.controls.find("first",options={"filter":{"type":"shift",},"include":["systems","pulses",],})
查找(“全部”)
find("all", options={})
返回可能包含多个结果的字典。
使用find('all')
的示例# Find all systemssystems=api.systems.find("all")# Find all systems and include their related controlssystems_including_controls=api.systems.find("all",options={"include":["controls",],})
魔法查找类型
这些神奇的功能可以作为一个快捷方式,通过
字段。只需将字段的名称(以snake_case
格式)添加到
这些函数提供该字段的条件作为第一个选项。
find_all_by()
函数将以类似find("all")
的格式返回结果,
而find_by()
返回的格式与find("first")
相同。
通过
查找所有find_all_by_<field_name>(value,filter=None,include=None)
使用“全部查找”的示例
# Find all systems with an `id` equal to 7 (there will only be one)systems=api.systems.find_all_by_id(7)# Find all pulses with a `segment_count` equal to 10pulses=api.pulses.find_all_by_segment_count(10)# Find all controls with a `type` equal to "drift" and include their related systemcontrols=api.controls.find_all_by_type("drift",include=["systems"])
通过
查找find_by_<field_name>(value,filter=None,include=None)
使用find_by的示例
# Find the system with the `id` equal to 7systems=api.systems.find_by_id(7)# Find the first pulse with a `segment_count` equal to 10pulses=api.pulses.find_by_segment_count(10)# Find the first control with a `type` equal to "drift" and# include its related systemcontrols=api.controls.find_all_by_type("drift",include=["systems"])
保存数据
准备保存的数据应传递给resourcescommit()
方法。
保存数据的示例
# Save a new system with a `name` of "Master System"system=api.systems.new()system.name="Master System"system.commit()
刷新数据
每个资源都有一个refresh()
方法,可用于接收任何更新的
来自api的属性。
刷新数据的示例
# Refresh a system that has been updatedprint(system.name)# "My System"system.refresh()print(system.name)# "My Updated System"
删除数据
准备删除的资源应传递给其delete()
和commit()
。
方法。这也将删除所有相关资源。
删除数据的示例
# Delete the system with the `id` of 7 and all related resourcessystem=api.systems.find_by_id(7)system.delete()system.commit()
贡献
见 Contributing。
测试
我们使用pytest进行单元测试。要运行单元测试,请使用以下命令:
py.test
学分
见 Contributors。
许可证
见LICENSE。