![]() |
#466 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
|
![]() |
![]() |
![]() |
#467 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,044
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
How is switch() meant to be used? Here, I got overdrive.png when I was expecting drm-locked.png, so I think I either
a) misunderstood the syntax b) misunderstood what instances it's meant to be used in: Last edited by ownedbycats; 01-08-2023 at 11:45 PM. |
![]() |
![]() |
Advert | |
|
![]() |
#468 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,044
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Is there a function that sorts a list into a user-defined order?
Context: I have a text column that may return results in a list-string like "foo, bar, foo, foo, foobar." Using Action Chains, I'd like to: a) Sort the entries in a user-defined order (e.g. "foobar, bar, foo, foo, foo) b) Select the first item of this sorted list And then replace the string with that. (Additional context: When FanFicFare downloads an anthology of multiple fics, it concenates all the ratings into an unsorted string like "General Audiences, Mature Audiences, Explicit, Teen and Up Audiences, Not Rated." In order, I want to pick the first of Explicit > Mature > Teen > General > Not Rated.) edit: can I check just one column with a first_non_empty? Last edited by ownedbycats; 01-09-2023 at 03:13 AM. |
![]() |
![]() |
![]() |
#469 | ||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Code:
program: possibles = 'Explicit, Mature, Teen, General, Not Rated'; actuals = 'foo, Mature, Teen, Explicit'; answer = ''; for item in possibles: if '^' & item & '$' inlist actuals then answer = item; break fi rof; answer Quote:
Last edited by chaley; 01-09-2023 at 09:04 AM. Reason: Removed incorrect text in the last sentence. |
||
![]() |
![]() |
![]() |
#470 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
The syntax you are using is switch_if(). |
|
![]() |
![]() |
Advert | |
|
![]() |
#471 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,044
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Here's what I came up with:
Code:
program: ## list_remove_duplicates probably isn't necessary list = list_remove_duplicates($#fanficrating, ','); first_non_empty( if 'Explicit' inlist list then 'Explicit' fi, if 'Mature Audiences' inlist list then "Mature Audiences" fi, if "Teen and Up Audiences" inlist list then "Teen and Up Audiences" fi, if "General Audiences" inlist list then "General Audiences" fi, if "Not Rated" inlist list then "Not Rated" fi ) The first_non_empty question I was thinking of something along the lines of: Code:
first_non_empty inlist list( "Explicit", "Mature Audiences" "Teen and Up Audiences" "General Audiences" "Not Rated" ) Last edited by ownedbycats; 01-09-2023 at 03:44 PM. |
![]() |
![]() |
![]() |
#472 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Note that what you wrote is exactly equivalent to this switch_if. I used the assignment to avoid writing the string twice. It isn't needed as the last test shows. Code:
program: ## list_remove_duplicates probably isn't necessary list = list_remove_duplicates($#fanficrating, ','); switch_if( (a = 'Explicit') inlist list, a, (a = 'Mature Audiences') inlist list, a, (a = "Teen and Up Audiences") inlist list, a, (a = "General Audiences") inlist list, a, "Not Rated" inlist list, "Not Rated", '' ) |
|
![]() |
![]() |
![]() |
#473 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,044
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
|
![]() |
![]() |
![]() |
#474 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,044
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Would there be any general use for a first_in_list(val, separator, value [, value]*) type function?
It would check a list for each pattern in order, and then return the found list item as the value. Template: first_in_list($tags, ',', "Fanfiction.Crossover") List contents: Fanfiction.Crossover, Fanfiction.Half-Life & Portal, Fanfiction.Mass Effect Template value: Fanfiction.Crossover Template: first_in_list(list, ',', "Explicit", "Mature Audiences", "Teen and Up Audiences", "General Audiences", "Not Rated") List contents: General Audiences, Explicit, Not Rated, Not Rated Template value: Explicit Regex pattern matching may be useful but as with the in/inlist, it lead to unintended results: Template: first_in_list(list, ',', "foo", "^foobar") List contents: foobar, foo, bar, bar, foo Template value: foobar whoops! the regex matched 'foo' EDIT: Perhaps as a custom function. Last edited by ownedbycats; 01-11-2023 at 07:50 AM. |
![]() |
![]() |
![]() |
#475 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
If I add switch_list() then I will probably have it set the built-in variable '$' to the result of the match. That would let you write (untested): Code:
switch_if(list, ',', '^foo$', $, strcat('bar', $#status), $, '') |
|
![]() |
![]() |
![]() |
#476 |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
For fun, here is a Python stored template that does what you want. Stored templates like this obviate the need for a lot of seldom-used built-in functions.
The template: Code:
python: def evaluate(book, context): args = context.arguments if args is None or len(args) != 3: raise ValueError('first_in_list requires 3 arguments') sep = args[1] lst = tuple(l.strip() for l in args[0].split(',') if l.strip()) test_lst = tuple(l.strip() for l in args[2].split(',') if l.strip()) if not test_lst: return '' for v in test_lst: if v in lst: return v return '' Calling it: Code:
program: first_in_list($tags, ',', 'a, b, Space Opera, c') Last edited by chaley; 01-11-2023 at 05:13 PM. |
![]() |
![]() |
![]() |
#477 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,044
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
That works!
![]() |
![]() |
![]() |
![]() |
#478 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,044
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
I just noticed that empty lines at the end of a template get removed, at least in tester. That's a nice little QoL detail.
|
![]() |
![]() |
![]() |
#479 |
Custom User Title
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 11,044
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Is there any risk of permanent damage if I accidentally open a stable version of Calibre where a new function hadn't been added?
The only thing I can think of is a error'd save path if I send a book to device while the template is (indirectly, using a composite column) using switch_if(). |
![]() |
![]() |
![]() |
#480 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
In other contexts, which I think includes most places including device drivers, the value of the template is the error message. This can affect the value of composite columns, so anything that depends on them will "see" the wrong answer. Whether this results in permanent damage I can't say. |
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Library Management: various questions not worth their own thread | ownedbycats | Library Management | 225 | 08-04-2025 06:31 PM |
[Metadata Source Plugin] Questions regarding parse select, docs and ref templates | Boilerplate4U | Development | 13 | 07-07-2020 02:35 AM |
Questions on Kobo [Interfered with another thread topic] | spdavies | Kobo Reader | 8 | 10-12-2014 11:37 AM |
[OLD Thread] Some questions before buying the fire. | darthreader13 | Kindle Fire | 7 | 05-10-2013 09:19 PM |
Thread management questions | meme | Feedback | 6 | 01-31-2011 05:07 PM |