我正在创建一个寻呼机,该寻呼机从python-couchdb的Apache CouchDB映射函数返回文档。这个生成器表达式运行良好,直到达到最大递归深度。如何才能改进以迭代,而不是递归?python-couchdb寻呼机触发递归深度限制
def page(db, view_name, limit, include_docs=True, **opts):
"""
`page` goes returns all documents of CouchDB map functions. It accepts
all options that `couchdb.Database.view` does, however `include_docs`
should be omitted, because this will interfere with things.
>>> import couchdb
>>> db = couchdb.Server()['database']
>>> for doc in page(db, '_all_docs', 100):
>>> doc
#etc etc
>>> del db['database']
Notes on implementation:
- `last_doc` is assigned on every loop, because there doesn't seem to
be an easy way to know if something is the last item in the iteration.
"""
last_doc = None
for row in db.view(view_name,
limit=limit+1,
include_docs=include_docs,
**opts):
last_doc = row.key, row.id
yield row.doc
if last_doc:
for doc in page(db, view_name, limit,
inc_docs=inc_docs,
startkey=last_doc[0],
startkey_docid=last_doc[1]):
yield doc
我看不懂这段代码。我不是PEP8鹦鹉的粉丝,但请至少使用* 4空格缩进。 – 2010-09-29 23:55:02
这并没有真正回答这个问题,但有用的说明是,您可以通过使用'sys.setrecursionlimit()' – 2010-09-30 00:15:48
来更改最大递归深度。感谢@Rafe,我知道,但是因为我返回了几十万行,我不想杀死电脑。 – 2010-09-30 00:26:04