View Single Post
Old 10-16-2011, 09:50 AM   #18
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,482
Karma: 8025704
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by Noughty View Post
Would making the list for list_intersection shorter make it easier for calibre? I could remove a lot from category character since there are way too many detail tags.
A bit, but I think not enough to justify the cost.

If you don't mind getting more complicated, we *can* justify the cost of using a python evaluation function. For example, for the template we have been playing with in the last 2 posts, a custom function could easily be 100 times faster to evaluate. That won't change the time needed to build the categories for the tag browser, but it would drastically reduce the amount of time needed to build the columns so the categories can be built.

Here is the last template rewritten as a template function. To put this into your library, go to preferences -> advanced -> template functions. You will see some boxes. Fill them in as follows:
Code:
Function: myListMerge (you can use any name here you want, as 
          long as it is alphabetic. You might consider using the column name, 
          such as myWhateverItIs.)

Arg count: 0 (ignore the warning that will appear later)

Documentation: whatever you want to put here

Program Code:
def evaluate(self, formatter, kwargs, mi, locals):
	tags = set(mi.get('tags', []))
	shelf = set(mi.get('#shelf', []))
	genre = set(mi.get('#genre', []))
	mode = set(mi.get('#mode', []))
	charac = set(mi.get('#character', []))

	st = tags | shelf
	gmc = set(['Short', 'delete']) | genre | mode | charac
	res = st - gmc
	return ', '.join(res)
Press 'create' (or modify if you are changing it later), then apply.

You would use this new function in your composite column template as follows, and assuming it is named myListMerge:
Code:
{:'myListMerge()'}
Given you current experience, I think you can see how this function works. It first gets all the fields from the book metadata information (mi). It then does a union of tags and shelf (the '|' operator), and then a union of genre, mode, charac, and the constant set containing 'Short' and 'delete'. Finally it does the difference (the '-' operator), then turns the result back into a comma-separated string (the 'join').

Do note that this function assumes that the case of all items is identical and uses a case-sensitive compare. For example, it thinks that foo and Foo are different. Fixing this is possible, but it would slow things down somewhat.

Last edited by chaley; 10-16-2011 at 10:41 AM. Reason: Fix mistake in composite template to call custom function
chaley is offline   Reply With Quote