10行代码实现一个值班提醒应用

10行代码实现一个值班提醒应用

10行代码实现一个值班提醒应用

工作当中,经常遇到团队成员需要值班排班及提醒的应用场景。虽然,有一些现成的APP可以用,但是对程序员来讲使用起来不免有些繁琐,而且可能不太灵活。

那么,我们如何手工完成一个简单的值班安排和提醒应用程序呢?

[En]

So, how do we hand-do a simple application of duty scheduling and reminders?

10行代码实现一个值班提醒应用

考虑一个简单的情况,并对约束做出以下假设:

[En]

Consider a simple situation and make the following assumptions about constraints:

  • 职责令是固定的,一旦确定就不会修改。
    [En]

    the duty order is fixed and will not be modified once it is determined.*

  • 每个人都是固定时间轮班,例如,每个人相当于一周
    [En]

    each person is on a fixed-time shift, for example, each person is worth one week*

对需求假设如下:

  • 适时提醒值班人员值班
    [En]

    remind the person on duty of the duty at the right time*

  • 提醒值班人员,并通知下一名值班人员
    [En]

    remind the person on duty and give notice of the next person on duty*

约束和要求按如下方式转换为代码:

[En]

The constraints and requirements are translated into the code as follows:


on_duty_list = ['孙悟空', '沙和尚', '猪八戒', '白龙马'] # 值班人列表
on_duty_period_in_days = 7 # 每人值一周
notify_days = [1, 5] # 假设值班第一天提醒一次,第5天再提醒一次
第一个值班的人的开始时间<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>The start time of the first person on duty</font>*</details>
on_duty_first_person = '孙悟空'
on_duty_first_day = '2022-03-24 00:00:00'
提醒消息模板
notify_template = Template(r"""
【===========报警值班提醒========】
【本周值班人】${on_duty_person_this_week}
【值班时间】 ${on_duty_range}
【下周值班人】${on_duty_person_next_week}
""")

如何使用该程序计算当前应由谁值班?

[En]

How to use the program to calculate which person should be on duty at the current time?

这里使用简单的数学计算:整数除法和余数运算。

[En]

Simple mathematical calculations are used here: integral division and remainder operations.

将当前时间与第一个值班人员开始值班时间的时间差除以7天,得到经过了几周的时间,再取总值班人数的余额,得到当前值班人数。

[En]

The time difference between the current time and the start time of the first person on duty is divided by 7 days to get how many weeks have passed, and then take the balance of the total number of people on duty to get the current number of people on duty.

def WhoIsOnDuty(timeTs = int(time.time())):
    ref_timeTs = UnixTimeFromDate(on_duty_first_day)
    deltaTs = timeTs - ref_timeTs
    deltaDay = deltaTs / oneDayTs
    index = deltaDay / on_duty_period_in_days % len(on_duty_list)
    return on_duty_list[index]

值班时间范围如何计算?

[En]

How to calculate the duty time range?

这里涉及到简单的日期计算,使用了datetime库

def WhoIsOnDuty(timeTs = int(time.time())):
    ref_timeTs = UnixTimeFromDate(on_duty_first_day)
    deltaTs = timeTs - ref_timeTs
    deltaDay = deltaTs / oneDayTs
    index = deltaDay / on_duty_period_in_days % len(on_duty_list)
    day_index = deltaDay % on_duty_period_in_days
    ts = time.localtime(timeTs)
    start_day = str(datetime.date(ts.tm_year, ts.tm_mon, ts.tm_mday) + datetime.timedelta(days = -day_index))
    end_day = str(datetime.date(ts.tm_year, ts.tm_mon, ts.tm_mday) + datetime.timedelta(days = - day_index + on_duty_period_in_days - 1))
    return on_duty_list[index], day_index + 1, start_day, end_day, on_duty_list[(index+1)%len(on_duty_list)]

这10行代码,是整个应用的核心。

整个应用代码如下:

#!/usr/bin/env python
coding: utf-8

import time
from string import Template
import datetime

on_duty_list = ['孙悟空', '沙和尚', '猪八戒', '白龙马']
on_duty_period_in_days = 7 # day
notify_days = [1, 5]
on_duty_first_person = '孙悟空'
on_duty_first_day = '2022-03-24 10:00:00'
oneDayTs = 24 * 60 * 60

notify_template = Template(r"""
【===========报警值班提醒========】
【本周值班人】${on_duty_person_this_week}
【值班时间】 ${on_duty_range}
【下周值班人】${on_duty_person_next_week}
""")

def SendMessage(message='test'):
    print message

def PairListToMessage(pair_list):
    conent_arr = []
    for k, v in pair_list:
        conent_arr.append(' '.join(['【' + k + '】', v]))
    return '\n'.join(conent_arr)

def UnixTimeFromDate(time_str, format='%Y-%m-%d %H:%M:%S'):
    # 先转换为时间数组
    timeArray = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
    # 转换为时间戳
    timeStamp = int(time.mktime(timeArray))
    return timeStamp

def DatetimeStr(timeTs, format='%Y-%m-%d %H:%M:%S'):
    return time.strftime(format, time.localtime(timeTs))

def WhoIsOnDuty(timeTs = int(time.time())):
    ref_timeTs = UnixTimeFromDate(on_duty_first_day)
    deltaTs = timeTs - ref_timeTs
    deltaDay = deltaTs / oneDayTs
    index = deltaDay / on_duty_period_in_days % len(on_duty_list)
    day_index = deltaDay % on_duty_period_in_days
    ts = time.localtime(timeTs)
    start_day = str(datetime.date(ts.tm_year, ts.tm_mon, ts.tm_mday) + datetime.timedelta(days = -day_index))
    end_day = str(datetime.date(ts.tm_year, ts.tm_mon, ts.tm_mday) + datetime.timedelta(days = - day_index + on_duty_period_in_days - 1))
    return on_duty_list[index], day_index + 1, start_day, end_day, on_duty_list[(index+1)%len(on_duty_list)]

提醒本周和下周在周四和周一值班的人<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>Remind those on duty this week and next week on Thursdays and Mondays</font>*</details>
notify_counter = 0
on_duty_person_this_week = ''
While True:
    timeTs = int(time.time())
    timeStr = DatetimeStr(timeTs)
    on_duty_person, day, start_day, end_day, on_duty_person_next = WhoIsOnDuty(timeTs)
    if on_duty_person != on_duty_person_this_week:
        on_duty_person_this_week = on_duty_person
        notify_counter = 0
        SendMessage(notify_template.safe_substitute({
            'on_duty_person_this_week': on_duty_person,
            'on_duty_person_next_week': on_duty_person_next,
            'on_duty_range': '~'.join([start_day, end_day]),
        }))
        notify_counter += 1
    else:
        if notify_counter < 2 and day in notify_days:
            SendMessage(notify_template.safe_substitute({
                'on_duty_person_this_week': on_duty_person,
                'on_duty_person_next_week': on_duty_person_next,
                'on_duty_range': '~'.join([start_day, end_day]),
            }))
            notify_counter += 1
    time.sleep(1)

效果如下:

10行代码实现一个值班提醒应用

这是一个值得读者深思的问题。欢迎您在评论区交流。

[En]

Here is a thinking question for readers. You are welcome to communicate in the comment area.

  • 如何实现临时换班功能?
    [En]

    how to realize the function of temporary shift transfer?
  • 调用第三方API实现如公众号提醒、短信提醒等

如果觉得文章对你有些帮助,可以在微信公众号关注:小而美实用Python,获取更多新鲜文章。

Original: https://www.cnblogs.com/jianxinjun/p/16073971.html
Author: 简信君
Title: 10行代码实现一个值班提醒应用

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/500182/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球