• yaml&easydict作为参数文件


    环境配置

    # 环境配置
    pip install easydict
    pip install pyyaml
    

    基本用法

    # 基本用法 及 dict vs. easydict
    
    # 普通dict
    # (k, v) 
    # k可以是int/float/str
    # v为dict/任意类型
    # 通过dict[?][?]...访问
    gdict = dict()
    gdict[1] = 1
    gdict[2.3] = 2.3
    gdict['4'] = '4'
    2.3 in gdict
    Out[17]: True
    gdict[2.3]
    Out[18]: 2.3
    
    # easydict(属于普通dict的子类,isinstance(tdict, dict)为True)
    # (k, v)
    # k只能是str
    # v为edict/任意类型
    # 通过edict.?.?...或edict[?][?]...访问
    from easydict import EasyDict as edict
    tdict = edict()
    tdict['sss'] = 'sss'
    tdict.ttt = 'ttt'
    'sss' in tdict
    Out[24]: True
    'ttt' in tdict
    Out[25]: True
    tdict['sss'], tdict['ttt'], tdict.sss, tdict.ttt
    Out[26]: ('sss', 'ttt', 'sss', 'ttt')
    
    isinstance(gdict, dict), isinstance(gdict, edict)
    Out[31]: (True, False)
    isinstance(tdict, dict), isinstance(tdict, edict)
    Out[32]: (True, True)
    

    实例

    # ttt.yaml
    NORMALIZE: True
    DATASET:
      NAME:
        A: 8
        B: 9
    
    # ttt.py
    from easydict import EasyDict as edict
    import yaml
    
    
    def _update_dict(cfg, value, only_update=False, keep_normal=[]):
        """
        将源edict更新到目的edict
        :param cfg: 目的edict对象
        :param value: 源edict对象
        :param only_update: 仅更新不插入,不存在k时报错
        :param keep_normal: 当k in keep_normal,则将v直接赋值给cfg[k](即使v为edict)
        :return:
        """
        for k, v in value.items():
            if only_update:
                if k not in cfg:
                    raise ValueError("{} not exist in config.py".format(k))
                else:
                    if isinstance(v, edict) and k not in keep_normal:
                        _update_dict(cfg[k], v, only_update, keep_normal)
                    else:
                        cfg[k] = v
            else:
                if isinstance(v, edict) and k not in keep_normal:
                    if k not in cfg:
                        cfg[k] = edict()
                    _update_dict(cfg[k], v, only_update, keep_normal)
                else:
                    cfg[k] = v
    
    
    def update_config(cfg, config_file, only_update=False, keep_normal=[]):
        """
        将配置文件更新到cfg
        :param cfg: 目的edict对象
        :param config_file: yaml文件路径
        :param only_update: 仅更新不插入,不存在k时报错
        :param keep_normal: 当k in keep_normal,则将v直接赋值给cfg[k](即使v为edict)
        :return:
        """
        with open(config_file) as f:
            exp_config = edict(yaml.load(f, Loader=yaml.FullLoader))
        _update_dict(cfg, exp_config, only_update, keep_normal)
    
    # 插入模式,不存在的键将插入值,存在的键将更新值
    cfg = edict()
    update_config(cfg, 'ttt.yaml')
    print(cfg)
    # {'NORMALIZE': True, 'DATASET': {'NAME': {'A': 8, 'B': 9}}}
    
    # 更新模式,必须保证存在键
    cfg = edict()
    cfg.NORMALIZE = None
    cfg.DATASET = edict()
    cfg.DATASET.NAME = edict()
    cfg.DATASET.NAME.A = None
    cfg.DATASET.NAME.B = None
    update_config(cfg, 'ttt.yaml', only_update=True)
    print(cfg)
    # {'NORMALIZE': True, 'DATASET': {'NAME': {'A': 8, 'B': 9}}}
    
    # 更新模式,但将cfg.DATASET的值作为普通元素而非edict进行下级插入/更新
    cfg = edict()
    cfg.NORMALIZE = None
    cfg.DATASET = None
    update_config(cfg, 'ttt.yaml', only_update=True, keep_normal=['DATASET'])
    print(cfg)
    # {'NORMALIZE': True, 'DATASET': {'NAME': {'A': 8, 'B': 9}}}
    
  • 相关阅读:
    Vue开发中的一些常见套路和技巧(上)
    【C语言】字符串逆序
    leetcode刷题(第四十五天)70. 爬楼梯 (进阶); 322. 零钱兑换 ; 279.完全平方数
    go-cli
    namespace命名空间
    JavaWeb Tomcat启动、部署、配置、集成IDEA
    Wireshark的数据包它来啦!
    Python 中的变量Variable
    kali操作系统--namp扫描,破解密码,抓包
    [Python] 文件读写
  • 原文地址:https://blog.csdn.net/qq_42283621/article/details/127048233