Recursively access dict via attributes as well as index access?

前端 未结 5 1505
隐瞒了意图╮
隐瞒了意图╮ 2020-12-02 06:36

I\'d like to be able to do something like this:

from dotDict import dotdictify

life = {\'bigBang\':
           {\'stars\':
               {\'planets\': []}
         


        
5条回答
  •  心在旅途
    2020-12-02 06:59

    Here's one way to create that kind of experience:

    class DotDictify(dict):
        MARKER = object()
    
        def __init__(self, value=None):
            if value is None:
                pass
            elif isinstance(value, dict):
                for key in value:
                    self.__setitem__(key, value[key])
            else:
                raise TypeError('expected dict')
    
        def __setitem__(self, key, value):
            if isinstance(value, dict) and not isinstance(value, DotDictify):
                value = DotDictify(value)
            super(DotDictify, self).__setitem__(key, value)
    
        def __getitem__(self, key):
            found = self.get(key, DotDictify.MARKER)
            if found is DotDictify.MARKER:
                found = DotDictify()
                super(DotDictify, self).__setitem__(key, found)
            return found
    
        __setattr__, __getattr__ = __setitem__, __getitem__
    
    
    if __name__ == '__main__':
    
        life = {'bigBang':
                   {'stars':
                       {'planets': {}  # Value changed from []
                       }
                   }
               }
    
        life = DotDictify(life)
        print(life.bigBang.stars.planets)  # -> []
        life.bigBang.stars.planets.earth = {'singleCellLife' : {}}
        print(life.bigBang.stars.planets)  # -> {'earth': {'singleCellLife': {}}}
    

提交回复
热议问题