schedule icon indicating copy to clipboard operation
schedule copied to clipboard

Configure day based on string input

Open liewlucas opened this issue 4 years ago • 2 comments

userinput = wednesday 
usertime = "18:30" 
schedule.every().userinput.at(usertime).do(function)

The above does not work as they do not detect the variable's value for userinput but works for usertime

liewlucas avatar May 28 '21 11:05 liewlucas

By writing schedule.every().userinput.at(usertime)., Python thinks you want to access a variable userinput located inside schedule.every(), which doesn't exist.

Unfortunately there is no function available to choose the day of the week based on the content of a variable. At the moment your best guess would be to do a bunch of ifs:

import schedule

def at_day(job, day_name):
    if day_name == "monday":
        return job.monday
    if day_name == "tuesday":
        return job.tuesday
    if day_name == "wednesday":
        return job.wednesday
    if day_name == "thursday":
        return job.thursday
    if day_name == "friday":
        return job.friday
    if day_name == "saturday":
        return job.saturday
    if day_name == "sunday":
        return job.sunday
    raise Exception("Unknown name of day")

userinput = "wednesday"
usertime = "18:30"

job = at_day(schedule.every(), userinput).at(usertime).do(lambda: print("hi"))

print(job.next_run)

SijmenHuizenga avatar Jun 08 '21 17:06 SijmenHuizenga

They should define a __getitem__ that calls the correct property on the job object, so we can invoke schedule.every()[userInput].at(userTime).do(function) where userInput would be a string ("monday", "tuesday", ...)

gruvw avatar Aug 10 '21 08:08 gruvw