Skip to content

Instantly share code, notes, and snippets.

@sodre
Created November 30, 2015 13:15
Show Gist options
  • Save sodre/cb0a0fb38623ad8f00ef to your computer and use it in GitHub Desktop.
Save sodre/cb0a0fb38623ad8f00ef to your computer and use it in GitHub Desktop.
The start of a cartesian job runner.
def partition(start, end, step):
'''
Partitions the interval [start, end) into 'k' intervals [a_i, a_{i+1}).
'''
from itertools import takewhile, chain, islice, tee
# itertools.count does not work with datetime.
def count(firstval=0, step=1):
x = firstval
while 1:
yield x
x += step
assert(start_date < end_date)
a_i = takewhile(lambda x: x < end, count(start, step))
a_i = tee(a_i)
a_ip1 = chain(islice(a_i[0], 1, None), [end])
return zip(a_i[1], a_ip1)
@sodre
Copy link
Author

sodre commented Nov 30, 2015

Usage

from itertools import product
from datetime import datetime, timedelta

start_date = datetime.strptime(input(prompt='Start Date YYYY/MM/DD: '), '%Y/%m/%d')
end_date = datetime.strptime(input(prompt='End Date YYYY/MM/DD: '), '%Y/%m/%d')

with open(selectors_filepath, 'r') as selectors:
    intervals = partition(start_date, end_date, 
                           timedelta(days=60))
    for (interval, sel) in product(intervals, selectors):
        print('{}: [{}, {})'.format(sel, *interval))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment