python cloudant库有一个上下文管理器来简化对文档的处理:
# Upon entry into the document context, fetches the document from the # remote database, if it exists. Upon exit from the context, saves the # document to the remote database with changes made within the context. with Document(database, 'julia006') as document: # The document is fetched from the remote database # Changes are made locally document['name'] = 'Julia' document['age'] = 6 # The document is saved to the remote database
Source: http://python-cloudant.readthedocs.io/en/latest/document.html
如果远程文档不存在,该行为是什么?文档是否设置为None
,或者是否引发异常?你知道吗
如您所见,如果文档不存在,则在调用
fetch()
时会引发异常。但它将在除块中处理。如果错误代码不是404,则将重新引发异常。所以对于404以外的所有错误代码,您将得到一个异常。你知道吗如果远程数据库中不存在该文档,则会在远程数据库中为您创建该文档。你知道吗
相关问题 更多 >
编程相关推荐