编程语言
首页 > 编程语言> > python之yaml文件读取封装

python之yaml文件读取封装

作者:互联网

import os
import yaml
from yamlinclude import YamlIncludeConstructor

YamlIncludeConstructor.add_to_loader_class(loader_class=yaml.FullLoader)   # 用于yaml文件嵌套

PATH = lambda p: os.path.abspath(os.path.join(
    os.path.dirname(__file__), p
))


class YamlData:
    def __init__(self, file):
        if os.path.isfile(PATH(file)):
            self.file = PATH(file)
        else:
            raise FileNotFoundError("文件不存在")

    @property  # 设置属性,调用data方法时可通过调用属性,不需要带括号
    def data(self):
        with open(file=self.file, mode="rb") as f:
            infos = yaml.load(f, Loader=yaml.FullLoader)
            # infos = yaml.load(f)
        return infos

调用操作

infos = YamlData("htmls/loginsucess.yaml").data
print(infos)
"D:\Program Files\Python\Python37-32\python.exe" D:/demo/yamldata.py
{'id': 'login_001', 'module': '登入页面', 'title': '登入时账号为空', 'message': '已打开链接', 'testcase': [{'element_info': 'css->[placeholder="请输入账号"]', 'operate_type': 'send_keys', 'keys': 'SSSS', 'info': '点击账号输入框,输入账号'}, {'element_info': 'css->[placeholder="请输入密码"]', 'operate_type': 'send_keys', 'keys': 'XXX', 'info': '点击密码输入框,输入密码'}, {'element_info': 'div->"登 录"', 'operate_type': 'click', 'info': '点击登入菜单'}, {'operate_type': 'is_sleep', 'keys': 3, 'info': '等待进入'}], 'check': None}

Process finished with exit code 0

 

标签:info,封装,python,keys,yaml,file,infos,os
来源: https://www.cnblogs.com/mian-1122/p/14235535.html