View Single Post
Old 10-14-2022, 04:24 PM   #7
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,529
Karma: 8075938
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Apologies for the churn. I looked at the underlying code for the API methods I used and found a significant inefficiency in what I proposed. The underlying reversed dictionary {id:name} was being generated for *every* field item. For big libraries this could be a significant performance hit.

Here is a version that reverses the id map once then uses it to build the output.
Code:
def ids_for_field(db, ids_of_books, field_name):
	# First get all the names for the desired books.
	# Use a set to make them unique
	unique_names = set()
	for tup in db.all_field_for(field_name, ids_of_books).values():
		for val in tup:
			unique_names.add(val)
	# reverse the map of ids to names so id_map[name] gives the id
	id_map = {v:k for k,v in db.get_id_map(field_name).items()}
	# Now build the pairs (id, name)
	id_field_pairs = list()
	for name in unique_names:
		id_field_pairs.append((id_map[name], name))
	return id_field_pairs
chaley is offline   Reply With Quote