在python中安排5天的6天时间表 [英] Schedule a 6-day timetable for 5 days in python

查看:58
本文介绍了在python中安排5天的6天时间表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 创建一个自动化程序,它将自动导航到我学校的网站,并使用 selenium 加入在线课程等.我已经完全完成了那部分,一切都完美无缺.现在,我想弄清楚如何安排这些操作.

我想使用schedule?对于简单的工作来说似乎不错,但我不知道对于这样的事情是否有好处.

我总共有 10 节课.我一天有 6 节课.我的学校周从星期一和星期五开始.在我的时间表中,总共有 6 天,这意味着每周,时间表中的一天轮换出去,而我那周没有,然后又轮换进来.>

所以,为了进一步解释我的意思,在第一周,我会有第 1-5 天星期一到星期五,但我不会有第 6 天.我有第 6 天下周一.

我定义的日子如下:

import configparser从硒导入网络驱动程序从 selenium.common.exceptions 导入 TimeoutException从 selenium.webdriver.support 导入 expected_conditions 作为 ECfrom selenium.webdriver.common.by import By从 selenium.webdriver.support.ui 导入 WebDriverWait驱动程序 = webdriver.Firefox()<定义每个类在这里用硒等做什么># 定义我每天上的课def Day1():班级()班级()班级()班级()班级()班级()def Day2():班级()班级()班级()班级()班级()班级()def Day3():班级()班级()班级()班级()班级()班级()def Day4():班级()班级()班级()班级()班级()班级()def Day5():班级()班级()班级()班级()班级()班级()def Day6():班级()班级()班级()班级()班级()班级()

每个class 只是我每天的不同 课程.有些是相同的,有些是不同的,但对于这个问题来说并不重要.(对于这个问题,我刚刚将它们全部称为 class,因为它更容易)

我如何安排这些天(第 1 天 - 第 6 天),以便它们在周一至周五运行,然后轮换一天,然后在下周返回(如上所述)?

Python schedule 模块似乎可以做到它,但当我遇到这样的事情时,我什至不知道从哪里开始.

我觉得这将是一个非常简单的解决方案,但我似乎无法解决.

解决方案

您需要计算学期的哪一天,并将其修改为 6.一个很好的起点是这段代码,以获得当前年份中的当前日期:

from datetime import datetimeday_of_year = datetime.now().timetuple().tm_yday

您还想知道一年中的哪一天是开学的第一天.称其为 first_day_of_school.然后,假设学校在星期一开学:

day_of_school = day_of_year - first_day_of_schoolweek_of_school = day_of_school//7day_of_week = day_of_school % 7 # 0 表示星期一,欧式day_of_term = week_of_school * 5 + day_of_weekclass_index = day_of_term % 6

现在 class_index 是 [0, 5] 中的数字,告诉您这一天要参加哪一组课程.

您可能需要针对假期进行调整,例如如果周五假期意味着周五的课程不会被跳过而是转移到周一.为此,您需要适当的工作日算术,这可以使用 NumPy 或其他库轻松有效地完成,或者您可以硬编码假期列表并进行暴力操作,因为一个学期只有几个月的时间.

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.

解决方案

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.

这篇关于在python中安排5天的6天时间表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆