pymongo and attribute dictionaries

Gregory Ewing greg.ewing at canterbury.ac.nz
Wed Feb 4 15:52:31 EST 2015


Travis Griggs wrote:
> for doc in client.db.radios.find({’_id': {’$regex’: ‘^[ABC]'}}): pprint(doc)
> 
> changes to
> 
> for doc in ((Doc(d) for d in client.db.radios.find({’_id': {’$regex’:
> ‘^[ABC]'}})): pprint(doc)
> 
> Are there other approaches? Feel free to impress me with evil abuses in the
> interest of academic enrichment...

You could encapsulate some of that in a helper function such as

def docs(source):
    for d in source:
       yield Doc(d)

then your example becomes

for doc in docs(client.db.radios.find({’_id': {’$regex’: ‘^[ABC]'}})):
    pprint(doc)

-- 
Greg



More information about the Python-list mailing list