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


Groups > comp.lang.python > #85226

Re: pymongo and attribute dictionaries

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'classes,': 0.05; 'plenty': 0.07; 'apis': 0.09; 'arguments': 0.09; 'methods,': 0.09; 'python': 0.11; 'def': 0.12; '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; '>>>': 0.22; 'question': 0.24; 'academic': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'returned': 0.30; 'message-id:@mail.gmail.com': 0.30; 'object.': 0.31; 'though.': 0.31; 'class': 0.32; 'extend': 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; '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; 'solve': 0.60; 'first': 0.61; '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=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=YGX8k3+990Qq6i150y8ra4LlisUS6G2VNhd5dBMDOxM=; b=DBSeixAWjhata11DYRWydgbq4xpQ+gE2RyfMC8TK2rsIJaotfuwCk7YhnydFfQ+jmc KBfleUdNuiJhAF6kpG+gQJ3yTt6K4orj6FovNpx9yKQrjagL3a0Uv8ChbxmLj6/Y/JXq tzIIlP0Cl9RpXYt4g5YH4LW7plM+607kjmH8aaSdv3WWOk2maTOqIMT8k3+gW/rwDUZN kpHqxAyHDunds91n6CMBdlnOV0Dyo6VqUPg5xv9V70MDgm47226gyzla4yWIdUV2XTGG DSw61N2zFkRHky3goY/AiDWdPJGjIW8OIhoOETcKEau2TsurXS7tawoJAJe7gpK3d6+G JXVA==
X-Received by 10.68.237.2 with SMTP id uy2mr48104604pbc.72.1423070562650; Wed, 04 Feb 2015 09:22:42 -0800 (PST)
MIME-Version 1.0
In-Reply-To <436419A8-6CD1-43DA-AC5E-A2E974AD585E@gmail.com>
References <436419A8-6CD1-43DA-AC5E-A2E974AD585E@gmail.com>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Wed, 4 Feb 2015 10:22:02 -0700
Subject Re: pymongo and attribute dictionaries
To Python <python-list@python.org>
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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.18472.1423070995.18130.python-list@python.org> (permalink)
Lines 48
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1423070995 news.xs4all.nl 2893 [2001:888:2000:d::a6]:36354
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:85226

Show key headers only | View raw


On Wed, Feb 4, 2015 at 9:50 AM, Travis Griggs <travisgriggs@gmail.com> wrote:
> I really like pymongo. And I really like Python. But one thing my fingers really get tired of typing is
>
> someDoc[‘_’id’]
>
> This just does not roll of the fingers well. Too many “reach for modifier keys” in a row. I would rather use
>
> someDoc._id
>
> Googling shows that I’m not the first to want to do this in the general sense (e.g. http://stackoverflow.com/questions/4984647/accessing-dict-keys-like-an-attribute-in-python).
>
> Arguments aside of whether this should or shouldn’t be done, I want to know how I might solve this with Python. Consider it an academic pursuit.
>
> 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’d just extend Dictionary to behave the way I want and be done with it. I can’t 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.
>
> What are my options, if any?

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.

Alternately, you could just wrap the dictionaries returned by pymongo
in an object. Something like this should be all you need:

class AttrDictWrapper(object):
    def __init__(self, the_dict):
        self.__dict__ = the_dict

>>> d = AttrDictWrapper({'spam': 42, 'ham': False})
>>> d.spam
42
>>> d.ham
False

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 10:22 -0700

csiph-web