View Single Post
Old 12-22-2022, 01:35 PM   #442
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,511
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Question: Here's a template I use to select, split, and sort hierarchical tags for a #subjects column:

[...]

Seeing as my books are always tagged as, e.g.
Fiction.Science Fiction.Space Opera
Nonfiction.Biographies and Memoirs, Nonfiction.Music

I thought it might make sense to change re($tags, '\.', ',') to exclude the 'topmost' (anything to the left of the first period) rather than remove them out after the fact.

a) Would this improve performance? I also have other tags to remove, so I wouldn't be removing the list_difference entirely.
b) What regex would I use? re($tags, '(.*)\.', ',') only semi-worked; I see why but I'm not sure how to properly capture it.
If you are going to call list_difference in any event then there isn't much point to removing the unwanted prefixes earlier in the template. It is more work to remove them then to use the existing list_difference().

For fun, here is a Python template that does what I think you want. I used #genre for both the booktype and tags values.
Code:
python:
def evaluate(book, context):
	t = book.get('#genre') # For you this would be '#booktype'
	# Check if any of the items in the list t startwith one of the values
	if any((s.startswith(('Science Fiction', 'Nonfiction', 'Magazines & Periodicals')) for s in t)):
		# At least one does. Split all the items into single words.
		# You would want the " in t " to be " in book.get('tags')"
		res = {t3 for t2 in t for t3 in t2.split('.')}
		# Remove the undesired items
		res -= set(('Science Fiction', 'Nonfiction', 'Magazines & Periodicals', 'Cultures & Regions', 'Social Issues'))
		# Sort the items then build a comma-separated string of them
	    return ', '.join(sorted(res))
	return ''

Last edited by chaley; 12-24-2022 at 07:48 AM. Reason: Remove doubled "python:" in template.
chaley is offline   Reply With Quote