Are there ways to avoid this triply nested for-loop?
def add_random_fields():
from numpy.random import rand
server = couchdb.Server()
databases = [database for database in server if not database.startswith('_')]
for database in databases:
for document in couchdb_pager(server[database]):
if 'results' in server[database][document]:
for tweet in server[database][document]['results']:
if tweet and 'rand_num' not in tweet:
print document
tweet['rand_num'] = rand()
server[database].save(tweet)
Update
People suggesting that I use a generator reminded me that I forgot to mention that couchdb_pager
is a generator over the list server[database]
.
documents = (document for database in databases for document in couchdb_pager(server[database]) if 'results' in document)
then loop through the documents. \$\endgroup\$