Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.117 X-Spam-Level: * X-Spam-Evidence: '*H*': 0.77; '*S*': 0.00; 'api': 0.11; 'python': 0.11; 'def': 0.12; 'changes': 0.15; 'subject:dictionaries': 0.16; 'travis': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'typing': 0.19; 'feb': 0.22; '(or': 0.24; 'academic': 0.26; 'header:In-Reply- To:1': 0.27; 'function': 0.29; 'message-id:@mail.gmail.com': 0.30; 'doc': 0.31; 'evil': 0.31; 'class': 0.32; 'skip:c 30': 0.32; 'skip:_ 10': 0.34; "i'd": 0.34; 'something': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'like,': 0.36; 'same.': 0.38; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'free': 0.61; '8bit%:10': 0.64; 'map': 0.64; 'strategy': 0.64; 'interest': 0.64; 'relatively': 0.65; 'skip:\xe2 10': 0.65; '8bit%:21': 0.69; '8bit%:43': 0.74; 'inline': 0.74; '2015': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=ZDkDpAm1lRr4ON+jf2eD2vqdZQmDtBXnxsQqQLhLAag=; b=JWQpSBeRYP0HkTi6m7iCr4ZtOpT4A+iwv1s+Q8xMQrcEQHw19i3XJXhBJJL7JHQ7Pe FZbUBfD8WRPGR7PJ2M/viIyZxewQscyiIBOfBSoF2Wp6XjFqr8shYUZ1lyNAUEPtiUgJ pPRmciH1QcU73vyuJkUINaAgRpYOyOLUaeFDGmUTo80yJKSpnHezF5n13UUeKXl0ea2b cfUqXYCsjqD6EmUS6yExgExzIxDUj1l1DVgCyujd2GKVKTA0oilsF3q9NTucjYqlIqBp 75YupNQPcS4ewG4mlW51u9lEq71fRpKeRamq6z0HeZFKFoI1I1dAYyNaY2XiQnZ+EEr0 7WRg== X-Received: by 10.194.71.77 with SMTP id s13mr74959wju.143.1423083556937; Wed, 04 Feb 2015 12:59:16 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <70CF795C-8494-48CF-A5F8-3D3F8FE3AE95@gmail.com> References: <436419A8-6CD1-43DA-AC5E-A2E974AD585E@gmail.com> <70CF795C-8494-48CF-A5F8-3D3F8FE3AE95@gmail.com> From: Ian Kelly Date: Wed, 4 Feb 2015 13:53:28 -0700 Subject: Re: pymongo and attribute dictionaries To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 38 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423083559 news.xs4all.nl 2870 [2001:888:2000:d::a6]:49209 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85232 On Wed, Feb 4, 2015 at 1:16 PM, Travis Griggs wrot= e: > Yes, that is clever. So if you wanted to minimize the amount of typing yo= u 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__ =3D target > > and then something like > > for doc in client.db.radios.find({=E2=80=99_id': {=E2=80=99$regex=E2=80= =99: =E2=80=98^[ABC]'}}): > pprint(doc) > > changes to > > for doc in ((Doc(d) for d in client.db.radios.find({=E2=80=99_id': {=E2= =80=99$regex=E2=80=99: =E2=80=98^[ABC]'}})): > pprint(doc) > > Are there other approaches? Feel free to impress me with evil abuses in t= he 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({=E2=80=99_id': {=E2=80=99$regex= =E2=80=99: =E2=80=98^[ABC]'}})): pprint(doc) Or if you like, a utility function wrapping the same. def docs(dicts): return map(Doc, dicts)