Python Forum

Full Version: Iterating trough Unique Object List
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!
I am stuck with idea of creating unique object list where I have this log:
Quote: 0.001051 1 1 Rx d 8 00 00 00 05 00 00 00 00 Length = 242000 BitCount = 125 ID = 1
0.001301 1 1 Rx d 8 00 00 00 05 00 00 00 00 Length = 242000 BitCount = 125 ID = 1
0.001549 1 6 Rx d 8 03 00 00 00 00 00 00 00 Length = 240000 BitCount = 124 ID = 6
0.001800 1 9 Rx d 8 04 00 00 00 00 00 00 00 Length = 242000 BitCount = 125 ID = 9
0.002050 1 9 Rx d 8 04 00 00 00 00 00 00 00 Length = 242000 BitCount = 125 ID = 9
0.002300 1 9 Rx d 8 04 00 00 00 00 00 00 00 Length = 242000 BitCount = 125 ID = 9
0.002990 1 1 Rx d 8 00 00 00 05 00 00 00 00 Length = 242000 BitCount = 125 ID = 1
0.003241 1 9 Rx d 8 04 00 00 00 00 00 00 00 Length = 242000 BitCount = 125 ID = 9
0.012996 1 6 Rx d 8 03 00 00 00 00 00 00 00 Length = 240000 BitCount = 124 ID = 6

Here I want to make unique objects list based on 3rd element in each row, That element represents id of a message. So when I read line by line first row will be first unique object in my list, and second row because it has the same ID I want to just append its first element as timestamp and its 00 00 00 05 00 00 00 00 as its value and nothing more, now the third row has ID that is not contained yet in unique object list. So it needs to be added as unique object item.

This is how my class looks like for one item:
class UniqueObjectClass:

    def __init__(self, data_frame):
        self.channel = data_frame[1]
        self.pduName = data_frame[2]
        self.direction = data_frame[3]
        self.payloadList = list()
        self.timestampList = list()
I have problem because I made some function that is not working at all, and cant figurre out any better approach for this.

This is my function that is iterating way too many times.
    with open(ASCFilePath1) as ASCOpen:
        for line in ASCOpen:
            line = line.strip()
            message_split = line.split(' ')
            message_split = list(filter(lambda a: a != '', message_split))      #if lenght is 23 of message then fo the work
            if len(message_split) == DATA_FRAME_LENGTH:
                if len(uniqueObjectList) == 0:
                    uniqueObject = UniqueObjectClass(message_split)
                    uniqueObject.payloadList.append(payload_creator(message_split))
                    uniqueObject.timestampList.append(message_split[0])
                    uniqueObjectList.append(uniqueObject)

                elif len(uniqueObjectList) > 0:
                    for x in uniqueObjectList:
                        print(uniqueObjectList)
                        print("For")
                        pprint(vars(x))
                        if message_split[2] == x.pduName:
                            print("Before append")
                            pprint(vars(x))
                            x.payloadList.append(payload_creator(message_split))
                            x.timestampList.append(message_split[0])
                            pprint(message_split[0])
                            print("After append")
                            pprint(vars(x))

                        else:
                            uniqueObject = UniqueObjectClass(message_split)
                            uniqueObject.payloadList.append(payload_creator(message_split))
                            uniqueObject.timestampList.append(message_split[0])
                            uniqueObjectList.append(uniqueObject)
                            print("New object added")
Than you for your help
I would use a defaultdict to store temporary data
DATA = """0.001051 1 1 Rx d 8 00 00 00 05 00 00 00 00 Length = 242000 BitCount = 125 ID = 1
0.001301 1 1 Rx d 8 00 00 00 05 00 00 00 00 Length = 242000 BitCount = 125 ID = 1
0.001549 1 6 Rx d 8 03 00 00 00 00 00 00 00 Length = 240000 BitCount = 124 ID = 6
0.001800 1 9 Rx d 8 04 00 00 00 00 00 00 00 Length = 242000 BitCount = 125 ID = 9
0.002050 1 9 Rx d 8 04 00 00 00 00 00 00 00 Length = 242000 BitCount = 125 ID = 9
0.002300 1 9 Rx d 8 04 00 00 00 00 00 00 00 Length = 242000 BitCount = 125 ID = 9
0.002990 1 1 Rx d 8 00 00 00 05 00 00 00 00 Length = 242000 BitCount = 125 ID = 1
0.003241 1 9 Rx d 8 04 00 00 00 00 00 00 00 Length = 242000 BitCount = 125 ID = 9
0.012996 1 6 Rx d 8 03 00 00 00 00 00 00 00 Length = 240000 BitCount = 124 ID = 6
"""
import collections
import io
DATA_FRAME_LENGTH = 23

def gen_msg_split(file):
    for lineno, line in enumerate(file, 1):
        message_split = [x for x in line.split(' ') if x]
        if len(message_split) == DATA_FRAME_LENGTH:
            yield lineno, message_split

class LineProcessor:
    def __init__(self):
        self.process = self.process_first
    
    def process_first(self, lineno, name, msg):
        print('process_first')
        self.unique = UniqueObjectClass(msg)
        self.unique.payloadList.append(payload_creator(msg))
        self.unique.timestampList.append(msg[0])
        del self.process
    
    def process(self, lineno, name, msg):
        print('process')
        self.unique.payloadList.append(payload_creator(msg))
        self.unique.timestampList.append(msg[0])

class UniqueObjectClass:
 
    def __init__(self, data_frame):
        self.channel = data_frame[1]
        self.pduName = data_frame[2]
        self.direction = data_frame[3]
        self.payloadList = list()
        self.timestampList = list()
        
def dummy_payload_creator(msg):
    return msg

if __name__ == '__main__':
    payload_creator = dummy_payload_creator
    dd = collections.defaultdict(LineProcessor)
    
    with io.StringIO(DATA) as file:
        for lineno, msg in gen_msg_split(file):
            name = msg[2]
            dd[name].process(lineno, name, msg)
            
    uniqueObjectList = [v.unique for v in dd.values()]
    for item in uniqueObjectList:
        print(item.pduName, item.timestampList)
Output:
9 ['0.001800', '0.002050', '0.002300', '0.003241'] 6 ['0.001549', '0.012996'] 1 ['0.001051', '0.001301', '0.002990']