Path: csiph.com!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'python.': 0.02; 'classes,': 0.05; 'plenty': 0.07; 'apis': 0.09; 'arguments': 0.09; 'methods,': 0.09; 'api': 0.11; 'python': 0.11; 'def': 0.12; 'changes': 0.15; '42,': 0.16; 'behave': 0.16; 'dict': 0.16; 'dictionaries': 0.16; 'googling': 0.16; 'i\xe2\x80\x99d': 0.16; 'i\xe2\x80\x99m': 0.16; 'modifier': 0.16; 'need:': 0.16; 'objective-c,': 0.16; 'subclasses': 0.16; 'subject:dictionaries': 0.16; 'travis': 0.16; 'language': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'module': 0.19; 'typing': 0.19; 'feb': 0.22; 'question': 0.24; 'academic': 0.26; 'header:In-Reply-To:1': 0.27; 'received:172.16': 0.29; 'am,': 0.29; 'returned': 0.30; '>>>>': 0.31; 'doc': 0.31; 'evil': 0.31; 'object.': 0.31; 'though.': 0.31; 'class': 0.32; 'extend': 0.32; 'skip:c 30': 0.32; '(e.g.': 0.33; 'guess': 0.33; 'not.': 0.33; 'copying': 0.34; 'sense': 0.34; 'skip:_ 10': 0.34; 'could': 0.34; 'problem': 0.35; 'problem.': 0.35; 'tool': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'really': 0.36; 'done,': 0.36; 'false': 0.36; 'done': 0.36; 'shows': 0.36; 'should': 0.36; 'too': 0.37; 'message-id:@gmail.com': 0.38; 'tired': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'does': 0.39; 'aside': 0.39; 'expensive': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'ian': 0.60; 'solve': 0.60; 'free': 0.61; 'first': 0.61; 'header:Message-Id:1': 0.63; '8bit%:10': 0.64; 'strategy': 0.64; 'interest': 0.64; 'relatively': 0.65; 'skip:\xe2 10': 0.65; '8bit%:21': 0.69; '8bit%:43': 0.74; '2015': 0.84; 'can\xe2\x80\x99t': 0.84; 'examples.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=content-type:mime-version:subject:from:in-reply-to:date :content-transfer-encoding:message-id:references:to; bh=O774073RYwZZakYUTuu9gfpnOIgklFq2DOZ4P+JhzZI=; b=SJUJfiuM/I1ZIFOTnfvZGRgfZOLlUWUl8A08fFplNZoQCQX4ngh7Q6Lt+leHygKmH7 SgwW86CLbQHb3kxVV/+uRtGrqGKADYiOPNatc23xnZVu9N8+TBLU9Llh/yCmvcDyFhJB jO7Mr8eQGcLXuXjS1ydNLA4MDwutzDPS58SeA7/g/vh5F+7T94pBcEnY3sHWTMu/zUYd L8kbglhjd21sColBMQgW8e9uo2MWY3TAX7A0dU7ZSUeBxJUwDUKq5ELYDBhFKt4ZSFPW H14773a4q84fKwyo9YdHHghGBzb1CL0XK4Sdqa0k4Dv9qDyrmLzkPRjVxP+Q5CxAmFLx +OVg== X-Received: by 10.67.1.66 with SMTP id be2mr42658pad.132.1423080987738; Wed, 04 Feb 2015 12:16:27 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2070.6\)) Subject: Re: pymongo and attribute dictionaries From: Travis Griggs In-Reply-To: Date: Wed, 4 Feb 2015 12:16:24 -0800 Content-Transfer-Encoding: quoted-printable References: <436419A8-6CD1-43DA-AC5E-A2E974AD585E@gmail.com> To: Python X-Mailer: Apple Mail (2.2070.6) 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: 80 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423081408 news.xs4all.nl 2882 [2001:888:2000:d::a6]:35933 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85228 > On Feb 4, 2015, at 9:22 AM, Ian Kelly wrote: >=20 > On Wed, Feb 4, 2015 at 9:50 AM, Travis Griggs = wrote: >> I really like pymongo. And I really like Python. But one thing my = fingers really get tired of typing is >>=20 >> someDoc[=E2=80=98_=E2=80=99id=E2=80=99] >>=20 >> This just does not roll of the fingers well. Too many =E2=80=9Creach = for modifier keys=E2=80=9D in a row. I would rather use >>=20 >> someDoc._id >>=20 >> Googling shows that I=E2=80=99m not the first to want to do this in = the general sense (e.g. = http://stackoverflow.com/questions/4984647/accessing-dict-keys-like-an-att= ribute-in-python). >>=20 >> Arguments aside of whether this should or shouldn=E2=80=99t be done, = I want to know how I might solve this with Python. Consider it an = academic pursuit. >>=20 >> The problem I have is not how to do the AttributeDictionary subclass, = there are plenty of those examples. The problem is that the pymongo APIs = already return dictionaries. In a language (Smalltalk, Objective-C, = Ruby) that supports class extensions, that would be my first tool of = choice to solve this problem. I=E2=80=99d just extend Dictionary to = behave the way I want and be done with it. I can=E2=80=99t do that in = Python though. I guess I could make my own module that subclasses the = relevant pymongo classes, and do super() calling implementations of all = of the relevant methods, coercing the return type. That is a maintenance = headache though. >>=20 >> What are my options, if any? >=20 > You could construct the AttributeDictionary by copying the dict > returned from pymongo. The question then is whether the copying would > be too expensive or not. >=20 > Alternately, you could just wrap the dictionaries returned by pymongo > in an object. Something like this should be all you need: >=20 > class AttrDictWrapper(object): > def __init__(self, the_dict): > self.__dict__ =3D the_dict >=20 >>>> d =3D AttrDictWrapper({'spam': 42, 'ham': False}) >>>> d.spam > 42 >>>> d.ham > False >=20 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__ =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 = the interest of academic enrichment...