Search code examples
pythondictionaryprintingattributeerror

AttributeError: 'list' object has no attribute 'dlc'


I am formatting a data stream to a dictionary and then attempting to print it out in a more human readable format.

However when trying to run the function/method printMsg I get an error which for the life of me I cannot figure out. Please see code below

#!/usr/bin/python

#Imported Modules/Libraries

import re, os, subprocess
from time import sleep, clock
from collections import defaultdict
from multiprocessing import Process, Queue
import pprint
from os import system

#Global Declarations
last_time = 0.0
myDict = {}
count = defaultdict(int)
queueVar = Queue()

class PCANmsg(object):
    def __manline__(self):

        can_ID          = 0
        self.DLC        = 0
        self.CANtime    = 0
        self.PCANperiod = 0
        self.Count      = 0
        self.hdata0     = 0
        self.hdata1     = 0
        self.hdata2     = 0
        self.hdata3     = 0
        self.hdata4     = 0
        self.hdata5     = 0
        self.hdata6     = 0
        self.hdata7     = 0
        self.timing     = 0

def fillDict(lines):
    global myDict
    global last_time
    global count

    x = PCANmsg()

    for line in lines:
        start = clock()
        if(line[0].isdigit()):
            splitline = line.strip().split()

            del splitline[1:4]
            can_ID           = splitline[1]
            x.DLC            = splitline[2]
            x.CANtime        = splitline[0]
            x.PCANperiod     = 0
            x.Count          = count[can_ID] + 1  
            x.hdata0         = splitline[3]
            x.hdata1         = splitline[4]
            x.hdata2         = splitline[5]
            x.hdata3         = splitline[6]
            x.hdata4         = splitline[7]
            x.hdata5         = splitline[8]
            x.hdata6         = splitline[9]
            x.hdata7         = splitline[10]
            end = clock()
            x.timing         = (end - start) * 1000

            myDict[can_ID] = [x.DLC,
                              x.hdata0,
                              x.hdata1,
                              x.hdata2,
                              x.hdata3,
                              x.hdata4,
                              x.hdata5,
                              x.hdata6,
                              x.hdata7,
                              x.timing,
                              x.Count,
                              x.PCANperiod]

            start = end 
            printMsg(myDict, can_ID)


def printMsg(_dict,_id):

    print('%06X: %3d %8.2f %8.2F  %02X %02X %02X %02X %02X %02X %02X %02X %d' %
           (_id,
            _dict[_id].DLC,
            _dict[_id].CANtime,
            _dict[_id].PCANperiod,
            _dict[_id].int(count),
            _dict[_id].hdata0,
            _dict[_id].hdata1,
            _dict[_id].hdata2,
            _dict[_id].hdata3,
            _dict[_id].hdata4,
            _dict[_id].hdata5,
            _dict[_id].hdata6,
            _dict[_id].hdata7,
            _dict[_id].ELAPSED))

if __name__ == "__main__":

    system("echo \"i 0x011c e\" >/dev/pcan33")    

    procRX = subprocess.Popen('receivetest -f=/dev/pcan33'.split(), stdout=subprocess.PIPE)
    lines = iter(procRX.stdout.readline,"")

    fillDict(lines)

When the code executes it runs fine up untul the printMsg(mydict, _id) function call:

Traceback (most recent call last):
  File "./manline.py", line 107, in <module>
    fillDict(lines)
  File "./manline.py", line 79, in fillDict
    printMsg(myDict, can_ID)
  File "./manline.py", line 86, in printMsg
    _dict[_id].DLC,
AttributeError: 'list' object has no attribute 'DLC'

Can anyone advise what is causing this. My aim for this as well is to be able to run the fillDict and printMsg in parallel once I get it printing correctly.


Solution

  • This code piece of yours

    myDict[can_ID] = [x.DLC,
                              x.hdata0,
                              x.hdata1,
                              x.hdata2,
                              x.hdata3,
                              x.hdata4,
                              x.hdata5,
                              x.hdata6,
                              x.hdata7,
                              x.timing,
                              x.Count,
                              x.PCANperiod]
    

    It is saving an entry against the ID in the form of a list. Where as your code piece here

    def printMsg(_dict,_id):
    
        print('%06X: %3d %8.2f %8.2F  %02X %02X %02X %02X %02X %02X %02X %02X %d' %
           (_id,
            _dict[_id].DLC,
            _dict[_id].CANtime,
            _dict[_id].PCANperiod,
            _dict[_id].int(count),
            _dict[_id].hdata0,
            _dict[_id].hdata1,
            _dict[_id].hdata2,
            _dict[_id].hdata3,
            _dict[_id].hdata4,
            _dict[_id].hdata5,
            _dict[_id].hdata6,
            _dict[_id].hdata7,
            _dict[_id].ELAPSED))
    

    is expecting it to be a dictionary.

    It should be something like

    myDict[can_ID] = {}
    myDict[can_ID]["DLC"]= bla bla
    myDict[can_ID]["hdata0"] = bla bla
    ...
    ...
    #So on so forth
    

    Then you can continue printing it the way you are doing right now. Or you can print like this

    for key, val in _dict[_id].iteritems():
        print key, value