Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Library Management

Notices

Reply
 
Thread Tools Search this Thread
Old 01-03-2023, 10:55 AM   #1
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Request for Comment: possible template function list_map()

Over the last while I have been asked a few times how to transform a list of values according to some rules, for example the latest posts in this thread. I have been wondering if this problem is general enough to merit direct support. Thus this RFC.

I propose a new function list_map() that takes a list of items then applies a series of rules to transform or not each item. It returns the transformed list. It does not modify the original list.

The syntax:
Code:
list_map(list_of_values, separator,
         rule [, rule]*)
You can have as many rules as you want. A rule is a triple (three arguments):
Code:
boolean to enable the rule, search expression for item, replacement expression
Example: This is a rule that for books authored by someone with Banks in their name, changes all tags 'Science Fiction' to 'SF':
Code:
'Banks' in $authors, '^Science Fiction' in $, 'SF'
The special variable $ is the current item in the list.

Putting it all together we have
Code:
program:
    r = list_map($tags, ', ',
                 'Banks' in $authors, '^Science Fiction' in $, 'SF')
The next example adds a rule to unconditionally delete the tag 'Fiction':
Code:
program:
    r = list_map($tags, ', ',
                 'Banks' in $authors, '^Science Fiction' in $, 'SF',
                 1, 'Fiction' == $, '')
The 'Fiction' rule is applied unconditionally because the test ('1') is always true.

Final example: I rewrote the example template in this post of the above mentioned thread to use the proposed list_map() function:
Code:
program:
#	show = 'Criminal Minds, Law and Order';
	show = 'Pretty Little Liars, Law and Order, Supergirl';
#	show = 'Criminal Minds, Law and Order, Supergirl';
#	show = $#show;

	ao3tags_pair = 'Alex / Emily, Olivia / Alex';
#	ao3tags_pair = $#ao3tags_pair;

	show1 = '^Criminal Minds$' inlist show && '^Law and Order$' inlist show;
	show2 = '^Pretty Little Liars$' inlist show && '^Law and Order$' inlist show;
	show3 = '^Supergirl$' inlist show && '^Law and Order$' inlist show;

	output = list_map(ao3tags_pair, ',',
			show1, $ == "Alex / Emily", "Emily Prentiss / Alex Cabot",
			show2, $ == "Alex / Emily", "Emily Fields / Alex Cabot",
			show3, $ == "Olivia / Alex", "Alex Danvers / Olivia Benson");

	output = list_sort(list_remove_duplicates(output, ','), 0, ',')
The output is
Code:
Alex Danvers / Olivia Benson, Emily Fields / Alex Cabot
The list_map() function isn't particularly hard to implement, but it does create maintenance and documentation headaches that aren't worth it if the function won't be used.

Thoughts?

Last edited by chaley; 01-03-2023 at 03:28 PM. Reason: Fix the separator in the last example
chaley is offline   Reply With Quote
Old 01-03-2023, 04:28 PM   #2
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,974
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
If I understand right, here's one instance I may find it useful:

FanFicFare adds an additional 'crossover' tag to #fanficcat when there's multiple entries. For certain sets of entries, I don't want this.

Since this isn't easy to work around in the FFF settings, I use a template to take the column, sort the entries, compare the string, and then replace if it matches:

Code:
program:
	f = list_sort($#fanficcat,0,',');

	if contains(f, '^Crossover, Mass Effect Trilogy, Mass Effect: Andromeda$', '1', '')
	then 'Mass Effect Trilogy, Mass Effect: Andromeda' 

	elif contains(f, '^Crossover, Half-Life, Portal$', '1', '')	
	then 'Half-Life, Portal' 

	else $#fanficcat

	fi
But my current solution working fairly well, so it's not a big deal either way.

Last edited by ownedbycats; 01-03-2023 at 04:32 PM. Reason: tidying up the template indents
ownedbycats is offline   Reply With Quote
Advert
Old 01-03-2023, 04:55 PM   #3
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
If I understand right, here's one instance I may find it useful:

FanFicFare adds an additional 'crossover' tag to #fanficcat when there's multiple entries. For certain sets of entries, I don't want this.

Since this isn't easy to work around in the FFF settings, I use a template to take the column, sort the entries, compare the string, and then replace if it matches:

Code:
program:
	f = list_sort($#fanficcat,0,',');

	if contains(f, '^Crossover, Mass Effect Trilogy, Mass Effect: Andromeda$', '1', '')
	then 'Mass Effect Trilogy, Mass Effect: Andromeda' 

	elif contains(f, '^Crossover, Half-Life, Portal$', '1', '')	
	then 'Half-Life, Portal' 

	else $#fanficcat

	fi
But my current solution working fairly well, so it's not a big deal either way.
Interesting. You are using list_sort() and contains() to test (sort-of) that one list is a subset of another list.

Question: Assume existence of a function list_is_subset(s1, s2, sep) that returns true (a non-empty value) if all the items in s2 are in s1. In this case, would you expect this example of the function list_subset() function to return True?
Code:
program:
    fanficcat = 'AAA, Crossover, DDD, Mass Effect Trilogy, Mass Effect: Andromeda';
    list_is_subset(fanficcat, 'Crossover, Mass Effect Trilogy, Mass Effect: Andromeda', ',')
All of the items in the second parameter are in the first parameter, so the second parameter is in fact a subset of the first. The items 'AAA' and 'DDD' in the first list are not in the second list, which is ignored.
chaley is offline   Reply With Quote
Old 01-03-2023, 05:44 PM   #4
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,974
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
But for list_is_subset, I'd expect it to return a 'true' for the example you provided.

But for my specific example, I was doing this for an exact match; e.g. if a book had Crossover, Half-Life, Mass Effect Trilogy, Mass Effect Andromeda then keep it as is. This template was done with Action Chains, so the second list excluding 'crossover' was actually overwriting the original.
ownedbycats is offline   Reply With Quote
Old 01-04-2023, 07:42 AM   #5
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,447
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
But for my specific example, I was doing this for an exact match; e.g. if a book had Crossover, Half-Life, Mass Effect Trilogy, Mass Effect Andromeda then keep it as is. This template was done with Action Chains, so the second list excluding 'crossover' was actually overwriting the original.
Why not use list_equals() for this?
chaley is offline   Reply With Quote
Advert
Old 01-04-2023, 03:07 PM   #6
ownedbycats
Custom User Title
ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.ownedbycats ought to be getting tired of karma fortunes by now.
 
ownedbycats's Avatar
 
Posts: 10,974
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
In all likelihood, probably because I didn't know what I was doing at the time. I'll take a look at that.
ownedbycats is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
ePub version in template function? tamaracks Library Management 8 06-17-2023 07:54 PM
BuiltinAdd template language function DyckBook Editor 4 10-19-2021 04:21 PM
Using built-in template functions in a custom template function ilovejedd Library Management 4 01-28-2018 12:20 PM
Problem with contains function in save template MicaOlaAdams Calibre 5 10-21-2016 10:25 AM
how to use re() function in Template Program Mode? msciwoj Library Management 3 07-07-2016 03:55 PM


All times are GMT -4. The time now is 04:06 AM.


MobileRead.com is a privately owned, operated and funded community.