python-o365 icon indicating copy to clipboard operation
python-o365 copied to clipboard

Question: get total number of events from get_events()

Open misilot opened this issue 3 years ago • 4 comments

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!

misilot avatar Oct 18 '22 22:10 misilot

len(list(events)) ?

alejcas avatar Oct 19 '22 12:10 alejcas

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!

misilot avatar Oct 19 '22 13:10 misilot

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()
    )

misilot avatar Oct 19 '22 13:10 misilot

enumerate the generator and you will get the total number at the end

alejcas avatar Oct 27 '22 07:10 alejcas