0

It's easy to get the nth occurrence of a dateutil.rrule.rrule instance:

rule = dateutil.rrule.rrule(...)
occurrence = rule[15]

...But I have an occurrence such that occurrence in rule is True; I would like to know which occurrence it is (i.e. the index of the occurrence, which is 15 in the above example) without causing performance problems, as I may need to do this with a lot of occurrences.

Is this possible, and if so, how would it be done?

Adam
  • 3,668
  • 6
  • 30
  • 55

1 Answers1

0

I am not aware of a method that would allow you to run a test on values with the current set of functions.

However, depending of how frequently you perform your calculations, you might consider doing the followings:

  1. Use the cache feature:

cache – If given, it must be a boolean value specifying to enable or disable caching of results. If you will use the same rrule instance multiple times, enabling caching will improve the performance considerably.

  1. Remap your results onto a numpy array for optimum index resolution:

Given r being a large set of results:

>>> r # Your result set
<dateutil.rrule.rrule object at 0x7f616d4182e8>
>>> r[1] # Example of what's inside
datetime.datetime(2015, 1, 1, 0, 0)
>>> import numpy as np # Let's load numpy
>>> a = np.array(list(r)) # We build a numpy array, should be fast
>>> np.argwhere(a == datetime.datetime(2015, 1, 1, 0, 0)) # Catch'em all!
array([[1]])

I am unsure if np.argwhere() will be faster than iterating over the result set with a simple loop, but this is a more readable approach.

Fabien
  • 4,862
  • 2
  • 19
  • 33