guarantee-admin-api-v0.2/Utils/CommonUtil.py

62 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from Utils.ErrorUtil import JustThrowError
def sub_dict(param1, param2):
"""
获取字典的子集
Parameters:
param1: 原字典
param2: 子集字段
Returns:
子集
"""
return dict((key, value) for key, value in param1.items() if key in param2)
def df_iterrows(param):
"""
按行以数组形式返回DataFrame的index、data
Parameters:
param: DataFrame 某个df对象
Returns:
result: list 遍历df对象每行数据包括index
"""
result = []
for row in param.iterrows():
index, data = row
result.append([index] + data.tolist())
return result
def get_attr(_dict_, _keys_, **kwargs):
"""
获取字典属性值
若发生了键值异常或类型异常,则返回设置的默认值
若没有默认值,则返回None
"""
value = kwargs['default'] if kwargs.__contains__('default') else None
try:
text = "_dict_"
for _key_ in _keys_:
text = text + "['{}']".format(_key_)
value = eval(text)
except KeyError:
pass
except TypeError:
pass
return value
def trans_fields_name(_dict_, names1, names2):
"""
替换字典键值名称
_dict_: 替换字典
names1: 原字典键值
names2: 新字典键值
"""
for i in range(len(names1)):
_dict_[names2[i]] = _dict_.pop(names1[i])
return _dict_