美文网首页
easydict模块

easydict模块

作者: ZK_L | 来源:发表于2019-05-31 17:54 被阅读0次

    简介

    EasyDict允许以属性的形式访问字典值,且可以递归地访问。一种用于python字典属性的点表示法,有点类似Javascript。

    安装

    pip install easydict
    

    使用

    from easydict import EasyDict as edict
    d = edict({'foo':3, 'bar':{'x':1, 'y':2}})
    print(d.bar.x)
    d = edict(foo=3)
    print(d.foo)
    
    from simplejson import loads
    j = """{
    "Buffer": 12,
    "List1": [
        {"type" : "point", "coordinates" : [100.1,54.9] },
        {"type" : "point", "coordinates" : [109.4,65.1] },
        {"type" : "point", "coordinates" : [115.2,80.2] },
        {"type" : "point", "coordinates" : [150.9,97.8] }
    ]}"""
    d = edict(loads(j))
    print(d.Buffer)
    print(d.List[0].coordinates[1])
    
    from easydict import EasyDict
    d = EasyDict()
    d.foo = 3
    print(d.foo)
    
    d = EasyDict(log=False)
    d.debug = True
    print(d.items())
    
    class Flower(EasyDict):
             power = 1
    f = Flower({'height': 12})
    print(f.power)
    print(f['power'])
    

    相关文章

      网友评论

          本文标题:easydict模块

          本文链接:https://www.haomeiwen.com/subject/ddowtctx.html