"""
@Project: py-gql-fast-test-api
@File: handle.py
@Author: zy7y
@Time: 2021/3/31 16:48
@Blog: https://www.cnblogs.com/zy7y
@Github: https://gitee.com/zy7y
@Desc: 处理方法
"""
class Handle:
var_pool = {}
@staticmethod
def set_var(key: object, value: object):
"""存变量"""
Handle.var_pool[key] = value
@staticmethod
def get_var(key: object, default: object = None):
"""取变量"""
return Handle.var_pool.get(key, object)
@staticmethod
def handle_var_list(value: list):
"""
处理参数替换~ 类型是list的逻辑
"""
for index, liv in enumerate(value):
if isinstance(liv, str) and liv[:2] == '${' and liv[-1] == '}':
liv = liv[2:-1]
value[index] = Handle.get_var(liv)
elif isinstance(liv, list):
Handle.handle_var_list(liv)
elif isinstance(liv, dict):
Handle.handle_var(liv)
@staticmethod
def handle_var(var: dict) -> dict:
"""处理自定义参数类型"""
for key, value in var.items():
if isinstance(value, dict):
Handle.handle_var(value)
elif isinstance(value, list):
Handle.handle_var_list(value)
elif isinstance(value, str) and value[:2] == '${' and value[-1] == '}':
value = value[2:-1]
var[key] = Handle.get_var(value)
return var
if __name__ == '__main__':
Handle.var_pool = {"name": "att", "age": 18, "height": 17.8}
data = {
"name": "${name}",
"info": {"${name}": ["${age}", "${height}"]},
"lists": ["${name}", ["${age}", "${height}"], {"name": "${name}"}]
}
print(Handle.handle_var(data))
可能有Bug