0

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.

Sceptual
  • 53
  • 7
  • 1
    `oldevents = allevents` then `allevents.clear()`, both oldevents and allevents are references to the same object, both will get cleared. make a copy instead, `oldevents = allevents.copy()` .[Good further read](https://nedbatchelder.com/text/names.html) – Paritosh Singh Apr 07 '19 at 16:48
  • @Paritosh Sight thank you, I've been working at it for about two hours. Guess I need to read that link you sent me thoroughly. – Sceptual Apr 07 '19 at 16:59
  • Possible duplicate of [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – Paritosh Singh Apr 07 '19 at 17:10
  • No worries, its a common gotcha. Marking question as dupe for "house keeping". Check the dupe target too for some different ways to get similar results. – Paritosh Singh Apr 07 '19 at 17:11

1 Answers1

0

in your code "first = False" sets first to zero. So it enters the if loop in next iteration.Remove "first = False" and try

Bharath
  • 113
  • 1
  • 1
  • 10