成都創(chuàng)新互聯(lián)從2013年成立,先為盂縣等服務(wù)建站,盂縣等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為盂縣企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
工作當(dāng)中,經(jīng)常遇到團(tuán)隊(duì)成員需要值班排班及提醒的應(yīng)用場景。雖然,有一些現(xiàn)成的APP可以用,但是對程序員來講使用起來不免有些繁瑣,而且可能不太靈活。
那么,我們?nèi)绾问謹(jǐn)]一個簡單值班排班及提醒的應(yīng)用呢?
考慮一種簡單的情況,對約束做如下假設(shè):
對需求假設(shè)如下:
約束和需求轉(zhuǎn)化為代碼如下:
on_duty_list = ['孫悟空', '沙和尚', '豬八戒', '白龍馬'] # 值班人列表
on_duty_period_in_days = 7 # 每人值一周
notify_days = [1, 5] # 假設(shè)值班第一天提醒一次,第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}
""")
如何用程序來計算當(dāng)前時間應(yīng)該是哪個人值班呢?
這里用到簡單數(shù)學(xué)計算:整除、取余運(yùn)算
當(dāng)前時間與首個值班人值班開始時間的時間差整除7天得到過了多少周,然后再對總值班人數(shù)取余,得到當(dāng)前應(yīng)該是第幾個值班人值班。
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行代碼,是整個應(yīng)用的核心。
整個應(yīng)用代碼如下:
#!/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'):
# 先轉(zhuǎn)換為時間數(shù)組
timeArray = time.strptime(time_str, "%Y-%m-%d %H:%M:%S")
# 轉(zhuǎn)換為時間戳
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)
效果如下:
這里留個思考題給讀者,歡迎評論區(qū)交流。
如果覺得文章對你有些幫助,可以在微信公眾號關(guān)注:小而美實(shí)用Python,獲取更多新鮮文章。