View Single Post
Old 10-14-2022, 12:29 PM   #5
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,461
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Here is an example that uses the current VL
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 val in tup:
			unique_names.add(val)
	# Now get the ids for the names and build the pairs
	id_field_pairs = list()
	for name in unique_names:
		id_field_pairs.append((db.get_item_id(field_name, name), name))
	return id_field_pairs

def evaluate(book, context):
	print('--------------------')
	# Get the list of books in the current VL
	ids_in_vl = context.db.data.search_getting_ids('', '', use_virtual_library=True)
	# Get the id,val pairs for the desired field
	field_pairs = ids_for_field(context.db.new_api, ids_in_vl, 'authors')
	print(field_pairs) 
	return ''

Last edited by chaley; 10-14-2022 at 12:44 PM. Reason: Small improvement to ids_for_field
chaley is offline   Reply With Quote