I was experimenting with a google Calendar API for python, and I want to check every five seconds for a new event addition to the calendar. I know to set it as an old list and then compare it again when it updates like this:
-main()
is the function that retrieves data from Calendar.
- Allevents
is a list of events from the calendar. It has events appended to it within main()
-Other names are pretty intuitive.
while 1 == 1:
time.sleep(5)
main()
if first == 0:
m = set(allevents) - set(oldevents)
print(oldevents)
if not m:
print('nothing changed')
else:
print(m)
print("--------------------------------------------------------")
else:
oldevents = allevents
first = False
oldevents = allevents
allevents.clear()
When I run this, first time it retrieves data goes perfectly: outputs allevents
(as written in main()
) and nothing else. The second time is when I haven't made any changes yet, so it outputs allevents
and oldevents
(which have the same values) along with a nothing changed
message.
The third time the while loop runs is when I add an event to the calendar. In theory, I want it to output the event that I changed because of the
m = set(allevents) - set(oldevents)
print(oldevents)
if not m:
print('nothing changed')
else:
print(m)
print("--------------------------------------------------------")
However, it follows if not m:
and outputs allevents
and oldevents
with the same values and the new event that I added and a nothing changed
message.
Side note: when I delete that event on the next loop, both lists remove that event as well.
The four loops(with some modifications for presentation purposes):
Getting the upcoming 10 events
['5kt8vb1m5tad2h6higtd8seqp9']
Getting the upcoming 10 events
['5kt8vb1m5tad2h6higtd8seqp9']
['5kt8vb1m5tad2h6higtd8seqp9']
nothing changed
Getting the upcoming 10 events
['7p4hs2ribddnl5h13erv5c1rir', '5kt8vb1m5tad2h6higtd8seqp9']
['7p4hs2ribddnl5h13erv5c1rir', '5kt8vb1m5tad2h6higtd8seqp9']
nothing changed
Getting the upcoming 10 events
['5kt8vb1m5tad2h6higtd8seqp9']
['5kt8vb1m5tad2h6higtd8seqp9']
nothing changed
If you think that more of the code is necessary, tell me and I will post the entire thing.