AttributeError: object has no attribute in for loop

AttributeError: object has no attribute in for loop

在我对FIFO算法的模拟中,我目前正在尝试为七个模拟任务中的每一个创建一个对象,稍后将用于在Excel中以图形方式显示一些时间参数.所以我在 for 循环中创建所有对象,在另一个循环中,我对这些对象中的每一个执行相应的函数,以便在另一个 class.

中传输到 Excel

但是在第二个for循环中我总是得到错误信息

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' "

我不知道为什么它在pyFIFOAlgorithm中寻找worksheet1,虽然对象来自pyToExcel并且相应的方法来自class。我已经更改了 taskTest 的名称,因为我读到这也可能是一个问题。之前只是叫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')

它正在查找 worksheet1 属性,因为您告诉它:

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

根据您引用的文本,错误消息中也引用的那一行是程序的第 48 行。您标记为错误位置的行是 38 行,但回溯肯定是 48.

该方法是 pyFIFOAlgorithm 的 class 定义的一部分,因此 self 几乎可以肯定是一个 pyFIFOAlgorithm 对象。也许 self 打错了。