python - 如何更改此代码,使其跳过周末但从星期二开始

标签 python python-3.x schedule

previous question中我的问题是,我问如何在 python 中执行以下操作(这是一个摘要):

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

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

这就是我得到的答案,这正是我所需要的,但是,它仅在 first_day_of_school 是星期一时才有效。

from datetime import datetime

day_of_year = datetime.now().timetuple().tm_yday
first_day_of_school = datetime(2020, 9, 15).timetuple().tm_yday
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
day_of_term = week_of_school * 5 + day_of_week
class_index = day_of_term % 6
print(class_index)

我想知道的是,如果 first_day_of_school 是星期二,我该如何正确修改它,使其以相同的方式工作?

我知道 //7* 5 部分是在星期一跳过周末的方式,但我无法计算出数学公式改天再上类吧。在过去的 12 个小时里我尝试了很多不同的组合,但还是无法解决。我真的希望答案不只是盯着我看,因为这真的让我很沮丧。

我已经尝试过前后解决这个问题,但没有任何效果。 Python 的这个方面对我来说非常新。

最佳答案

我认为timedelta就是您正在寻找的东西

import datetime

day_of_year = datetime.datetime.now().timetuple().tm_yday

skip_days = 2
delta = datetime.timedelta(days=skip_days)
first_day_of_school = datetime.datetime(2020, 9, 15) + delta

first_day_of_school = first_day_of_school.timetuple().tm_yday
day_of_school = day_of_year - first_day_of_school
week_of_school, day_of_week = divmod(day_of_school, 7) 
day_of_term = week_of_school * 5 + day_of_week
class_index = day_of_term % 6
print(class_index)

关于python - 如何更改此代码,使其跳过周末但从星期二开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64084277/

相关文章:

python - Django 初始化

python-3.x - 无法分配形状为 (1482535, 67826) 且数据类型为 int64 的数组

Python 3.x 请求使用 unicode 字符进行重定向

sql - 如何从 Postgres 的预订中找到第一个免费开始时间

python - 使用 minidom 解析 Bash 导出变量,Python 出错

python - 如何在 Scikit-Image 中使用 View block ?

ruby-on-rails - 你会如何建立这个每日类(class)表?

wordpress - 像WordPress这样的计划帖子是如何工作的,它是一个cron?

Python Flask Jinja2 模板 null 错误

python - 如何将包含字符串和数字的值列表写入文本文件