私はPythonでtime_slotのアプリケーションを書いています
このコードを見てください
from datetime import datetime ,timedelta
appointments = [(datetime(2012, 5, 22, 10), datetime(2012, 5, 22, 10, 30)),
(datetime(2012, 5, 22, 12), datetime(2012, 5, 22, 13)),
(datetime(2012, 5, 22, 15, 30), datetime(2012, 5, 22, 17, 10))]
hours = (datetime(2012, 5, 22, 9), datetime(2012, 5, 24, 18))
duration = timedelta(minutes = 120)
def get_slots(hours, appointments,duration):
slots = sorted([(hours[0], hours[0])] + appointments + [(hours[1], hours[1])])
for start, end in ((slots[i][1], slots[i+1][0]) for i in range(len(slots)-1)):
assert start <= end, "Cannot attend all appointments"
while start + duration <= end:
dt_obj = start
date_str = dt_obj.strftime("%Y-%m-%d %H:%M:%S")
today18 = start.replace(hour=17, minute=59, second=59, microsecond=0)
if start < today18:
print "{:%d:%H:%M} - {:%d:%H:%M}".format(start, start + duration)
start += duration
else:
start += timedelta(hours = 15)
if __name__ == "__main__":
get_slots(hours, appointments,duration)
上記のコードは、python time_slots.pyのように実行しているときに、正常に機能しています。
しかし、djanogビューで上記のコードを使用している場合
from datetime importdatetime ,timedelta
duration = timedelta(minutes = 60)
appointments = [(datetime(2012, 5, 22, 10), datetime(2012, 5, 22, 10, 30)),
(datetime(2012, 5, 22, 12), datetime(2012, 5, 22, 13)),
(datetime(2012, 5, 22, 15, 30), datetime(2012, 5, 22, 17, 10))]
# hours = (datetime.datetime(2012, 5, 24, 18), datetime.datetime(2012, 5, 22, 9))
hours = (datetime(2012, 5, 22, 9), datetime(2012, 5, 24, 18))
slots = sorted([(hours[0], hours[0])] + appointments + [(hours[1], hours[1])])
for start, end in ((slots[i][1], slots[i+1][0]) for i in range(len(slots)-1)):
assert start <= end, "Cannot attend all appointments"
while start + duration <= end:
today18 = start.replace(hour=17, minute=59, second=59, microsecond=0)
if start < today18:
print "{:%H:%M} - {:%H:%M}".format(start, start + duration)
else:
start += timedelta(hours = 15)
このビューを呼び出すと、コンソールに無限ループが返されます
ここで私が間違っていることを助けてください私はtime_slots.pyで取得しているので利用可能なタイムスロットが欲しいです