[Python] basic grammar exercises on the 11th day job

'''
1, class code knock all over again (not requested)
2, there is a set of data, the following format:
[
{ 'Case_id': 1, 'method': 'post', 'url': '/ member / login', 'data': '123', 'actual': 'do not pass', 'excepted': 'through' },
{ 'Case_id': 2, 'method': 'post', 'url': '/ member / login', 'data': '123', 'actual': 'by', 'excepted': 'by'} ,
{ 'Case_id': 3, 'method': 'post', 'url': '/ member / login', 'data': '123', 'actual': 'do not pass', 'excepted': 'through' },
{ 'Case_id': 4, 'method': 'post', 'url': '/ member / login', 'data': '123', 'actual': 'by', 'excepted': 'by'} ,
{ 'Case_id': 4, 'method': 'post', 'url': '/ member / login', 'data': '123', 'actual': 'do not pass', 'excepted': 'through' },
{ 'Case_id': 5, 'method': 'post', 'url': '/ member / login', 'data': '123', 'actual': 'by', 'excepted': 'by'} ,
]
Please list each dictionary to traverse out, each with a data dictionary object to hold,
Requirements: using the last instance of the object test operation to save data in each class in the dictionary
The key corresponding to the dictionary attribute name, value for the attribute value. (Do not use setattr)
Finally, all the test objects, into a list, such as to give the following data formats:
[Object use cases, for example with the object, with the object cases ...]

3, is defined as a class of
class CaseData:
    pass
In question to the second data type by using classData setattr (reflection), to save the format required by the second question
'''

# The second question

class Cases():
    """用例类"""
    def __init__(self, case_id, url, data, method, excepted, actual):
        self.case_id = case_id
        self.url = url
        self.data = data
        self.method = method
        self.excepted = excepted
        self.actual = Actual
 # Please list traversal out each dictionary, the dictionary data of each object to hold a 
Data = [
{ ' The case_id ' :. 1, ' Method ' : ' POST ' , ' URL ' : ' / Member / Login ' , ' Data ' : ' 123 ' , ' Actual ' : ' do not pass ' , ' excepted ' : ' through ' },
{ ' The case_id ' : 2,   ' Method ' : ' POST ' , ' URL ' : ' / Member / Login ' , ' Data ' : ' 123 ' , ' Actual ' : ' by ' , ' excepted ' : ' by ' } ,
{ ' The case_id ' :. 3, ' Method ' : ' POST ' , ' URL ' : ' / Member / Login ' , ' Data ' : ' 123 ' , ' Actual ' : ' do not pass ' , ' excepted ' : ' through ' },
{ ' The case_id ' :. 4,   ' Method ' : ' POST ' , ' URL ' : ' / Member / Login ' , ' Data ' : ' 123 ' , ' Actual ' : ' by ' , ' excepted ' : ' by ' } ,
{ ' The case_id ' :. 4, ' Method ' : ' POST ' , ' URL ' : ' / Member / Login ' , ' Data ' : ' 123 ' , ' Actual ' : ' do not pass ' , ' excepted ' : ' through ' },
{ ' The case_id ' :. 5,   ' Method ' : ' POST ' , ' URL ' : ' / Member / Login ' , ' Data ' : ' 123 ' , ' Actual ' : ' by ' , ' excepted ' : ' by ' } ,
]
# Way a 
DEF work2_2 (the Data):
    new_data = []
    for dic in data:
        # 通过位置传参
        case = Cases(dic["case_id"],dic["url"],dic["data"],dic["method"],dic["excepted"],dic["actual"])
        new_data.append(case)
        return new_data
work2_2(data)

# Second way: 
DEF work2_3 (Data):
     # Create a new list 
    new_data = []
     # go through the list 
    for DIC in Data:
         # create an object will traverse the obtained data into the class init method, initialization (Properties set) 
        Case = Cases (** dic) # here to use the dictionary to unpack ** 
        # will be created out of objects into the new list 
        new_data.append (Case)
         return new_data
work2_3(data)
# res = work2_3(data)
# print(res)

# The third question 
class CaseData ():
     Pass

# Way a 
DEF work3_1 (Data):
     # Create a new list 
    new_data = []
     # go through the list 
    for I in Data:
         # Create an object 
        Case = CaseData ()
         # traversing the current dictionary all the data 
        for K, V in I. items ():
             # traverses out data set as an object property 
            setattr (Case, K, V)
         # set the object good properties, added to the list 
        new_data.append (Case)
     return new_data
work3_1(data)
# res = work3_1(data)
# print(res)

 

Guess you like

Origin www.cnblogs.com/python-test001/p/12400823.html