python-o365
python-o365 copied to clipboard
Question: get total number of events from get_events()
Is there a way to get the total number of events that come back from a query like
schedule = account.schedule()
calendar = schedule.get_default_calendar()
q = calendar.new_query('start').greater_equal(start_time)
q.chain('and').on_attribute('end').less_equal(end_time)
events = calendar.get_events(query=q, include_recurring=True)
Looking for an easy way to determine if events is empty / how many events there are? Most of the time we should only get 1 event, but if we get > 1, somehow we ended up with overlapping events, and we would need to look at those.
Thanks!
len(list(events)) ?
Thank you!
I swear I had tried that and it was throwing an error, but it might be another part of the code that is erroring out :-/
Sorry for the noise!
So, I guess it there a better way to do this? Since the generator gets exhausted? Or do I need to do two get_events()'s so I can the total number of events, and be able to iterate over it if there are any events?
events = calendar.get_events(limit=1, query=q, include_recurring=True)
print(len(list(events))) # This exhausts the generator, so the following errors out
for event in events:
libcal = re.search(r"(Created by LibCal\.)", event.get_body_text())
return (
event.subject,
libcal,
# event.to_api_data()
)
enumerate the generator and you will get the total number at the end