I’m creating an automation in Python, that will automatically navigate to my school’s website, and join the online class, etc using selenium. I’ve completely finished that part, and it all works perfectly. Now, I’m trying to figure out how to schedule these actions.
I was thinking of using schedule? It seems good for straightforward jobs, but I don’t know if it would be any good for something like this.
I have 10 classes in total. I have 6 classes in a day. My school week goes from Monday and Friday. In my timetable, there are a total of 6 days, meaning that every week, one day in the timetable gets rotated out, and I don’t have it that week, and another rotates in.
So, to further explain what I mean by that, in the first week, I would have Days 1-5 Monday to Friday, but I would not have Day 6. I would have Day 6 the following Monday.
I’ve defined the days as follows:
import configparser from selenium import webdriver from selenium.common.exceptions import TimeoutException from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait driver = webdriver.Firefox() <defining what each class does here with selenium, etc.> # Defining what classes I have in each day def Day1(): class() class() class() class() class() class() def Day2(): class() class() class() class() class() class() def Day3(): class() class() class() class() class() class() def Day4(): class() class() class() class() class() class() def Day5(): class() class() class() class() class() class() def Day6(): class() class() class() class() class() class()
class just being a different class I have each day. Some are the same, some are different, but it doesn’t really matter for this question. (I’ve just called them all
class for this question cos it’s easier)
How do I go about scheduling these days (Day1 – Day6) so that they run on Monday to Friday with one day rotating out and then back in the next week (as I described above)?
schedule module seems like it would be able to do it, but I don’t even know where to start with that when I have something like this.
I feel like it would be a pretty simple solution, but I can’t seem to work it out.
You need to calculate which day of the school term it is, and mod that by 6. A good starting point would be this bit of code, to get the current day within the current year:
from datetime import datetime day_of_year = datetime.now().timetuple().tm_yday
You’ll also want to figure out which day of the year was the first day of school. Call that
first_day_of_school. Then, assuming school started on a Monday:
day_of_school = day_of_year - first_day_of_school week_of_school = day_of_school // 7 day_of_week = day_of_school % 7 # 0 means Monday, European style day_of_term = week_of_school * 5 + day_of_week class_index = day_of_term % 6
class_index is the number in [0, 5] telling you which set of classes to attend on this day.
You may need to adjust this for holidays, e.g. if a Friday holiday means the Friday classes are not skipped but shifted to Monday. For that you need proper business day arithmetic, which can be done easily and efficiently using NumPy or other libraries, or you can just hard-code a list of holidays and do it brute-force, since a school term is only a few months long.