为了更好的处理嵌套数据,如JSON,YAML文件。采用
装饰器@classmethod 加 递归处理 的设计模式比较好。
from addict import Dict
class ConfigDict(Dict):
def __init__(__self, *args, **kwargs):
object.__setattr__(__self, '__parent', kwargs.pop('__parent', None))
object.__setattr__(__self, '__key', kwargs.pop('__key', None))
object.__setattr__(__self, '__frozen', False)
for arg in args:
if not arg:
continue
# Since ConfigDict.items will convert LazyObject to real object
# automatically, we need to call super().items() to make sure
# the LazyObject will not be converted.
if isinstance(arg, ConfigDict):