假设的python库.is api
python-hypothesis的Python项目详细描述
这个包提供了到假设api的python绑定。
低级访问
api子模块是Hypothesis API的低级实现。
此模块的内容反映了api的其余性质:
- api调用是由函数进行的。
- 输入和输出数据是未解释的数据(例如,json字符串,而不是 解释json产生的对象)。
- 参数检查是最小的,当api 返回一个错误(如果服务器不返回200,则引发APIError)。
- 参数auth总是首先给出(除了root(),它 不接受授权)。auth当前可以是none或string 包含开发人员令牌。
示例:
>>> h_annot.api.read(None, '53LMZGVCEemN4zOvm3oFEQ') u'{"updated": "2019-04-22T21:09:23.352503+00:00", "group": "__world__", ...>>> h_annot.api.read(None, 'bogusannotationid') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "h_annot/api.py", line 39, in read raise APIError(r) h_annot.exceptions.APIError: API call returned 404 (Not Found): not_found
高级访问
注释类是注释的对象抽象。使用load()class方法从其id:
>>> annot = h_annot.Annotation.load('53LMZGVCEemN4zOvm3oFEQ') >>> annot.text u"I'm imagining!!!"
例外情况更为严重:
>>> h_annot.Annotation.load('somebogusannotid') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "h_annot/annotation.py", line 97, in load raise KeyError('annotation ID %s not found' % annot_id) KeyError: 'annotation ID somebogusannotid not found'
不应直接调用注释构造函数。
可以更新某些属性;为此,请使用h_annot.auth()上下文管理器设置身份验证令牌:
>>> with h_annot.auth(authentication_token): ... annot.text = 'new text'
批注没有固有的身份验证概念,因此以前声明批注身份验证的方式:
>>> annot = h_annot.Annotation.load('someannotationid', 'somedevelkey') >>> annot.text = 'new text'
已弃用。
通过tags属性访问和更改标记。此属性的行为类似于不区分大小写的集合(就像假设本身处理标记一样)。
>>> print annot.tags TagSet(objectives, interwebs)>>> for tag in annot.tags: ... print tag objectives interwebs>>> with h_annot.auth(authentication_token): ... annot.tags = ['all', 'new', 'tags'] ... annot.tags.add('and one more') ... annot.tags.remove('new')
不赞成通过Annotation.search()进行搜索。此搜索仅将api.search()的结果包装在批注构造函数中,因此api.search()现在应用于使用Annotation.search()的搜索。对于高级搜索界面,请使用h_annot.search()。它接受关键字参数uri、user、tags和text。注意,tags由and连接,text中的单独单词由or连接,这是Hypothesis search API的行为。h_annot.search()尊重由h_annot.auth()上下文管理器设置的身份验证。
h_annot.search()返回一个SearchResults实例。SearchResults实例正确响应len(),迭代将导致注释:
>>> results = h_annot.search() 800765 >>> len(results) >>> for annotation in results: ... print(annotation) ... break <Hypothesis annotation WcxuNG0CEemcl4_d0fJoaw>
SearchResults对象将根据需要加载来自假设的更多结果,但在每次后续查询之前都有一秒的延迟,以避免在完成类似于list(h_annot.search())的简单且看似无害的操作时快速重复命中假设。