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


Groups > comp.lang.python > #85223

pymongo and attribute dictionaries

From Travis Griggs <travisgriggs@gmail.com>
Subject pymongo and attribute dictionaries
Date 2015-02-04 08:50 -0800
Newsgroups comp.lang.python
Message-ID <mailman.18469.1423068652.18130.python-list@python.org> (permalink)

Show all headers | View raw


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?

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


Thread

pymongo and attribute dictionaries Travis Griggs <travisgriggs@gmail.com> - 2015-02-04 08:50 -0800
  Re: pymongo and attribute dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-05 09:38 +1100
    Re: pymongo and attribute dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-04 17:19 -0700
      Re: pymongo and attribute dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-05 12:58 +1100
    Re: pymongo and attribute dictionaries Vito De Tullio <vito.detullio@gmail.com> - 2015-02-05 06:36 +0100
      Re: pymongo and attribute dictionaries Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-05 16:43 +1100
        Re: pymongo and attribute dictionaries Anssi Saari <as@sci.fi> - 2015-02-05 15:27 +0200
          Re: pymongo and attribute dictionaries Ian Kelly <ian.g.kelly@gmail.com> - 2015-02-05 14:11 -0700

csiph-web