库有一个上下文管理器来简化文档的处理:
当输入文档上下文时,从# remote数据库获取文档(如果存在的话)。退出上下文后,使用上下文中的更改将#文档保存到远程数据库。以文档(数据库,'julia006')为文档:#从远程数据库获取文档#更改在本地进行文档‘’name‘= 'Julia’文档‘are’=6#文档保存到远程数据库源:http://python-cloudant.readthedocs.io/en/latest/document.html
如果远程文档不存在,行为是什么?是将文档设置为None,还是引发异常?
发布于 2017-01-13 08:09:26
如您所见,如果文档不存在,则将在调用fetch()时引发异常。但是它将在But块中处理。如果错误代码不是404,则会重新引发异常。因此,对于404以外的所有错误代码,您将得到一个异常。
def __enter__(self):
"""
Supports context like editing of document fields. Handles context
entry logic. Executes a Document.fetch() upon entry.
"""
# We don't want to raise an exception if the document is not found
# because upon __exit__ the save() call will create the document
# if necessary.
try:
self.fetch()
except HTTPError as error:
if error.response.status_code != 404:
raise
return self发布于 2017-01-13 08:25:29
如果远程数据库中不存在文档,则将在远程数据库中为您创建文档。
https://stackoverflow.com/questions/41629863
复制相似问题