Home > Software design >  AttributeError: object has no attribute in for loop
AttributeError: object has no attribute in for loop

Time:01-10

In my simulation of a FIFO algorithm, I am currently trying to create an object for each of the seven simulated tasks, which will later be used to display some time parameters graphically in Excel. So I create all objects in a for loop, and in another one, I execute with each of these objects the corresponding function for transfer to Excel in another class.

But in the second for loop I always get the error message

File "G:\Schedulibg\PyScheduler\pyFIFOAlgorithm.py", line 48, in sched_StartScheduler
    self.worksheet1.write('A'   str((self.tasksAdded * 4)   1), 'Turn Around Time:')
AttributeError: 'pyFIFOAlgorithm' object has no attribute 'worksheet1' "

I don't know why it looks for worksheet1 in pyFIFOAlgorithm, although the object is from pyToExcel and the corresponding method comes from the class. I have already changed the name of taskTest, because I read that this could also be a problem. Before it was just called tsk.

import pyScheduler
from pyToExcel import pyToExcel


def alg_SortArrivedTasks(arrivedTasks):
    sortedTasks = []
    for taskx in arrivedTasks:
        sortedTasks.append(taskx)
    sortedTasks.sort(key=lambda x: x.tskArrival)
    return sortedTasks


class pyFIFOAlgorithm(pyScheduler.pyScheduler):
    def __init__(self, taskSet, sched_Alg, idleTime):
        self.alg_Identifier = 2
        self.alg_Name = 'FIFO'
        self.completionTime = 0
        self.turnAroundTime = 0
        self.totalWaitingTime = 0
        self.totalTurnAroundTime = 0
        self.tasksAdded = 0
        super(pyFIFOAlgorithm, self).__init__(taskSet, sched_Alg, idleTime)


    def sched_StartScheduler(self, taskSet):
        self.sched_SchedulingPoints.insert(0, self.sched_Clock)
        taskList = []
        taskNumber = 1
        for task in alg_SortArrivedTasks(taskSet):
            print("\nArrival time of ", task.tskName, ": ", task.tskArrival)
            self.sched_ExecuteAvailableTasks(task)
            self.completionTime = task.completiontime
            self.turnAroundTime = ((self.completionTime) - task.tskArrival)
            #taskList.append(pyToExcel(taskNumber, self.completionTime, self.turnAroundTime))
            self.totalTurnAroundTime  = self.turnAroundTime
            print("Turn Around Time: ""{:.2f}".format(self.turnAroundTime))
            print("Completion Time: ""{:.2f}".format(self.completionTime))
            taskList.append(pyToExcel(task.tskName, self.completionTime, self.turnAroundTime))

        for taskTest in taskList:
            pyToExcel.inputData(pyToExcel, taskNumber, taskTest.turn, taskTest.completion) #Line with Error
            taskNumber  = 1


        print("\nAll tasks executed at: ", "{:.2f}".format(self.sched_Clock))
        print("Average Waiting Time: ", "{:.2f}".format((self.totalWaitingTime /len(taskSet))))
        print("Average Turn Around Time: ", "{:.2f}".format((self.totalTurnAroundTime / len(taskSet))))
        self.worksheet1.write('A'   str((self.tasksAdded * 4)   1), 'Turn Around Time:')
        self.worksheet1.write('B'   str((self.tasksAdded * 4)   1), self.totalTurnAroundTime / len(taskSet))
        self.workbook.close()

import xlsxwriter

class pyToExcel:
    def __init__(self, task, completion, turn):
        self.task = task
        self.completion = completion
        self.turn = turn

    workbook = xlsxwriter.Workbook('AlgorithmData.xlsx')
    worksheet1 = workbook.add_worksheet('FIFO')
    worksheet2 = workbook.add_worksheet('Graphics')
    cell_format = workbook.add_format(
        {
            "border": 1,
            "border_color": "#000000"
        }
    )
    cell_format.set_font_color('green')
    cell_format.set_bold()
    cell_format.set_align('center')
    cell_format.set_align('vcenter')
    worksheet1.set_column(0, 0, 17)
    worksheet1.set_column(1, 1, 12)
    worksheet2.write('A'   str(1), 'Task', cell_format)
    worksheet2.write('B'   str(1), 'Completion Time', cell_format)
    worksheet2.write('C'   str(1), 'Turn Around Time', cell_format)
    worksheet2.write('D'   str(1), 'Waiting Time', cell_format)
    worksheet2.set_column(0, 0, 4)
    worksheet2.set_column(1, 1, 15)
    worksheet2.set_column(2, 2, 17)
    worksheet2.set_column(3, 3, 12)

    def inputData(self, task,  turnaround, completion):

        pyToExcel.worksheet1.write('A'   str((task * 4)   1), 'Turn Around Time:')
        pyToExcel.worksheet1.write('B'   str((task * 4)   1), turnaround)
        pyToExcel.worksheet1.write('A'   str((task * 4)   2), 'Completion Time:')
        pyToExcel.worksheet1.write('B'   str((task * 4)   2), completion)
        pyToExcel.worksheet2.write('A'   str((task   1)), task)
        pyToExcel.worksheet2.write('B'   str((task   1)), completion)
        pyToExcel.worksheet2.write('C'   str((task   1)), turnaround)
        pyToExcel.worksheet2.write('D'   str((task   1)), 'waiting time')

CodePudding user response:

It's looking up the worksheet1 attribute because you told it to:

self.worksheet1.write('A'   str((self.tasksAdded * 4)   1), 'Turn Around Time:')
     ^^^^^^^^^^

That line, which is also quoted in the error message, is line 48 of the program, as per the text you quote. The line you mark as being the location of the error is line 38, but the traceback certainly says line 48.

The method is part of the class definition for pyFIFOAlgorithm, so self is almost certainly a pyFIFOAlgorithm object. Perhaps self was a typo.

  •  Tags:  
  • Related