Python - finding time slots

2024/10/12 12:26:58

I am writing a small Python script to find time available slots based off calendar appointments. I was able to reuse the code on the post here: (Python - Algorithm find time slots).

It does seem to work for booked appointments an hour or longer, but for those less than an hour it doesn't seem to catch them. In other words, it shows time slots as available even though appointments are booked (less than an hou).

Sample code below from post mentioned, with my own values for "hours" and "appointments".

#get_timeslots.pyfrom datetime import datetime, timedeltaappointments = [(datetime.datetime(2017, 9, 7, 9, 30), datetime.datetime(2017, 9, 7, 12, 30), datetime.datetime(2017, 9, 7, 13, 30), datetime.datetime(2017, 9, 7, 14, 0))]hours = (datetime.datetime(2017, 9, 7, 6, 0), datetime.datetime(2017, 9, 7, 23, 0))def get_slots(hours, appointments, duration=timedelta(hours=1)):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:print "{:%H:%M} - {:%H:%M}".format(start, start + duration)start += durationif __name__ == "__main__":get_slots(hours, appointments)

When I run the script, I get:

06:00 - 07:00
07:00 - 08:00
08:00 - 09:00
12:30 - 13:30
13:30 - 14:30
14:30 - 15:30
15:30 - 16:30
16:30 - 17:30
17:30 - 18:30
18:30 - 19:30
19:30 - 20:30
20:30 - 21:30
21:30 - 22:30

The issue is that while the first appointment from 9:30-12:30 was blocked out and doesn't appear in available slots, the later 13:30-2:00 appointment was not blocked and therefore shows as available in the time slots output. (see "13:30 - 14:30").

I am a Python newbie and admit I recycled the code without fully understanding it. Can someone point me to what to change to make it properly block out the appointments less than hour?

TIA,

-Chris

Answer

You missed the brackets in the appointments. Try this:

#from datetime import datetime, timedelta
import datetime#notice the additional brackets to keep the 2 slots as two separate lists. So, 930-1230 is one slot, 1330-1400 is an another.appointments = [(datetime.datetime(2017, 9, 7, 9, 30), datetime.datetime(2017, 9, 7, 12, 30)), (datetime.datetime(2017, 9, 7, 13, 30), datetime.datetime(2017, 9, 7, 14, 0))]
https://en.xdnf.cn/q/118197.html

Related Q&A

ReportLab - error when creating a table

This is the first time Ive used ReportLab, I have tried to edit an existing script that does exactly what I want to do, but I get the following error, when I try and run the script.Script - import os, …

Secure login with Python credentials from user database

I like to create a secure login with Python but need to check the user table from a database, so that multiple users can log in with their own password. Mainly like this, works like a charm but not sec…

count number of names in list in python [duplicate]

This question already has answers here:How to count the frequency of the elements in an unordered list? [duplicate](33 answers)Closed 6 years ago.i have one list wich has names in it:names = [test,hal…

tensorflow logits and labels must be same size

Im quite new to tensorflow and python, and currently trying to modify the MNIST for expert tutorial for a 240x320x3 image. I have 2 .py scripttfrecord_reeader.pyimport tensorflow as tf import numpy as…

How to call an action when a button is clicked in Tkinter

I am experimenting with Tkinter for the first time, and am trying to call a function when a button is clicked. This is part of my code. mt is referring to a label that I have made dynamic by attachin…

Access range of elements from an array Python

Considering the following dataset:>>> data[:10] array([(T, 2, 8, 3, 5, 1, 8, 13, 0, 6, 6, 10, 8, 0, 8, 0, 8),(I, 5, 12, 3, 7, 2, 10, 5, 5, 4, 13, 3, 9, 2, 8, 4, 10),(D, 4, 11, …

Python - Remove extended ascii

Okay, so I am new to the whole python world so bear with me. Background: We are trying to offload logs into mongo to be able to query and search for them quicker. The device already prints them in a de…

Selenium - Python - Select dropdown meun option - No ID or Name

I am trying to select and element in a dropdown menu:The HTML is:<div class="col-lg-6"><select data-bind="options: indicator_type_list,value: indicatorType,optionsCaption: Choos…

How to prevent triples from getting mixed up while uploading to Dydra programmatically?

I am trying to upload some data to Dydra from a Sesame triplestore I have on my computer. While the download from Sesame works fine, the triples get mixed up (the s-p-o relationships change as the obje…

Adding a new row to a dataframe in pandas for every iteration

Adding a new row to a dataframe with correct mapping in pandasSomething similar to the above question.carrier_plan_identifier ... hios_issuer_identifier 1 AU…