V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
proxytoworld
V2EX  ›  问与答

[ Python ] 使用 Python 动态解析如下格式的值班表文件并定点提醒如何实现

  •  
  •   proxytoworld · 2022-10-24 14:46:55 +08:00 · 364 次点击
    这是一个创建于 568 天前的主题,其中的信息可能已经有所发展或是发生改变。

    需求:目前有一个值班,分为 3 班,计划做成如下文件

    2022-10-24,a,b,c #a b c 代表早中晚班的人名
    2022-10-21,c,y,d
    2022-10-22,c,d,ra
    2022-10-23,c,d,rs
    2022-10-24,c,d,rs
    2022-10-25,c,d,rs
    
    

    使用 python 解析文件,定点调用 api 发送明天的值班人,因为一次只能出两个礼拜的值班表,所以值班表文件每两个周要变动一次,计划在值班表修改的时候能够重新读取值班表,让程序可以通过新值班表定点发送。

    目前的想法是起一个线程,用 watchdog 监控文件修改,在 on_modified 触发时调用解析函数 Reminder.callback()重新解析配置文件,使得新值班表进入变量内

    在 callback 中读取配置文件并解析,通过 datetime.today()获取当前时间,如果当前时间减去设置好的时间 +6h +12h +18h 则调用Reminder.send()发送消息

    但目前因为要持续获取当前时间,设置了死循环,导致文件修改时触发回调没用,死循环所在的线程还在运行。 有什么好办法可以解决吗

    class MonitorHandler(FileSystemEventHandler):
        def __init__(self) -> None:
            super().__init__()
        ...
        def on_modified(self, event):
            print("on_modified", event.src_path)
            # print(event.src_path[-len(Config.monitoring_file):])
            if Config.monitoring_file == event.src_path[-len(Config.monitoring_file):]:
                print("reload")
                self.reload_file()
       ...
        def reload_file(self):
            Reminder.callback()
    
    

    Reminder 类:

    
        def callback():
            with open(Config.monitoring_directory+'/'+Config.monitoring_file, 'r') as f:
                lines = f.readlines()
                dates = []
                dutys = []
                for line in lines:
                    l = line.strip('\n').split(',')
                    dates.append(l[0])
                    dutys.append(l[1:])
                # print(dutys)
                # print()
                while True:
                    today = datetime.today()
                    zeroToday = today - timedelta(
                        hours=today.hour, minutes=today.minute, seconds=today.second, microseconds=today.microsecond)
                    tomorrow = today + timedelta(days=1)
    
                    if str(tomorrow)[:10] in dates:
                        # 取出明天的值班人,和 index
                        index = dates.index(str(tomorrow)[:10])
                        time_index = 0
                        now = datetime.today()
                        while(str(now)[:10] == str(today)[:10]):
                            now = datetime.today()
                            gap = now - zeroToday
                            gap_list = str(gap).split(':')
                            if int(gap_list[0]) == Config.notify_time[time_index]:
                                print("yes")
                                time_index += 1
                            else:
                                print("gap:", gap_list[0],
                                      str(Config.notify_time[time_index]))
    
                            time.sleep(1)
    
                    else:
                        # Reminder.send("暂无" + str(tomorrow)[:10])
                        break
    
    2 条回复    2022-10-24 15:34:08 +08:00
    geeglo
        1
    geeglo  
       2022-10-24 15:11:00 +08:00
    一个 crontab 就搞定的事情,搞这么复杂干啥。
    proxytoworld
        2
    proxytoworld  
    OP
       2022-10-24 15:34:08 +08:00
    @geeglo 有道理。。是我想多了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5223 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 03:57 · PVG 11:57 · LAX 20:57 · JFK 23:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.