Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #85231 > unrolled thread

Re: pymongo and attribute dictionaries

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2015-02-04 13:54 -0700
Last post2015-02-04 21:28 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: pymongo and attribute dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-04 13:54 -0700
    Re: pymongo and attribute dictionaries Rob Gaddi <rgaddi@technologyhighland.invalid> - 2015-02-04 21:28 +0000

#85231 — Re: pymongo and attribute dictionaries

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-02-04 13:54 -0700
SubjectRe: pymongo and attribute dictionaries
Message-ID<mailman.18476.1423083310.18130.python-list@python.org>
On Wed, Feb 4, 2015 at 1:16 PM, Travis Griggs <travisgriggs@gmail.com> wrote:
> Yes, that is clever. So if you wanted to minimize the amount of typing you had to do at all of your pymongo API call sites, what strategy would you use to keep that relatively terse?
>
> Is the following the right approach to take?
>
> class Doc(object):
>     def __init__(self, target):
>         self.__dict__ = target
>
> and then something like
>
> 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...

I'd prefer map (or itertools.imap in Python 2) over the inline
generator in this case:

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

Or if you like, a utility function wrapping the same.

def docs(dicts):
    return map(Doc, dicts)

[toc] | [next] | [standalone]


#85235

FromRob Gaddi <rgaddi@technologyhighland.invalid>
Date2015-02-04 21:28 +0000
Message-ID<mau2ub$of7$1@dont-email.me>
In reply to#85231
On Wed, 04 Feb 2015 13:54:22 -0700, Ian Kelly wrote:

> I'd prefer map (or itertools.imap in Python 2) over the inline generator
> in this case:
> 
> for doc in map(Doc, client.db.radios.find({’_id': {’$regex’:
> ‘^[ABC]'}})):
>     pprint(doc)
> 
> Or if you like, a utility function wrapping the same.
> 
> def docs(dicts):
>     return map(Doc, dicts)

Or, if you really wanted to be crazy

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

I mean, I realize linefeeds don't grow on trees and all... 


-- 
Rob Gaddi, Highland Technology -- www.highlandtechnology.com
Email address domain is currently out of order.  See above to fix.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web