-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschedule.py
331 lines (283 loc) · 10.5 KB
/
schedule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import csv
import math
#Josiah Grace
#O-Week 2014 Scheduling
#2/11/14 - 2/12/14
"""
Interview Scheduler
(Optional)
Access the Google Docs API in order to download the CSV
files from the duncaroos@gmail folder.
a. Transform from CSV into acceptable interview format
b. Accept different interview blocks for coordinators
(e.g. 15 minutes w/ 5 minute breaks)
c. Turn candidate preferences into acceptable interview
times
(Optional)
Turn back into Google Doc spreadsheet
"""
# (date) : [(start, end),(start, end)]
# TODO: remove hard-coded form of coord_avail
coord_avail = {
('Fri 14th') : [(15.0, 18.0)],
('Sat 15th') : [(10.0, 19.0)],
('Sun 16th') : [(10.0, 14,0), (19.0, 21.0)],
('Fri 21st') : [(15.0, 20.0)],
('Sat 22nd') : [(10.0, 19.0)],
('Sun 23rd') : [(10.0, 14,0), (19.0, 21.0)]
}
def parse_csv(filename):
"""
Input: .csv filename string
Parse input into list of available times
Return: dictionary mapping
(name) : {(date) : excluded_times (start, end)}
______________________________________________________
CSV Format (Google spreadsheet):
Name date1 date2 date3
a start1 end1 start2 end2 ... ...
b
c
______________________________________________________
"""
name_exclude = {}
dates = []
with open(filename, 'rb') as csvfile:
schedule_reader = csv.reader(csvfile, delimiter=',')
for i, row in enumerate(schedule_reader):
if(i == 0):
dates = row[1::]
else:
exclude = {}
for j, date in enumerate(dates):
no_work = row[j+1].split()
temp = []
for k in range(0, len(no_work), 2):
temp.append((float(no_work[k]), float(no_work[k+1])))
exclude[date] = temp
name_exclude[row[0]] = exclude
return name_exclude
def coordinator_availability(pattern):
"""
Input:
pattern - tuple of (interview length, break) in min
coord_avail - global dict mapping dates to availability (hours)
(date) : [(start, end), (start,end)]
Returns: block_avail - split the interview availability into
pattern specific block, output is dictionary of form:
(date) : [start1, start2, start3]
where each list entry is the start time of an interview in
(hour.decimal_minute) format
"""
interview_length = pattern[0] #interview length
break_length = pattern[1]
block_avail = {}
for date in coord_avail:
times = []
for strt_end in coord_avail[date]:
start = strt_end[0]*60
end = strt_end[1]*60
while(start < end):
times.append(start/60)
start += interview_length + break_length
block_avail[date] = times
return block_avail
def available_times(name_exclude, block_avail):
"""
Input:
name_exclude - dictionary consisting of
(name) : {(date) : excluded_times (start, end)}
block_avail - dictionary consisting of
(date) : [start1, start2, start3]
Returns:
name_available - (name) : {(dates) : times}
Reduces excluded times and available startimes interviews
into a single dictionary mapping names to available interview
start times per input date.
"""
name_available = {}
for person in name_exclude:
available_interviews = {}
for date in name_exclude[person]:
available_interviews[date] = []
for available in block_avail[date]:
if( not is_in(available, name_exclude[person][date]) ):
available_interviews[date].append(available)
name_available[person] = available_interviews
return name_available
def sort_and_match(name_available, block_avail):
"""
Input:
name_available - (name) : {(dates) : times}
mapping names to available interview start times
block_avail - (date) : [start1, start2, start3]
mapping dates to all possible interview start times
Return:
matching_dict - (date) : {(time): name}
not_matched - [name1, name2, name3]
Reduces availabe interview start times per person to actual
interview mappings per person by attempting to match the most
difficult inputs (smallest number of available interview times)
to interview times by least to most popular.
** variation on longest task first scheduling - Decreasing Time **
"""
most_popular = get_most_popular(name_available, block_avail)
matching_dict = {}
not_matched = []
for date in block_avail:
matching_dict[date] = {}
#create list using least number of
#available dates
#match hardest first
name_sort = []
for person in name_available:
count = 0
for date in name_available[person]:
count += len(name_available[person][date])
name_sort.append((person, count))
for person in sorted(name_sort, key=lambda x:x[1]):
found = False
for time_date in sorted(most_popular, key=lambda x: x[2]):
if(close_enough_in_date(time_date[1],
name_available[person[0]][time_date[0]])):
matching_dict[time_date[0]][time_date[1]] = person[0]
most_popular.remove(time_date)
found = True
break
if found == False:
not_matched.append(person[0])
return matching_dict, not_matched
def write_to_csv(outfile, matching_dict, block_avail, not_matched):
csv_out = []
header_row = ["times"]
sorted_dates = sorted(block_avail.keys(), key=lambda x: x[5::])
#clever way to sort by date!
for key in sorted_dates:
header_row.append(key)
csv_out.append(header_row)
set_accum = []
for date in block_avail:
set_accum += block_avail[date]
set_accum = sorted(list(set(set_accum))) #remove duplicates
for time in set_accum:
date_time = [float_mil_to_actual(time)]
date_time += ["--"]*(len(header_row[1::])) #len of header -1
csv_out.append(date_time)
for date in matching_dict:
for time in matching_dict[date]:
time_pos = set_accum.index(time) + 1
date_pos = sorted_dates.index(date) + 1
csv_out[time_pos][date_pos] = matching_dict[date][time]
blank_row = ["Not Matched"] + ["-----"]*(len(header_row)-1)
csv_out.append(blank_row)
offset = len(csv_out)
for i in range( int(math.ceil(len(not_matched) / (len(header_row))))):
blank_names = ["-----"] * (len(header_row))
csv_out.append(blank_names)
matched = 0
for i in range( int(math.ceil(len(not_matched) / (len(header_row))))):
for j in range(len(header_row)):
csv_out[i+offset][j] = not_matched[matched]
matched += 1
with open(outfile, 'w') as fp:
a = csv.writer(fp, delimiter=',')
a.writerows(csv_out)
return
#########################################################
############# Helper Functions ##########################
#########################################################
def get_most_popular(name_available, block_avail):
"""
Input:
name_available - (name) : {(dates) : times}
mapping names to available interview start times
block_avail - (date) : [start1, start2, start3]
mapping dates to all possible interview start times
Return:
List of tuples with (date, time, popularity count)
Reduce name_available and block_avail into a list of
tuples with counts for how often each time occured in
input availabilities. Used for sorting by popularity
in sort and match.
"""
popular = {}
for date in block_avail:
popular[date] = {}
for date in popular:
for entry in block_avail[date]:
popular[date][entry] = 0
for name in name_available:
for date in name_available[name]:
for entry in name_available[name][date]:
popular[date][entry] += 1
return_list = []
for date in popular:
for entry in popular[date]:
return_list.append( (date, entry, popular[date][entry]) )
return return_list
def pretty_print_interviews(matching_dict):
"""
Print in form:
______________________________________________________
Interview Times
(Date):
(time1) -- (name1)
(time2) -- (name2)
______________________________________________________
"""
print "Interview Times"
for date in sorted(matching_dict, key=lambda x: x):
print date + ":"
for time in sorted(matching_dict[date], key=lambda x: x):
print "\t {0} -- {1}".format(float_mil_to_actual(time),
matching_dict[date][time])
def float_mil_to_actual(time):
"""
Change float format (date.decimal_minute)
to acceptable time string. Rounded to nearest
standard time.
e.g. 10.33 -> "10:20"
"""
if(time > 12.0):
final_time = int(time - 12.0)
else:
final_time = int(time)
decimal = time % 1
actual = int(round(decimal * 60))
if(actual == 0):
actual = "00"
return "{0}:{1}".format(final_time, actual)
def close_enough_in_date(time, time_list):
"""
Check if an input time is in a list of
times within 0.01 error, prevents float comparison
issues.
"""
for x in time_list:
if(compare_close(time, x, 0.001)):
return True
return False
def compare_close(a, b, error):
return abs(a-b) < error
def is_between(start_end, time):
return time >= start_end[0] and time <= start_end[1]
def is_in(available, excluded_times):
"""
Check of an input time is in a list of (start, end)
excluded times.
"""
test = []
for time in excluded_times:
test.append( is_between(time, available) )
return any(test)
#########################################################
############# End Helper Functions ######################
#########################################################
def main():
name_exclude = parse_csv("csv/test_schedule_1.csv")
block_avail = coordinator_availability((15, 5))
name_available = available_times(name_exclude, block_avail)
matching_dict, not_matched = sort_and_match(name_available, block_avail)
write_to_csv("csv/out.csv", matching_dict, block_avail, not_matched)
if __name__ == "__main__":
main()