Python - 当事件接近时,做(某事) [英] Python - Do (something) when event is near

查看:265
本文介绍了Python - 当事件接近时,做(某事)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我们的游戏社区创建一个事件通知系统。但是,我并不完全确定从哪里开始。



我有一个事件及其时间的字典,例如:

  {'游戏之夜':19:00 2013-06-29,
'3CB vs ST3 Match':18:45 2013-07-02,
'网站维护':13:00 2013-07-16,
etc}

时间已经使用strptime()转换为正确的datetime格式。我现在需要的是,当这些事件中的一个即将发生时(例如15分钟警报,然后是5分钟警报),用户可以收到通知。



例如:

注意:3CB vs ST3比赛将在15分钟内开始! 
10分钟后...
注意:3CB vs ST3比赛将在5分钟内开始!

我的问题是这个:
如何让python等到一个事件接近(通过比较当前时间和事件的时间),然后执行一个动作(例如我的情况下的通知)?



PS
我使用Python 2.7.5(由于缺少API更新)

解决方案

尝试循环直到您的检查评估为True:

 导入时间
interval = 0.2#nr秒
while True:
stop_looping = myAlertCheck()
if stop_looping:
break
time.sleep(interval)
/ pre>

睡眠为您提供其他任务的CPU时间。



编辑



好的,我不知道你的问题究竟是什么。首先我以为你想知道如何让python'等待'一个事件。现在,您似乎想知道如何将活动日期与当前日期进行比较。
我认为以下是一个更完整的方法。我想你可以自己填写细节?

  import time 
from datetime import datetime

interval = 3#nr of seconds
events = {
'游戏之夜':'14:00 2013-06-23',
'3CB vs ST3 Match':'18 :45 2013-07-02',
'网站维护':'13:00 2013-07-16',
}

def myAlertCheck(events):
标题,event_date in events.iteritems():
ed = datetime.strptime(event_date,'%H:%M%Y-%m-%d')
delta_s =(datetime。 now() - ed).seconds
如果delta_s < (15 * 60):
print'在15分钟内%s开始'%title
return True

while True:
stop_looping = myAlertCheck(events)
如果stop_looping:
break
time.sleep(间隔)


I'm creating an event notification system for our gaming community. However, I'm not entirely sure where to start.

I have a dictionary of events and their times, for example:

{'Game Night': 19:00 2013-06-29,
'3CB vs ST3 Match': 18:45 2013-07-02,
'Website Maintainance': 13:00 2013-07-16,
etc}

The times have already been converted to the correct datetime format using strptime(). What I now need is for the user to be notified when one of these events is about to occur (e.g a 15 minute alert, then a 5 minute alert).

For example:

"NOTICE: 3CB vs ST3 Match will begin in 15 minutes!"
10 minutes later...
"NOTICE: 3CB vs ST3 Match will begin in 5 minutes!"

My question is this: How can I get python to wait until an event is near (by comparing the current time, with the event's time), then perform an action (e.g. a notification in my case)?

P.S I'm using Python 2.7.5 (due to lack of API updates)

解决方案

Try to loop until your check evaluates True:

import time
interval = 0.2  # nr of seconds
while True:
    stop_looping = myAlertCheck()
    if stop_looping:
        break
    time.sleep(interval)

The sleep gives you CPU time for other tasks.

EDIT

Ok, I'm not sure what exactly your question is. First I thought you wanted to know how to let python 'wait' for an event. Now it seems you want to know how to compare event dates with the current date. I think the following is a more complete approach. I think you can fill in the details yourself??

import time
from datetime import datetime

interval = 3  # nr of seconds    
events = {
    'Game Night': '14:00 2013-06-23',
    '3CB vs ST3 Match': '18:45 2013-07-02',
    'Website Maintainance': '13:00 2013-07-16', 
}

def myAlertCheck(events):
    for title, event_date in events.iteritems():
        ed = datetime.strptime(event_date, '%H:%M %Y-%m-%d')
        delta_s = (datetime.now() - ed).seconds
        if delta_s < (15 * 60):
            print 'within 15 minutes %s starts' % title
            return True

while True:
    stop_looping = myAlertCheck(events)
    if stop_looping:
        break
    time.sleep(interval)

这篇关于Python - 当事件接近时,做(某事)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆