Quantcast
Channel: How to use a dot "." to access members of dictionary? - Stack Overflow
Viewing all articles
Browse latest Browse all 40

Answer by touch my body for How to use a dot "." to access members of dictionary?

$
0
0

To build upon epool's answer, this version allows you to access any dict inside via the dot operator:

foo = {"bar" : {"baz" : [ {"boo" : "hoo"} , {"baba" : "loo"} ]    }}

For instance, foo.bar.baz[1].baba returns "loo".

class Map(dict):    def __init__(self, *args, **kwargs):        super(Map, self).__init__(*args, **kwargs)        for arg in args:            if isinstance(arg, dict):                for k, v in arg.items():                    if isinstance(v, dict):                        v = Map(v)                    if isinstance(v, list):                        self.__convert(v)                    self[k] = v        if kwargs:            for k, v in kwargs.items():                if isinstance(v, dict):                    v = Map(v)                elif isinstance(v, list):                    self.__convert(v)                self[k] = v    def __convert(self, v):        for elem in range(0, len(v)):            if isinstance(v[elem], dict):                v[elem] = Map(v[elem])            elif isinstance(v[elem], list):                self.__convert(v[elem])    def __getattr__(self, attr):        return self.get(attr)    def __setattr__(self, key, value):        self.__setitem__(key, value)    def __setitem__(self, key, value):        super(Map, self).__setitem__(key, value)        self.__dict__.update({key: value})    def __delattr__(self, item):        self.__delitem__(item)    def __delitem__(self, key):        super(Map, self).__delitem__(key)        del self.__dict__[key]

Viewing all articles
Browse latest Browse all 40

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>