开发者

Python 2.5 version of this method

I have the below function running fine on python 2.6, but appengine's production environment is 2.5 and it blows up on:

AttributeError: 'module' object has no attribute 'Mapping'

Does anyone have a solution for 2.5?

Thanks

Chris

import types, collections

class AttrDict(dict):
    '''AttrDict - dict with JS-like key=attr access'''
    def __init__(self, *argz, **kwz):
        if len(argz) == 1 a开发者_StackOverflow社区nd not kwz and isinstance(argz[0], types.StringTypes):
            super(AttrDict, self).__init__(open(argz[0]))
        else:
            super(AttrDict, self).__init__(*argz, **kwz)
            for k,v in self.iteritems(): setattr(self, k, v) # re-construct all values via factory

    def __val_factory(self, val):
        return AttrDict(val) if isinstance(val, collections.Mapping) else val

    def __getattr__(self, k):
        return super(AttrDict, self).__getitem__(k)
        __getitem__ = __getattr__

    def __setattr__(self, k, v):
        return super(AttrDict, self).__setitem__(k, self.__val_factory(v))
        __setitem__ = __setattr__


Try to replace

isinstance(val, collections.Mapping)

with

isinstance(val, dict)


The function will never get a parameter that is an instance of Mapping because that class is not in python 2.5. Simply replace that line with return val.

This is assuming none of your other code uses Mapping or a subclass thereof. If you're actually using it, you'll have to come up with a replacement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜