View Single Post
Old 02-19-2021, 11:14 AM   #342
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,476
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
This is working for me:

[...]
Note that in both cases you must divide the result of mean() by 2, either at the call or later in the list_union. For historical reasons calibre keeps ratings as numbers between 0 and 10. One star == 2, two stars == 4, etc.

The following template does that divide. I also changed the search to include only rated books so that the template can avoid writing xxx:0 if no books with that category are rated.
Code:
program:
	genres = 'Comics, Mysteries, Science Fiction';
	res = '';
	for g in genres:
		ratings = from_search('rating', strcat('rating:true and #genre:"=.', g, '"'));
		if ratings then
			avg = divide(mean(ratings), 2);
			res = list_union(res, strcat(g, ':', avg), ',')
		fi
	rof;
	res
Quote:
Also there is a bug with the mean function when it is fed an empty list or a list that contains no numerical values. Will fix this in the next release.
This is what confused me. I was debugging both the consequences of the extra space in the for loop variable and mean() not working. The extra space meant that the search was empty, triggering the mean() problem you found.

BTW: this is fun, all of figuring out the details of how action chains works, the "logic problem" of how to chain the steps, and constructing templates that compute what is needed. I also am finding some template functions that I might add. One is[list_]count_matching(l, sep, pattern) that returns how many items in l match the pattern.
chaley is offline   Reply With Quote