views:

439

answers:

2

I'm using the python dateutil module for a calendaring application which supports repeating events. I really like the ability to parse ical rrules using the rrulestr() function. Also, using rrule.between() to get dates within a given interval is very fast.

However, as soon as I try doing any other operations (ie: list slices, before(), after(),...) everything begins to crawl. It seems like dateutil tries to calculate every date even if all I want is to get the last date with rrule.before(datetime.max).

Is there any way of avoiding these unnecessary calculations?

A: 

This module is completely new to me, so thanks for that!

That said, does it help if you pass cache=True into your rrule or rruleset constructors, as documented here?

hanksims
cache = True makes subsequent queries instant, but it still requires that all dates be calculated at least once
pklall
+1  A: 

My guess is probably not. The last date before datetime.max means you have to calculate all the recurrences up until datetime.max, and that will reasonably be a LOT of recurrences. It might be possible to add shortcuts for some of the simpler recurrences. If it is every year on the same date for example, you don't really need to compute the recurrences inbetween, for example. But if you have every third something you must, for example, and also if you have a maximum recurrences, etc. But I guess dateutil doesn't have these shortcuts. It would probably be quite complex to implement reliably.

May I ask why you need to find the last recurrence before datetime.max? It is, after all, almost eight thousand years into the future... :-)

Lennart Regebro
I'm storing events in a database and would like to query for events between certain dates (ie: beginning and end of the month). If I can store the first and last dates, I could eliminate all events which do not "overlap" with a given time interval reducing the number of events for which I have to call rrule.between().
pklall
On second thought, I think adding a generous maximum "count" to each rrule would fix the problem.
pklall
I usually fix this by a generous max date, like 20 years in the future. 8000 years seems like overkill. Also you can have special handling for dates that doesn't have a count or end date, since you know that by definition these will never end, so they *will* overlap. :)
Lennart Regebro