Search code examples
pythonattributesschedule

Increment 'schedule' modules day attribute with variable in Python


I'm very new to Python so forgive me if my terminology in the title or body of this question is incorrect.

So this is how you'd normally use schedule to fire something off:

schedule.every().sunday.at('8:00').do(job)

Rather than listing unique days and times in the schedule module I'm trying to use a for loop to increment day and time from an array and an excel sheet respectively, example:

dayArray = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']

for i in range(0, 6):            
    if(str(xlsxSheet.cell_value(i,1)) != 'NA'):
        schedule.every().dayArray[i].at(str(xlsxSheet.cell_value(i+18,1))).do(job)

But I just get the error AttributeError: 'Job' object has no attribute 'dayArray', is it possible to substitute this attribute name with a variable? Most answers I'm finding online are in regards to returning the value of an attribute, haven't been able to find anything.


Solution

  • That third element in the expression must be a time indicator or unit. The dangerous way is to build the command string you want (see this tutorial link, example 7), and then use literal_eval to execute the command.

    Another possibility is to put the elements you want into your list, rather than their display names:

    day_scheduler_list = [
        schedule.every().sunday,
        schedule.every().monday,
        ...
    ]
    ...
    day_scheduler_list[i].at(str(xlsxSheet.cell_value(i+18,1))).do(job)