pymongo and attribute dictionaries

Chris Kaynor ckaynor at zindagigames.com
Wed Feb 4 15:45:51 EST 2015


On Wed, Feb 4, 2015 at 12:16 PM, Travis Griggs <travisgriggs at gmail.com> wrote:
>
>> On Feb 4, 2015, at 9:22 AM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>>
>> On Wed, Feb 4, 2015 at 9:50 AM, Travis Griggs <travisgriggs at 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
>>
>
> 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...

One possible approach, that may or may not work based on the use case,
would be to monkey patch the module. The main drawback is that if it
is used in places that do not expect the wrapper, it could well break.
Untested code:

import types
def monkey(module):
    for attr in dir(module):
        if attr.startswith('_'): # Ignore private attributes.
            continue
        object = getattr(module, attr)
        if not isinstance(object, types.Function): # Ignore non-functions.
            continue
        setattr(module, attr, lambda *args, **kwargs:
AttrDictWrapper(object(*args, **kwargs)))


Another option would be to create a wrapper module. This option has
the side-effect that it would be difficult to recurse into classes, as
you would have to copy the class. This would be very similar (again,
untested):
import types # Note that this may get overridden in the function,
which could cause problems.
def makeWrapper(module):
    for attr in dir(module):
        if attr.startswith('_'): # Ignore private attributes.
            continue
        object = getattr(module, attr)
        if not isinstance(object, types.Function): # Ignore non-functions.
            object = lambda *args, **kwargs:
AttrDictWrapper(object(*args, **kwargs))
        globals()[attr] = object


Both of these are written to (attempt) to replace all instances.
Obviously, this could be reduced to a white-list or additional checks
could be made to restrict the wrapped sets. I would be VERY weary of
actually using the first due to the noted drawback. The second is
safer, but you would want to be careful to test it for unexpected
side-effects.



More information about the Python-list mailing list