View Single Post
Old 07-04-2022, 09:42 AM   #83
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: 11,779
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Two new template functions: book_count() and book_values()

04/July/2022 (in calibre 6.0)
  • New template function book_count():
    Code:
    book_count(query, use_vl)
    returns the count of books found by searching for query. If use_vl is 0 (zero) then virtual libraries are ignored. This function and its companion ``book_values()`` are particularly useful in template searches, supporting searches that combine information from many books such as looking for series with only one book. It cannot be used in composite columns unless the tweak allow_template_database_functions_in_composites is set to True. It can be used only in the GUI.
  • New template function book_values():
    Code:
    book_values(column, query, sep, use_vl)
    returns a list of the unique values contained in the column column (a lookup name), separated by sep, in the books found by searching for query. If use_vl is 0 (zero) then virtual libraries are ignored. This function and its companion book_count() are particularly useful in template searches, supporting searches that combine information from many books such as looking for series with only one book. It cannot be used in composite columns unless the tweak allow_template_database_functions_in_composites is set to True. It can be used only in the GUI.
Example: this template search uses these functions to find all series with only one book:
  1. Define a stored template (Preferences->Advanced->Template functions) named series_only_one_book (the name is arbitrary). The template is:
    Code:
    program:
    	vals = globals(vals='');
    	if !vals then
    		all_series = book_values('series', 'series:true', ',', 0);
    		for series in all_series:
    			if book_count('series:="' & series & '"', 0) == 1 then
    				vals = list_join(',', vals, ',', series, ',')
    			fi
    		rof;
    		set_globals(vals)
    	fi;
    	str_in_list(vals, ',', $series, 1, '')
    The first time the template runs (the first book checked) it stores the results of the database lookups in a global template variable named vals. These results are used to check subsequent books without redoing the lookups.
  2. Use the stored template in a template search:
    Code:
    template:"program: series_only_one_book()#@#:n:1"
    Using a stored template instead of putting the template into the search eliminates problems caused by the requirement to escape quotes in search expressions.

Last edited by chaley; 07-11-2022 at 10:28 AM.
chaley is offline   Reply With Quote