Search code examples
pythonarraysschedule

Schedule times overwritten with Schedule module when called via array in Python


I'm attempting to build an alarm application but I'm struggling to get the 'schedule' module to function how I'd like it to. The problem is that I can't seem to schedule multiple alarms for one day while calling the day attribute via an array.

Example of how you'd normally schedule multiple times for one day:

schedule.every().sunday.at('17:25').do(job)
schedule.every().sunday.at('17:30').do(job)
schedule.every().sunday.at('17:35').do(job)

This works fine, but I really want to load times with a for loop so I don't have a giant if statement, and so that I can load times dynamically:

dayArray = [
schedule.every().sunday,
schedule.every().monday,
schedule.every().tuesday,
schedule.every().wednesday,
schedule.every().thursday,
schedule.every().friday,
schedule.every().saturday
]

for i in range(1, xlsxAlarmSheet.ncols):
                for j in range(1, 8):
                    if(str(xlsxAlarmSheet.cell_value(j, i)) != '0'):
                        dayArray[j - 1].at(str(xlsxAlarmSheet.cell_value(j, i))[:2] + ':' + str(xlsxAlarmSheet.cell_value(j, i))[2:]).do(job)

The days are being loaded from an array and the times from an xlsx file via the XLRD module. The only problem is the alarms are overwriting each other somehow when I schedule multiple times for one day. If I schedule 3 times for Sunday with this method for example, only the third scheduled time fires off. I thought it must be because when I load the days into an array they are no longer unique somehow, so I tried doing a 2-dimensional array:

dayArray = [[
schedule.every().sunday,
schedule.every().monday,
schedule.every().tuesday,
schedule.every().wednesday,
schedule.every().thursday,
schedule.every().friday,
schedule.every().saturday
]] * (xlsxAlarmSheet.ncols - 1)

for i in range(1, xlsxAlarmSheet.ncols):
                for j in range(1, 8):
                    if(str(xlsxAlarmSheet.cell_value(j, i)) != '0'):
                        dayArray[i - 1][j - 1].at(str(xlsxAlarmSheet.cell_value(j, i))[:2] + ':' + str(xlsxAlarmSheet.cell_value(j, i))[2:]).do(job)

With no luck... the times are still overwriting each other, any ideas?


Solution

  • In another question I posted, I was originally trying to substitute the day attribute with a string, like Vig is suggesting with his answer to this post. This wasn't working for me, so I ended up storing objects in an array, as Prune suggested on my original question.

    However, Prune also posted a link to an example (example 7) in which the entire call to schedule a time was stored in a string and then called via eval(), which seems to work.

    So this is what I ended up doing:

    dayArray = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']
    dayTimeArray = []
    
    for i in range(1, xlsxAlarmSheet.ncols):
        for j in range(1, 8):
            if(str(xlsxAlarmSheet.cell_value(j, i)) != '0'):
                dayTimeArray.append(
                    "schedule.every().{}.at('{}').do(StartSubProcess)".format(
                        dayArray[j - 1], 
                        str(xlsxAlarmSheet.cell_value(j, i))[:2] + ':' + str(xlsxAlarmSheet.cell_value(j ,i))[2:]
                    )
                )
    
    for i in range(0, len(dayTimeArray)):
        eval(dayTimeArray[i])