def build_groups(self, mpsnl, force_new=False, snlgroup_guess=None, testing_mode=False): # testing mode is used to see if something already exists in DB w/o adding it to the db match_found = False if not force_new: if snlgroup_guess: sgp = self.snlgroups.find_one({'snlgroup_id': snlgroup_guess}) snlgroup = SNLGroup.from_dict(sgp) match_found = self._add_if_belongs(snlgroup, mpsnl, testing_mode) if not match_found: # look at all potential matches for entry in self.snlgroups.find( {'snlgroup_key': mpsnl.snlgroup_key}, sort=[("num_snl", DESCENDING)]): snlgroup = SNLGroup.from_dict(entry) match_found = self._add_if_belongs(snlgroup, mpsnl, testing_mode) if match_found: break if not match_found: # add a new SNLGroup snlgroup_id = self._get_next_snlgroup_id() snlgroup = SNLGroup(snlgroup_id, mpsnl) if not testing_mode: self.snlgroups.insert(snlgroup.to_dict) return snlgroup, not match_found
def switch_canonical_snl(self, snlgroup_id, canonical_mpsnl): sgp = self.snlgroups.find_one({'snlgroup_id': snlgroup_id}) snlgroup = SNLGroup.from_dict(sgp) all_snl_ids = [sid for sid in snlgroup.all_snl_ids] if canonical_mpsnl.snl_id not in all_snl_ids: raise ValueError( 'Canonical SNL must already be in snlgroup to switch!') new_group = SNLGroup(snlgroup_id, canonical_mpsnl, all_snl_ids) self.snlgroups.update({'snlgroup_id': snlgroup_id}, new_group.to_dict)