1

I need to query an API for data over a multi-month period. However, the API chokes on longer than 3-day intervals.

So I want to create a generator function to separate my multi-month date range into 3 day segments that I can use while repeatedly calling my function that hits the API: When I pass it the start date and end date, it gives me:

  • the first time gives me start date,start date +3 days
  • next time gives me start date + 3 days, start date + 6 days
  • each time thereafter it moves forward by 3 day increments
  • until it hits the end date, when it gives me the days remaining to hit the end date if I still have 1 or 2 days left of data to grab
  • stops

Here's my code so far. It will work for the first time, but I'm not sure how to get the start date to increment by 3 days the next time I call the function. And I'm also not sure how if I still have 1 or 2 days left before hitting the final end date to set my until variable to the final end date--I think right now it simply says "there's less than 3 days left until the final date, so let's quit":

3_day_segmenter(start, end):
    start_date = datetime.strptime(start, '%Y-%m-%d')
    end_date = datetime.strptime(end, '%Y-%m-%d')
    since = start_date
    for date in range(int ((end_date - start_date).days)): 
        until = start_date + datetime.timedelta(days=3)     
        yield since, until
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Jeff Widman
  • 22,014
  • 12
  • 72
  • 88
  • possible duplicate of [Generate a list of datetimes between an interval in python](http://stackoverflow.com/questions/10688006/generate-a-list-of-datetimes-between-an-interval-in-python) – Martijn Pieters Oct 10 '13 at 22:22
  • That answer provides you with a generator that produces dates by a `timedelta` step size; it's trivial to turn that into something that also produces an end date. – Martijn Pieters Oct 10 '13 at 22:24

1 Answers1

5

Adapting Generate a list of datetimes between an interval to your needs:

from datetime import date, timedelta

def perdelta(start, end, delta):
    curr = start
    while curr < end:
        yield curr, min(curr + delta, end)
        curr += delta

This yields a (date1, date2) tuple that is constrained to the end date for the last segment:

>>> for s, e in perdelta(date(2011, 10, 10), date(2011, 11, 10), timedelta(days=3)):
...     print s, e
... 
2011-10-10 2011-10-13
2011-10-13 2011-10-16
2011-10-16 2011-10-19
2011-10-19 2011-10-22
2011-10-22 2011-10-25
2011-10-25 2011-10-28
2011-10-28 2011-10-31
2011-10-31 2011-11-03
2011-11-03 2011-11-06
2011-11-06 2011-11-09
2011-11-09 2011-11-10

Note that the last result covers only 2 days.

Community
  • 1
  • 1
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343