View Single Post
Old 02-21-2021, 02:16 PM   #355
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,495
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by capink View Post
@chaley: I am having a little problem with the for loop, relating to a new template function I've added to the plugin. The function is called category_items(). It works as follows

Code:
program:
    category_items('#genre')
This would return all the #genre tags.

It can also take second optional arg to limit the result to set of book_ids

Code:
program:
    book_ids = from_selection('id');
    category_items('#genre',book_ids)
from_selection() can be replaced with from_search() to achieve the same result. I've updated both to accept 'id' as column name and return a list of book_ids.
Good idea. I have used the calibre built-in version of this function many times. Are you using it to do the implementation? It is calibre.db.cache.get_categories. It supports a list of book ids, does caching, and has several other optimizations. It returns a dict
Code:
['lookup_name': set_of_values, ...]
The tag browser uses this function to build the category list.
Quote:
The problem arise when I run the new function on a field containing names like 'authors'. I have to return a list separated with an ampersand, because author names can have commas in them. The documentation says the for loop will only accept a comma separated list of values. Can this modified to allow the user to somehow choose a different separator, maybe by setting a variable before running the for loop.
I can do this. The big question is how? What I would prefer to do is extend the language for the for statement, making its grammar
Code:
'for' varname 'in' expression ['separator' expression] ':'
Specifying the separator is optional. OK with you?
Quote:
Another thing: I started making another function top_category_items() that is the same to the above function, but only extracts the top hierarchy level. I then starting having doubt, because I thought maybe combining the category_items() with subitem() would be a better way to achieve this and also offers more possibilities. The problem here is with that approach, I have to remove duplicates myself. It can be done using template but a lot of hassle. I ran recently into this problem in this template. The solution would be to add a uniq() template that removes duplicates from a list. I can easily add this, but I think it might be better to be included in the template builtin functions. Would you be willing to add something like this? I not, I will add to the plugin.
You can already do exactly this with list_union, as in
Code:
list_union('', list_to_remove_duplicates, sep)
However, I realize that this use of list_union is rather esoteric, so I will add the function
Code:
list_remove_duplicates(list, sep)
If you think that list_uniq(), or perhaps list_unique(), is a better name than I can use that.
chaley is offline   Reply With Quote