View Single Post
Old 10-14-2022, 12:13 PM   #4
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,475
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
I think you want something like this ids_for_field() function. I don't know if the order is right. This produces (id, name) but you might want (name, id).
Code:
python:

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 vals in tup:
			unique_names.update((vals,))
	# Now get the ids for the names and build the pairs
	id_field_pairs = list()
	for aut in unique_names:
		id_field_pairs.append((db.get_item_id(field_name, aut), aut))
	return id_field_pairs

def evaluate(book, context):
	db = context.db.new_api
	print('--------------------')
	# Get the list of books in the current VL
	ids_in_vl = db.search('', restriction='authors:a')
	# Get the id,val pairs for the desired field
	field_pairs = ids_for_field(db, ids_in_vl, 'authors')
	print(field_pairs) 
	return ''
NB: I am using the python template facility to test the code, which is why "python:" and "def evaluate()" are there. It hadn't occurred to me when we added it that one use is interactive python code debugging inside calibre.
chaley is offline   Reply With Quote