Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass in day name #574

Open
jamesgeddes opened this issue Apr 1, 2023 · 3 comments
Open

Pass in day name #574

jamesgeddes opened this issue Apr 1, 2023 · 3 comments

Comments

@jamesgeddes
Copy link

jamesgeddes commented Apr 1, 2023

Firstly, thanks for writing schedule! It is fantastic and I have used it a tonne.

I am getting user input to ascertain which days the program should execute. Though I never normally trust a user, for the sake of simplicity here, we shall assume that the user is infallible.

day_name = input("Day: ")

The docs show that one can specify days to run a function on as follows.

 schedule.every().monday.do(job)
 schedule.every().wednesday.at("13:15").do(job)

Unfortunately, I cannot see a way to pass a day name or value into schedule. As such, I am currently running a "check day" job every day.

def check_day():
    day_today = datetime.today().strftime("%A")
    if day_today.lower() != str(input_day).lower():
        return
    schedule.every().day.at(go_time).do(job)


def main():
    schedule.every().day.at("00:01").do(check_day)
    while True:
        n = schedule.idle_seconds()
        if n is None:
            # no more jobs
            break
        elif n > 0:
            # sleep exactly the right amount of time
            sleep(n)
        schedule.run_pending()

This seems a bit inefficient, particularly when there is nothing to do for several days.

TL;DR

It would be great to be able to pass in a day name or number as a variable.

@SearchDream
Copy link

You methods seems will introduce duplicated jobs after 1 month.

@luster
Copy link

luster commented Jul 24, 2023

i'm running into the same thing. without patching the library specifically, you can do something somewhat hacky like the following:

e = schedule.every()
e.start_day = day_name
e.unit = "weeks"
e.at(go_time).do(job)

the day of week properties are setting start_day and unit on the Job object, so this effectively accomplishes the same thing. would be great if the library had a method day_of_week(day_name) that effectively did the same setting of start_day and unit=weeks

@mohana-martin
Copy link

@jamesgeddes I use the following solution:
getattr(schedule.every(5), "seconds").do(lambda: print("Test"))

For your case, replace the "seconds" with the user input. You can also first check whether the user input is actually valid by checking if it exists in in the Job class, i.e.:
"seconds" in schedule.every().__dir__()
and repeat asking your user until you don't get a valid response.

Well and of course don't forget to .lower() the user input first.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants