You will be able to access the elements of the dictionary with dots. Like this.
d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
print d.foo
print d.bar.x
#Output 3
#Output 1
I tried it on windows7 (python 2.7 series).
It is here. https://github.com/makinacorpus/easydict Play with the readme here.
Installation python setup.py install easy_install easyjson Add it in the meantime.
An error occurs when Japanese is entered in the key. Error even if quoted. At some point, this might be possible ... Japanese can be used for the value.
Could as be used like this ... EasyDict of readme seems to be useless in this case unless it is all edict.
test
code
from easydict import EasyDict as edict
from simplejson import loads
#from prettyprint import pp
d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
print d.foo
print d.bar.x
#Output 3
#Output 1
j = """{
"Buffer": 12,
"name":"Allegra",
"List1": [
{"type" : "point", "coordinates" : [100.1,54.9] },
{"type" : "point", "coordinates" : [109.4,65.1] },
{"type" : "point", "coordinates" : [115.2,80.2] },
{"aaa" : "point", "coordinates" : [150.9,97.8] }
]
}"""
d = edict(loads(j))
print d.Buffer
print d.name
print d.List1[0].coordinates[1]
print d.List1[3].aaa
#pp(d)
#Output 12
#Output Allegra
#Output 54.9
#Output point
b = edict()
b.debug = True
b.foo = 7
b.zzz = u"Gundam"
print b.items()
#output[('debug', True), ('foo', 7), ('log', False), ('zzz', u'\u30ac\u30f3\u30c0\u30e0')]
#Output Gundam
class Flower(edict):
power = 1
f = Flower({'height': 12})
print f.power
print f['power']
print f.items()
#Output 1
#Output 1
#output[('power', 1), ('height', 12)]
Recommended Posts