Skip to content

Instantly share code, notes, and snippets.

@radiosilence
Created May 13, 2013 16:57
Show Gist options
  • Save radiosilence/5569771 to your computer and use it in GitHub Desktop.
Save radiosilence/5569771 to your computer and use it in GitHub Desktop.
import datetime
total = 900
blocks = [(datetime.datetime(*start), datetime.datetime(*end), q) for start, end, q in [
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2012,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2013,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2012,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2013,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2012,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2013,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2012,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2013,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2012,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2013,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2012,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2013,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2012,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2013,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2012,01,12,10,32), 1),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,02,8,00), (2012,01,10,10,32), 3),
((2012,01,01,8,00), (2013,01,12,10,32), 1),
]]
allocated = []
def get_graph(total, allocated):
pass
def get_avail_at_point(total, allocated, point):
avail = total
for start, end, quantity in allocated:
if start < point and end > point:
avail -= quantity
return avail
def try_add(total, allocated, start, end, quantity):
granularity = datetime.timedelta(minutes=30)
x = start
while x <= end:
avail = get_avail_at_point(total, allocated, x)
if avail < quantity:
raise Exception("Cannot add because availability is {} at {}".format(
avail, x))
x += granularity
allocated.append((start, end, quantity))
for block in blocks:
try_add(total, allocated, *block)
print allocated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment