LoginSignup
7
3

More than 5 years have passed since last update.

easydictを使ってみた(メモ)。

Last updated at Posted at 2014-08-24

ドットで辞書の要素にアクセスできるようになる。
こんな感じ。

d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
print d.foo
print d.bar.x
#出力 3
#出力 1

windows7(python 2.7系) で試した。

ここにある。
https://github.com/makinacorpus/easydict
ここのreadmeをいじる。

インストール
python setup.py install
easy_install easyjson ついでに入れる。

キーに日本語を入れるとエラー。クォートしててもエラー。
何かの時に、これでやられるかもしれん…
値に日本語は通る。

asってこういう風に使えたのか…
readmeのEasyDictは、全部 edictにしないと、この場合ダメなようだ。

テスト
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

#出力 3
#出力 1

j = """{
        "Buffer": 12,
        "name":"アレグラ",
        "List1": [
            {"type" : "point", "coordinates" : [100.1,54.9] },
            {"type" : "point", "coordinates" : [109.4,65.1] },
            {"type" : "point", "coordinates" : [115.2,80.2] },
            {"aaa" : "ポイント", "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)

#出力 12
#出力 アレグラ
#出力 54.9
#出力 ポイント

b = edict()
b.debug = True
b.foo = 7
b.zzz = u"ガンダム"

print b.items()

#出力 [('debug', True), ('foo', 7), ('log', False), ('zzz', u'\u30ac\u30f3\u30c0\u30e0')]
#出力 ガンダム 

class Flower(edict):
     power = 1

f = Flower({'height': 12})
print f.power
print f['power']
print f.items()

#出力 1
#出力 1
#出力 [('power', 1), ('height', 12)]
7
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
3