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)
Answer
Try to loop until your check evaluates True:
import time
interval = 0.2 # nr of seconds
while True:stop_looping = myAlertCheck()if stop_looping:breaktime.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 datetimeinterval = 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).secondsif delta_s < (15 * 60):print 'within 15 minutes %s starts' % titlereturn Truewhile True:stop_looping = myAlertCheck(events)if stop_looping:breaktime.sleep(interval)
Im new to Python and I thought Id try to learn the ropes a bit by writing a function to find the nth prime number, however I cant get my code to work properly. No doubt this is due to me missing someth…
I have an unordered list, lets say:lst = [12,23,35,54,43,29,65]and the program will prompt the user to input two numbers, where these two numbers will represent the range.input1 = 22input2 = 55therefor…
I have noticed an issue with using the tag add command of a ttk.Treeview widget when activated with the tk.call() method. That is, it cant handle white space in the value of the str() elements of its i…
I have asked a question before about this bit of code and it was answered adequately, but I have an additional question about showing the ten most overdue numbers. (This program was a part of an in-cla…
I am not an expert in web programming and know very little about it. I am trying to run a webservice on an EC2 instance (Windows Server 2012R2) and the webservice is written in Python using Flask packa…
This question already has answers here:Why is int" not iterable in Python, but str are?(4 answers)Closed 2 years ago.As i know we can not iterate int value while we can iterate strings in python.…
i create two identical tuples and use is operator on them the answer that should come is false but when i use it in vscode/atom/notepadd++ it comes true but when i use the same code in pthon run throug…
Taking a class on Python coding and trying to use inheritance to code an answer to this problem: Write an Employee class that keeps data attributes for the following piece of information:
Employee name…
Thanks for everyones reply. I will explain here.
Suppose there is a given matrixx y B = [5,-4,5,-6]
[[0,0,0,0], [[0,1,0,1],[0,0,0,0], [0,0,0,0],[0,0,0,0],…