def test_sort_by_reference(): ids = [4, 7, 1, 3] objs = [{'id': 1}, {'id': 7}, {'id': 4}, {'id': 3}] sorted_ = util.sort_by_reference(ids, objs, lambda x: x['id']) h.assert_equal(sorted_, [{'id': 4}, {'id': 7}, {'id': 1}, {'id': 3}])
def _expand_facets(facets, dataset): dim_names = [d.name for d in dataset.dimensions] for name in facets.keys(): if name in dim_names and dataset[name].is_compound: dim = dataset[name] member_names = [x[0] for x in facets[name]] facet_values = [x[1] for x in facets[name]] members = dim.members(dim.alias.c.name.in_(member_names)) members = util.sort_by_reference(member_names, members, lambda x: x['name']) facets[name] = zip(members, facet_values)
def _get_entries(docs): # List of ids in Solr return order ids = [d['id'] for d in docs] # partition the entries by dataset (so we make only N queries # for N datasets) by_dataset = defaultdict(list) for d in docs: by_dataset[d['dataset']].append(d['id']) # create a great big list of entries, one per doc entries = [] for ds_name, ids in by_dataset.iteritems(): dataset = model.Dataset.by_name(ds_name) query = dataset.alias.c.id.in_(ids) entries.extend([(dataset, e) for e in dataset.entries(query)]) entries = util.sort_by_reference(ids, entries, lambda x: x[1]['id']) for dataset, entry in filter(lambda e: e is not None, entries): yield dataset, entry
def _get_entries(docs): # List of ids in Solr return order # print [docs] ids = [d['id'] for d in docs] # partition the entries by dataset (so we make only N queries # for N datasets) by_dataset = defaultdict(list) for d in docs: by_dataset[d['dataset']].append(d['id']) # create a great big list of entries, one per doc entries = [] for ds_name, ds_ids in by_dataset.iteritems(): dataset = Dataset.by_name(ds_name) query = dataset.alias.c.id.in_(ds_ids) entries.extend([(dataset, e) for e in dataset.entries(query)]) entries = util.sort_by_reference(ids, entries, lambda x: x[1]['id']) for dataset, entry in entries: yield dataset, entry
def test_sort_by_reference(self): ids = [4, 7, 1, 3] objs = [{'id': 1}, {'id': 7}, {'id': 4}, {'id': 3}] sorted_objs = util.sort_by_reference(ids, objs, lambda x: x['id']) assert sorted_objs == [{'id': 4}, {'id': 7}, {'id': 1}, {'id': 3}]