zoukankan      html  css  js  c++  java
  • yaml读取封装

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """
    对yaml格式的配置文件的操作
    """
    
    import yaml
    import env
    from vendor.config.config_handle import ConfigHandle
    
    
    class YamlHandle(ConfigHandle):
        def __init__(self, dir_name, file_name):                    # 知识点
            """
            @param dir_name: 文件夹名字,从env.py文件导入,eg:env.ELEMENTINI_PATH
            @param file_name: 文件名字
            """
            print(file_name)
            try:
                self.f = open(super().get_file_full_path_name(dir_name, file_name), 'r', encoding='utf-8')
                self.cfg = self.f.read()
            except:
                print("配置文件不存在")
    
        """
            获取yaml文件中的字典value值,可获取字典值任意一层
            ele_dict = {
                'password_input':
                    {
                    'dec': '登录按钮', 
                    'type': 'XPATH', 
                    'value': '//button'
                    },
                'account_number_input':
                    {
                    'dec': '账号输入框', 
                    'type': 'CLASS_NAME', 
                    'value': 'ivu-input', 
                    'index': 0
                    },
            }
        """
        def get_value(self, *keys):
            """
            @param keys:yaml文件中的key值,可多层,['password_input','dec']或者['password_input']
            @return: 返回字典中value值
            """
            try:
                ele_dict = yaml.load(self.cfg, Loader=yaml.FullLoader)  # 用load方法转字典
                print(ele_dict)
                if not keys:
                    return ele_dict
                else:
                    for key in keys:
                        tmp = ele_dict.get(key)
                        if tmp is not None:
                            ele_dict = tmp
                    return ele_dict
            except:
                print("值不存在")
    
        def __del__(self):
            try:
                self.f.close()
            except:
                print("文件操作符关闭失败")
    
    
    if __name__ == '__main__':
        yh = YamlHandle(env.ELEMENTINI_PATH, 'admin_login.yaml')
        print(yh.get_value('login_button'))
  • 相关阅读:
    C#中的委托是什么?事件是不是一种委托?
    SQL重点复习
    数据库生成脚本
    用Winfrom动态生成SQL的insert语句
    如何实现远程连接SQL Server 2008 Express
    跨页面传送
    win7 防火墙开启ping
    关于*.class和*.jar的几个基本认识
    使用cobertura确定测试代码的覆盖率
    Java学习笔记之I/O
  • 原文地址:https://www.cnblogs.com/lvchengda/p/12614004.html
Copyright © 2011-2022 走看看