View Single Post
Old 02-19-2021, 10:13 AM   #341
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,203
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
Quote:
Originally Posted by chaley View Post
This example action computes the average rating for 3 Genres and their children then stores those ratings in a tags-like custom column in the form 'genre name':average_rating.
Nice work.

Quote:
Originally Posted by chaley View Post
@capink:I couldn't get the mean() function to work. How do I convert the result of "from_search()" to an "iterable"?
I don't know what exactly is happening. But removing the spaces in the genres like below, makes it work. Will investigate this later:

This is working for me:

Code:
program:
	genres = 'Comics,Mysteries,Science Fiction';
	res = '';
	for g in genres:
		ratings = from_search('rating', strcat('#genre:"=.', g, '"'));
		avg = mean(ratings);
		res = list_union(res, strcat(g, ':', avg), ',')
	rof;
	res
Notice that mean discards any non numerical value. If someone wants None values to weigh on the average, modify as follows:

Code:
program:
	genres = 'Comics,Mysteries,Science Fiction';
	res = '';
	for g in genres:
		ratings = from_search('rating', strcat('#genre:"=.', g, '"'));
		ratings = re_non_numerical(ratings, 0);
		avg = mean(ratings);
		res = list_union(res, strcat(g, ':', avg), ',')
	rof;
	res
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.

Quote:
Originally Posted by chaley View Post
My concern is "mysterious behavior" that depends on a sequence of choices. I am not convinced that action A should affect action B. If it does then there should be something that explicitly declares "action global" variables that persist. Or perhaps the notion of "super actions" that run named actions in a sequence using the same globals dict.

The next question is their lifetime. Do they live for the calibre invocation, while a library is open, while a VL is open, or something else?
That makes sense. Will leave it as it is for now. Maybe in the future will add a template function to let the user persist whatever he wants.

Edit1: No need to create a template function for that. If a user wants to persist data, he can create an empty book record and use the single field edit to persist data to it.

Edit2: from_search() and from_selection() always return an iterable (comma separated values).

Last edited by capink; 02-19-2021 at 10:31 AM.
capink is offline   Reply With Quote