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


Groups > comp.lang.python > #85232

Re: pymongo and attribute dictionaries

References <436419A8-6CD1-43DA-AC5E-A2E974AD585E@gmail.com> <CALwzid=W=R3A9nS1ZFUNduR0WRdMnLdu2g9q+E753DSfEUBy8Q@mail.gmail.com> <70CF795C-8494-48CF-A5F8-3D3F8FE3AE95@gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2015-02-04 13:53 -0700
Subject Re: pymongo and attribute dictionaries
Newsgroups comp.lang.python
Message-ID <mailman.18477.1423083559.18130.python-list@python.org> (permalink)

Show all headers | View raw


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)

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: pymongo and attribute dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-04 13:53 -0700

csiph-web