class WhosOutChecker(object): def __init__(self, api_key, company, host=None): self.bamboohr_client = BambooHrClient(api_key, company, host) self.emps = self.bamboohr_client.get_employees_directory() self.namesets = self._build_namesets(self.emps) @staticmethod def _build_namesets(employees): '''Maps all derived employee names to lists of employee ids they may refer to, for speedy querying''' namesets = defaultdict(set) # default dict of names - empIds for emp_id, emp in employees.items(): names = sum((re.split('[ -]', _normalise_name(name)) for name in emp if name is not None), []) for name in names: namesets[name].add(emp_id) return namesets @staticmethod def _get_employee_ids_from_name(typed_name, namesets): '''Get a list of employee ids that a typed name can refer to''' typed_name = _normalise_name(typed_name) typed_names = re.split('[ -]', typed_name) match_sets = [namesets[tn] for tn in typed_names if tn in namesets] if len(match_sets) == 0: return [] intersection = set(match_sets[0]) for i in range(1, len(match_sets)): intersection.intersection_update(match_sets[i]) return list(intersection) def get_whos_out(self): '''Get a list of who's out, each element as (Employee, Leave)''' leaves = self.bamboohr_client.get_timeoff_whosout() return [(self.emps[emp_id], leave) for emp_id, leave in leaves.items()] def where_is(self, name): '''Returns a list of (Employee, Leave) pairs for employees matching NAME; Leave will be None if the employee is not currently on leave''' print("where_is called with name=", name) matching_emps = sorted( self._get_employee_ids_from_name(name, self.namesets)) if len(matching_emps) == 0: return [] current_leaves = self.bamboohr_client.get_timeoff_whosout() return [(self.emps[x], current_leaves.get(x)) for x in matching_emps]
def __init__(self, api_key, company, host=None): self.bamboohr_client = BambooHrClient(api_key, company, host) self.emps = self.bamboohr_client.get_employees_directory() self.namesets = self._build_namesets(self.emps)