def intersect_ids(cohort_id_list): user_ids = dict() # only a single cohort id in the expression - return all users of this # cohort if len(cohort_id_list) == 1: for id in query_mod.get_cohort_users(cohort_id_list[0]): yield id else: for cid in cohort_id_list: for id in query_mod.get_cohort_users(cid): if id in user_ids: user_ids[id] += 1 else: user_ids[id] = 1 # Executes only in the case that there was more than one # cohort id in the expression for key in user_ids: if user_ids[key] > 1: yield key
def get_users(cohort_expr): """ get users from cohort """ if search(COHORT_REGEX, cohort_expr): logging.info(__name__ + ' :: Processing cohort by expression.') users = [user for user in parse_cohorts(cohort_expr)] else: logging.info(__name__ + ' :: Processing cohort by tag name.') try: id = query_mod.get_cohort_id(cohort_expr) users = [u for u in query_mod.get_cohort_users(id)] except (IndexError, TypeError, query_mod.UMQueryCallError) as e: logging.error(__name__ + ' :: Could not retrieve users ' 'for cohort {0}: {1}'. format(cohort_expr, str(e))) return [] return users