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


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

Re: pymongo and attribute dictionaries

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2015-02-04 13:53 -0700
Last post2015-02-04 13:53 -0700
Articles 1 — 1 participant

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:53 -0700

#85232 — Re: pymongo and attribute dictionaries

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-02-04 13:53 -0700
SubjectRe: pymongo and attribute dictionaries
Message-ID<mailman.18477.1423083559.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] | [standalone]


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


csiph-web