Tag: Object [] Traversal key value request [1] amount self ADL
#! /usr/bin/python
# Coding:utf-8
# class Changedata (object):
Class GetValues (object):
def __init__ (self):
Pass
#通过key获取嵌套字典value
def get_target_value (Self,key, DIC, tmp_list):
"""
:p Aram Key: Target key value
:p Aram Dic:json Data
:p Aram Tmp_list: Used to store acquired data
: return:list
"""
If not isinstance (DIC, dict) or not isinstance (tmp_list, list): # format check for incoming data
Return ' argv[1] not a dict or argv[-1] not a list '
If key in Dic.keys ():
Tmp_list.append (Dic[key]) # Incoming data is present and stored in tmp_list
Else
For value in Dic.values (): # Traversal of the value of the incoming data if it does not conform
If Isinstance (value, dict):
Print (' Look here ', value)
Self.get_target_value (key, Value, Tmp_list) # The value of the incoming data is a dictionary, it calls itself directly
Elif isinstance (value, (list, tuple)):
Self.get_value (key, Value, Tmp_list) # The value of the incoming data is a list or tuple, then the _get_value is called
Return tmp_list
#通过key获取嵌套字典value子方法
def get_value (Self,key, Val, tmp_list):
For Val_ in Val:
If Isinstance (Val_, dict):
Self.get_target_value (Key, Val_, tmp_list) # The value of the incoming data is a dictionary, then the get_target_value is called
Elif isinstance (Val_, (list, tuple)):
Self.get_value (Key, Val_, tmp_list) # The value of the incoming data is a list or a tuple, it calls itself
#替换请求参数
"""
Key: A key to replace the dictionary
DIC: Target Dictionary
Changedata: Replace value
"""
def replace_target_value (Self,key,dic,changedata):
If not isinstance (DIC, Dict): # format check for incoming data
Return ' argv[1] not a dict or argv[-1] not a list '
If key in Dic.keys ():
Dic[key]=changedata # Modifying Dictionaries
Else
For value in Dic.values (): # Traversal of the value of the incoming data if it does not conform
If Isinstance (value, dict):
Self.replace_target_value (key, Value, Changedata) # The value of the incoming data is a dictionary, it calls itself directly, and the value is passed in as a dictionary
Value[key]=changedata #替换key的value
Elif isinstance (value, (list, tuple)):
Self.replace_target (key, Value, Changedata) # The value of the incoming data is a list or tuple, then the _get_value is called
return dic
#替换参数子方法
#数据类型判断, call the unused method after traversal
def replace_target (Self,key, Val, changedata):
For Val_ in Val:
If Isinstance (Val_, dict):
Self.replace_target_value (Key, Val_, Changedata) # The value of the incoming data is a dictionary, then the replace_target_value is called
Elif isinstance (Val_, (list, tuple)):
Self.replace_target (Key, Val_, Changedata) # The value of the incoming data is a list or a tuple, it calls itself
if __name__ = = ' __main__ ':
Tmp_list=[]
tmp_dic={
' Respcode ': ' 0000 ',
' Message ': ' Success ',
' Data ': {
' TotalCount ': 1,
' Totalpage ': 1,
' Items ': [{
' Publishtime ': 12345678910,
' TotalAmount ': 0,
' ProjectID ': ' 789 '
}]
}
}
test_dic={
' Respcode ': ' 0000 ',
' Message ': ' Success ',
' Data ': {
' TotalCount ': 1,
' Totalpage ': 1,
' Items ': [{
' Publishtime ': 1521083405000,
' TotalAmount ': 0,
' Fullsuccesstime ': 1521083405000,
' Submittime ': 1521082143000,
' Deadlineunit ': 1,
' Createtime ': 1521082143000,
' Managementfee ': ' 0.00 ',
' Penaltyfee ': ' 0.00 ',
' Contractamount ': ' 3000.00 ',
' Days ': 0,
' Borrowtype ': 1,
' ProjectID ': ' 57185181850095616 '
}]
}
}
Print (Type (test_dic))
#a =getvalues (). Get_target_value (key= ' ProjectID ', dic=test_dic,tmp_list=tmp_list)
Print (' Pre-replace: ', tmp_dic)
B=getvalues (). Replace_target_value (key= ' ProjectID ', dic=tmp_dic,changedata= ' 57185181850095616 ')
Print (' After replace: ', b)
Python interface Automation--JSON parameter substitution