Created
October 24, 2009 11:09
-
-
Save anandology/217479 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""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