python 测试常用的小方法
def xia_to_tuo(src: str, firstUpper: bool = False):
"""
将下划线分隔的名字,转换为驼峰模式
:param src:
:param firstUpper: 转换后的首字母是否指定大写(如
:return:
"""
arr = src.split('_')
res = ''
for i in arr:
res = res + i[0].upper() + i[1:]
if not firstUpper:
res = res[0].lower() + res[1:]
return res
def tuo_to_xia(text):
"""
将驼峰分隔的名字,转换为下划线模式
"""
lst = []
for index, char in enumerate(text):
if char.isupper() and index != 0:
lst.append("_")
lst.append(char)
return "".join(lst).lower()
def del_null(data):
"""删除字典值为空的字段"""
for k in list(data.keys()):
if not data[k]:
del data[k]
return data
def del_part(data,col):
"""删除字典部分字段"""
for k in list(data.keys()):
if k in col:
del data[k]
return data
def dict_process(data):
"""将2个字典合并为一个字典"""
return dict(data['head'],**data['body'])
def create_row(start,end):
"""创建固定格式的字典元组"""
li = []
for i in range(start, end + 1):
di = {'row': i}
li.append(di)
return tuple(li)
def create_row_list(start,end):
"""创建固定格式的字典列表"""
li = []
for i in range(start, end + 1):
di = {'row': i}
li.append(di)
return list(li)
if __name__ == '__main__':
print(xia_to_tuo("super_merchant_code"))
print(tuo_to_xia("superMerchantCode"))
params = {
"random": "201910111606001",
"pageNumber": 1,
"pageSize": 20,
"payType": ""
}
print(del_null(params))
del_col = ["pageNumber","pageSize"]
print(del_part(params,del_col))
requestJson = {
"head": {
"service": "front.userauth",
"version": "1.0",
"input_charset": "UTF-8"
},
"body": {
"channel_type": "QP"
}
}
print(dict_process(requestJson))
print(create_row(2, 4))
print(create_row_list(2, 4))
返回信息
superMerchantCode
super_merchant_code
{'random': '201910111606001', 'pageNumber': 1, 'pageSize': 20}
{'random': '201910111606001'}
{'service': 'front.userauth', 'version': '1.0', 'input_charset': 'UTF-8', 'channel_type': 'QP'}
({'row': 2}, {'row': 3}, {'row': 4})
[{'row': 2}, {'row': 3}, {'row': 4}]