-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_meetings.py
493 lines (385 loc) · 14.6 KB
/
find_meetings.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
import argparse
from datetime import datetime
from itertools import combinations, product
from collections import Counter
import operator as op
from functools import reduce
import sys
def main():
args = get_commandline_arguments()
path_to_csv_file = args.doodlepoll_csv_filepath
if_need_be_yes = True
if args.treat_if_need_be_as_no:
print("Treating if-need-be responses as No.")
if_need_be_yes = False
doodle_poll = DoodlePoll.from_csv_file(path_to_csv_file, if_need_be_yes)
meetings = doodle_poll.get_meetings()
meetings = list(meetings)
filters = get_meeting_filters(args)
meetings = list(apply_filters(filters, meetings))
print_filter_status(filters)
calculate_and_print_number_of_candidates(len(meetings), args.meetings_per_solution)
halt_if(args.dry_run)
meeting_sets = generate_meeting_sets(meetings, args.meetings_per_solution)
meeting_set_filters = get_meeting_set_filters(args, doodle_poll)
meeting_sets = apply_filters(meeting_set_filters, meeting_sets)
print_meeting_sets(meeting_sets)
print_filter_status(meeting_set_filters)
def get_commandline_arguments():
parser = argparse.ArgumentParser()
parser.add_argument(
'doodlepoll_csv_filepath',
type=str,
help='Location of the doodlepoll csv file.')
parser.add_argument(
'--meetings-per-solution',
type=int,
default=1,
help='Number of meetings in a solution.')
parser.add_argument(
'--dry-run',
action='store_true',
help='Just report filter counts and the number of candidates.')
parser.add_argument(
'--treat-if-need-be-as-no',
action='store_true',
help='If set, if-need-be responses will be considered No responses.')
parser.add_argument(
'--weekday',
action='store_true',
help='Exclude weekend meetings.')
parser.add_argument(
'--min-start',
type=int,
help='Exclude meetings starting before hour (24 clock).')
parser.add_argument(
'--max-start',
type=int,
help='Exclude meetings starting after hour (24 clock).')
parser.add_argument(
'--min-people',
type=int,
default=2,
help='Exclude meetings with fewer than the given number of people who can attend.')
parser.add_argument(
'--max-people',
type=int,
help='Exclude meetings with more than the given number of people who can attend.')
parser.add_argument(
'--min-facilitators',
type=int,
default=1,
help='Exclude meetings with fewer than the given number of facilitators.')
parser.add_argument(
'--max-facilitators',
type=int,
help='Exclude meetings with more than the given number of facilitators.')
parser.add_argument(
'--min-participants',
type=int,
default=1,
help='Exclude meetings with fewer than the given number of participants.')
parser.add_argument(
'--max-participants',
type=int,
help='Exclude meetings with more than the given number of participants.')
parser.add_argument(
'--max-facilitations',
type=int,
help='Exclude candidates where any one facilitator must facilitate more than the given number of meetings.')
return parser.parse_args()
def get_meeting_filters(args):
filters = []
if args.weekday:
filters.append(WeekdayFilter())
if args.min_start:
filters.append(MinStartFilter(args.min_start))
if args.max_start:
filters.append(MaxStartFilter(args.max_start))
if args.min_people:
filters.append(MinPeopleFilter(args.min_people))
if args.max_people:
filters.append(MaxPeopleFilter(args.max_people))
if args.min_facilitators:
filters.append(MinFacilitatorsFilter(args.min_facilitators))
if args.max_facilitators:
filters.append(MaxFacilitatorsFilter(args.max_facilitators))
if args.min_participants:
filters.append(MinParticipantsFilter(args.min_participants))
if args.max_participants:
filters.append(MaxParticipantsFilter(args.max_participants))
return filters
def apply_filters(filters, items):
for f in filters:
items = f.apply(items)
return items
def print_filter_status(filters):
for f in filters:
print(f)
def calculate_and_print_number_of_candidates(number_of_meetings, meetings_per_candidate):
n = number_of_meetings
k = meetings_per_candidate
total = ncr(n, k)
print(f'There are {total} {k}-meeting candidates')
def halt_if(c):
if c:
sys.exit(0)
def generate_meeting_sets(meetings, k):
return combinations(meetings, k)
def get_meeting_set_filters(args, doodle_poll):
filters = []
participants = doodle_poll.get_participants()
filters.append(AllParticipantsCanAttendAtLeastOneMeetingFilter(participants))
if args.max_facilitations:
filters.append(MaxFacilitationsFilter(args.min_facilitators, args.max_facilitations))
return filters
def print_meeting_sets(meeting_sets):
for i, ms in enumerate(meeting_sets, 1):
print(f'========= Solution {i} =============')
for m in ms:
print()
print(m)
print()
class DoodlePoll:
@classmethod
def from_csv_file(cls, path_to_file, if_need_be_yes=True):
string = load_file(path_to_file)
return cls.from_csv_string(string, if_need_be_yes)
@classmethod
def from_csv_string(cls, string, if_need_be_yes=True):
lines = string.split('\n')
raw_data = [ln.split(',') for ln in lines]
datetimes = tuple(cls.parse_datetimes(raw_data))
people = tuple(cls.parse_people(raw_data))
matrix = cls.parse_availability_matrix(raw_data, if_need_be_yes)
return DoodlePoll(people, datetimes, matrix)
@classmethod
def parse_datetimes(cls, raw_data):
# this looks like a hyphon, but it's not
DOODLEPOLL_TIME_SEPARATOR = ' – '
times = raw_data[3:6]
for i in range(1, len(times[0])):
if times[0][i]:
month_year = times[0][i]
if times[1][i]:
day_date = times[1][i]
date = day_date.split(' ')[1]
if times[2][i]:
time = times[2][i]
start_time = time.split(DOODLEPOLL_TIME_SEPARATOR)[0]
dt = datetime.strptime(f'{month_year} {date} {start_time}', '%B %Y %d %I:%M %p')
yield dt
@classmethod
def parse_people(cls, raw_data):
rows = raw_data[6:cls.get_index_of_count_row(raw_data)]
for r in rows:
name = r[0]
if name[0] == '*':
yield Facilitator(name)
else:
yield Participant(name)
@classmethod
def parse_availability_matrix(cls, raw_data, if_need_be_yes=True):
rows = [row[1:] for row in raw_data[6:cls.get_index_of_count_row(raw_data)]]
for row in rows:
for i in range(len(row)):
if row[i] == 'OK':
row[i] = Yes()
elif row[i] == '(OK)':
if if_need_be_yes:
row[i] = IfNeedBeYes()
else:
row[i] = IfNeedBeNo()
else:
row[i] = No()
return rows
@classmethod
def get_index_of_count_row(cls, raw_data):
for i in range(-1, -len(raw_data), -1):
if raw_data[i][0] == 'Count':
return i
def __init__(self, people, datetimes, availability_matrix):
self.people = people
self.datetimes = datetimes
self.availability_matrix = availability_matrix
def get_meetings(self):
for dt in self.datetimes:
yield Meeting(dt, self.get_people_who_can_attend(dt))
def get_people_who_can_attend(self, datetime):
datetime_index = self.datetimes.index(datetime)
for person_index, person_is_available in enumerate(self.availability_matrix):
if person_is_available[datetime_index]:
yield self.people[person_index]
def get_participants_who_can_attend(self, datetime):
return filter(lambda x: x.is_participant(), self.get_people_who_can_attend(datetime))
def get_facilitators_who_can_attend(self, datetime):
return filter(lambda x: x.is_facilitator(), self.get_people_who_can_attend(datetime))
def get_people(self):
return self.people
def get_participants(self):
return filter(lambda x: x.is_participant(), self.get_people())
def get_facilitators(self):
return filter(lambda x: x.is_facilitator(), self.get_people())
def load_file(filepath):
with open(filepath) as f:
return f.read()
class Person:
def __init__(self, name):
self.name = name
def is_facilitator(self):
return False
def is_participant(self):
return False
def __eq__(self, other):
return self.name == other.name
def __ne__(self, other):
return self.name != other.name
def __str__(self):
return self.name
def __repr__(self):
return str(self)
def __hash__(self):
return hash(self.name)
class Facilitator(Person):
def is_facilitator(self):
return True
class Participant(Person):
def is_participant(self):
return True
class Response:
def is_if_need_be(self):
return False
class Yes(Response):
def __bool__(self):
return True
class No(Response):
def __bool__(self):
return False
class IfNeedBe(Response):
def is_if_need_be(self):
return True
class IfNeedBeYes(IfNeedBe, Yes):
pass
class IfNeedBeNo(IfNeedBe, No):
pass
class Meeting:
def __init__(self, datetime, people_who_can_attend):
self.datetime = datetime
if people_who_can_attend is not None:
self.people_who_can_attend = tuple(people_who_can_attend)
else:
self.people_who_can_attend = None
def get_people_who_can_attend(self):
return self.people_who_can_attend
def get_participants_who_can_attend(self):
return filter(lambda x: x.is_participant(), self.get_people_who_can_attend())
def get_facilitators_who_can_attend(self):
return filter(lambda x: x.is_facilitator(), self.get_people_who_can_attend())
def __str__(self):
s = [self.datetime.strftime('%c')]
for a in self.get_people_who_can_attend():
s.append(f'{a}')
return '\n '.join(s)
class Filter:
def apply(self, items):
self.success_count = 0
self.fail_count = 0
self.count_in = 0
self.count_out = 0
return filter(self.counting_condition, items)
def counting_condition(self, item):
self.count_in += 1
if self.condition(item):
self.success_count += 1
self.count_out += 1
return True
else:
self.fail_count += 1
return False
def __str__(self):
if hasattr(self, 'name'):
name = self.name
else:
name = type(self).__name__
return f'{name}: in={self.count_in}, filtered={self.count_in-self.count_out}, out={self.count_out}'
class WeekdayFilter(Filter):
def condition(self, meeting):
return meeting.datetime.weekday() < 5
class MinStartFilter(Filter):
def __init__(self, hour):
self.hour = hour
self.name = f'MinStartFilter({hour})'
def condition(self, meeting):
return meeting.datetime.hour >= self.hour
class MaxStartFilter(Filter):
def __init__(self, hour):
self.hour = hour
self.name = f'MaxStartFilter({hour})'
def condition(self, meeting):
return meeting.datetime.hour <= self.hour
class MinPeopleFilter(Filter):
def __init__(self, n):
self.n = n
self.name = f'MinPeopleFilter({n})'
def condition(self, meeting):
return len(list(meeting.get_people_who_can_attend())) >= self.n
class MaxPeopleFilter(Filter):
def __init__(self, n):
self.n = n
self.name = f'MaxPeopleFilter({n})'
def condition(self, meeting):
return len(list(meeting.get_people_who_can_attend())) <= self.n
class MinFacilitatorsFilter(Filter):
def __init__(self, n):
self.n = n
self.name = f'MinFacilitatorsFilter({n})'
def condition(self, meeting):
return len(list(meeting.get_facilitators_who_can_attend())) >= self.n
class MaxFacilitatorsFilter(Filter):
def __init__(self, n):
self.n = n
self.name = f'MaxFacilitatorsFilter({n})'
def condition(self, meeting):
return len(list(meeting.get_facilitators_who_can_attend())) <= self.n
class MinParticipantsFilter(Filter):
def __init__(self, n):
self.n = n
self.name = f'MinParticipantsFilter({n})'
def condition(self, meeting):
return len(list(meeting.get_participants_who_can_attend())) >= self.n
class MaxParticipantsFilter(Filter):
def __init__(self, n):
self.n = n
self.name = f'MaxParticipantsFilter({n})'
def condition(self, meeting):
return len(list(meeting.get_participants_who_can_attend())) <= self.n
class AllParticipantsCanAttendAtLeastOneMeetingFilter(Filter):
def __init__(self, all_participants):
self.all_participants = set(all_participants)
def condition(self, meeting_set):
coverage = set()
for m in meeting_set:
coverage = coverage.union(set(m.get_participants_who_can_attend()))
return len(coverage) == len(self.all_participants)
class MaxFacilitationsFilter(Filter):
def __init__(self, min_facilitators, max_facilitations):
self.max_facilitations = max_facilitations
self.min_facilitators = min_facilitators
self.name = f'MaxFacilitationsFilter(min_facilitators={min_facilitators}, max_facilitations={max_facilitations})'
def condition(self, meeting_set):
each_meetings_minimum_facilitator_combinations = []
for m in meeting_set:
c = combinations(m.get_facilitators_who_can_attend(), self.min_facilitators)
each_meetings_minimum_facilitator_combinations.append(c)
for configuration in product(*each_meetings_minimum_facilitator_combinations):
c = Counter([f for meeting_facilitators in configuration for f in meeting_facilitators])
if all(v <= self.max_facilitations for v in c.values()):
return True
return False
def ncr(n, r):
r = min(r, n-r)
numer = reduce(op.mul, range(n, n-r, -1), 1)
denom = reduce(op.mul, range(1, r+1), 1)
return numer // denom
if __name__ == '__main__':
main()