dict对象,支持点表示法中的寻址键。
dotted-dict的Python项目详细描述
默认python dict对象的轻量级扩展。这允许将密钥名用作 对象属性。
简单用法
In [1]: from dotted_dict import DottedDict In [2]: example = DottedDict() In [3]: example['foo'] = 1 In [4]: example.foo Out[4]: 1 In [5]: example.bar = 2 In [6]: example Out[6]: DottedDict({'bar': 2, 'foo': 1}) In [7]: del example['foo'] In [8]: del example.bar In [9]: example Out[9]: DottedDict({})
还允许以与普通dict对象相同的方式传入值。
In [10]: example = DottedDict({'foo': 1, 'bar': 2}) In [11]: example Out[11]: DottedDict({'bar': 2, 'foo': 1})
无效字符的问题。此库范围内的有效密钥名必须符合
遵循正则表达式[a-zA-Z_][a-zA-Z0-9_]*$
。如果密钥名不一致,
库将使您的密钥变异为安全格式。空格和无效字符替换为
_是的。如果键以int开头,则添加前导符。
In [12]: DottedDict({'My fun key': 1, 'John\'s': 1, 'Mr. Man': 1}) Out[12]: DottedDict({'John_s': 1, 'Mr__Man': 1, 'My_fun_key': 1}) In [13]: DottedDict({1: 2}) Out[13]: DottedDict({'_1': 2})