Increment 'schedule' modules day attribute with variable in Python

百般思念 提交于 2020-02-07 05:25:25

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/58458664/increment-schedule-modules-day-attribute-with-variable-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!