10 行代码实现一个值班揭示利用
工作当中,常常遇到团队成员须要值班排班及揭示的利用场景。尽管,有一些现成的 APP 能够用,然而对程序员来讲应用起来不免有些繁琐,而且可能不太灵便。
那么,咱们如何手撸一个简略值班排班及揭示的利用呢?
思考一种简略的状况,对束缚做如下假如:
- 值班程序固定,一旦确定下来不再批改
- 每人值固定工夫的班,比方:每人值一周
对需要假如如下:
- 在适合的工夫,揭示值班人值班工夫
- 揭示值班人的同时预报下一个值班人
束缚和需要转化为代码如下:
on_duty_list = ['孙悟空', '沙和尚', '猪八戒', '白龙马'] # 值班人列表
on_duty_period_in_days = 7 # 每人值一周
notify_days = [1, 5] # 假如值班第一天揭示一次,第 5 天再揭示一次
# 第一个值班人值班开始工夫
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}
""")
如何用程序来计算以后工夫应该是哪个人值班呢?
这里用到简略数学计算:整除、取余运算
以后工夫与首个值班人值班开始工夫的时间差整除 7 天失去过了多少周,而后再对总值班人数取余,失去以后应该是第几个值班人值班。
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]
如何计算值班工夫范畴呢?
这里波及到简略的日期计算,应用了 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)]
# 周四、每周一揭示本周值班人员,下周值班人员
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)
成果如下:
这里留个思考题给读者,欢送评论区交换。
- 如何实现长期调班的性能?
- 调用第三方 API 实现如公众号揭示、短信揭示等
如果感觉文章对你有些帮忙,能够在微信公众号关注:小而美实用 Python,获取更多陈腐文章。