Python Forum
Iterating trough Unique Object List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterating trough Unique Object List
#1
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
Reply
#2
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']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sample random, unique string pairs from a list without repetitions walterwhite 1 462 Nov-19-2023, 10:07 PM
Last Post: deanhystad
  add object and name in list 3lnyn0 4 1,312 Nov-24-2022, 07:33 PM
Last Post: buran
  How to get unique entries in a list and the count of occurrence james2009 5 2,995 May-08-2022, 04:34 AM
Last Post: ndc85430
  AttributeError: 'list' object has no attribute 'upper' Anldra12 4 4,912 Apr-27-2022, 09:27 AM
Last Post: Anldra12
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,020 Jan-19-2022, 08:33 AM
Last Post: menator01
  Delete list while iterating shantanu97 1 1,898 Jun-06-2021, 11:59 AM
Last Post: Yoriz
  AttributeError class object has no attribute list object scttfnch 5 3,469 Feb-24-2021, 10:03 PM
Last Post: scttfnch
  'list' object not callable sidra 5 5,563 Nov-29-2020, 04:56 PM
Last Post: bowlofred
  Creating list of lists from generator object t4keheart 1 2,218 Nov-13-2020, 04:59 AM
Last Post: perfringo
  Creating a list of dictionaries while iterating pythonnewbie138 6 3,306 Sep-27-2020, 08:23 PM
Last Post: pythonnewbie138

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020