Skip to content

Instantly share code, notes, and snippets.

@anandology
Created October 24, 2009 11:09
Show Gist options
  • Save anandology/217479 to your computer and use it in GitHub Desktop.
Save anandology/217479 to your computer and use it in GitHub Desktop.
"""Utility to iterate over couchdb view result."""
import urllib
import simplejson
class Result:
def __init__(self, total_rows, offset, data):
self.total_rows = total_rows
self.offset = offset
self._data = data
def __iter__(self):
return iter(self._data)
def couchview(url):
result = urllib.urlopen(url)
head = result.readline() + ']}'
head = simplejson.loads(head)
def parse():
while True:
row = result.readline().rstrip().rstrip(',')
if row and row != ']}':
yield simplejson.loads(row)
else:
break
return Result(head['total_rows'], head['offset'], parse())
def main(url):
result = couchview(url)
print result.total_rows, result.offset
for row in result:
print row
if __name__ == "__main__":
import sys
main(sys.argv[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment