Schedule a 6-day timetable for 5 days in python

可紊 提交于 2021-02-07 10:07:35

问题


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()

Each 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)?

The Python 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.


回答1:


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

Now 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.



来源:https://stackoverflow.com/questions/64074378/schedule-a-6-day-timetable-for-5-days-in-python

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