01-04-2023, 07:47 PM | #451 |
Custom User Title
Posts: 8,841
Karma: 62032373
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Thanks.
When using first_non_empty, is there any performance difference between checking a default and custom column? Trying to figure out the optimal order for this (e.g. the more complex ones with multiple checks at the end). Code:
program: ## Missing metadata first_non_empty( if !$formats then 'plus.png' fi, if !$tags then 'tags.png' fi, if !$languages then 'languages.png' fi, if !$#booktype then 'catalog.png' fi, if !$#pagecount then 'cover_flow.png' fi, if !$#chaptercount && !'paperbook' in approximate_formats() then 'cover_flow.png' fi, if $#booktype == 'Documentation & Manuals' && !$#manualtype then 'catalog.png' fi, if $#kobostatus && $#kobostatus != 'Kobo Store' && !$#kobopath then 'snippets.png' fi, ) |
01-05-2023, 10:40 AM | #452 | |
Grand Sorcerer
Posts: 11,789
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
The first_non_empty() conditions should be ordered by likelihood. Put the ones first that are most likely to match, regardless of complexity. |
|
01-05-2023, 09:30 PM | #453 |
Custom User Title
Posts: 8,841
Karma: 62032373
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Thanks. Another question: what is the difference, if any, between using a first_non_empty and an if-then-elif in a situation like this?
first_non_empty is a bit easier for me (less misplaced fi's, for one!) so I'm inclined to keep it unless there's some large advantage to the other. Code:
program: if $#booktype == 'Fanfiction' then first_non_empty( if $#fanficstatus == 'In-Progress' && days_between(format_date(today(), 'yyyy-MM-dd'), $#fanficupdated) >= 365 then 'clock.png' fi, if !'^cover:' inlist $#admintags then 'test' fi ) fi Code:
program: if $#booktype == 'Fanfiction' then if $#fanficstatus == 'In-Progress' && days_between(format_date(today(), 'yyyy-MM-dd'), $#fanficupdated) >= 365 then 'clock.png' elif !'^cover:' inlist $#admintags then 'test' fi fi Last edited by ownedbycats; 01-05-2023 at 11:49 PM. |
01-06-2023, 05:26 AM | #454 | |
Grand Sorcerer
Posts: 11,789
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
I have wondered whether I should make a variant that combines first_non_empty() and switch(), perhaps named first_true(). It would take a list of argument pairs where each pair is a condition expression and a value expression. For each pair in order it would evaluate the condition. If True then it would evaluate the value expression and return it, otherwise try the next pair. There would be a 'none are true' value at the end, like switch(). This would be faster than either first_non_empty() or a series of ifs, and would probably be easier to read. But it would be yet another maintenance and documentation chore. It is also quite easy to do in a python template. I am still thinking about it. |
|
01-06-2023, 11:05 AM | #455 | |
Grand Sorcerer
Posts: 11,789
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
|
|
01-06-2023, 04:57 PM | #456 |
Custom User Title
Posts: 8,841
Karma: 62032373
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
I'll give it a test in a few days
Two questions here. #purchasecost is a floating-point with format of $x.xx. Code:
program: if $$#purchasecost ==# '0.00' then '0.00' elif $$#purchasecost <=# '0.99' then '$0.01 - $0.99' elif $$#purchasecost <=# '4.99' then '$1.00 - $4.99' elif $$#purchasecost <=# '10.00' then '$5.00 - $9.99' elif $$#purchasecost <=# '15.00' then '$10.00 - $14.99' elif $$#purchasecost <=# '15.00' then '$10.00 - $14.99' elif $$#purchasecost <=# '20.00' then '$15.00 - $19.99' elif $$#purchasecost <=# '30.00' then '$20.00 - $29.99' elif $$#purchasecost >=# '30.00' then '$30.00 and up' fi b) More out of curiosity: The first time I made this template, I forgot to add the '#' to all the cmps. Line 11, >= '30.00', showed up on undefineds. This didn't happen for == or <=. How did it get 'undefined is more than 30.00'? Last edited by ownedbycats; 01-06-2023 at 05:01 PM. |
01-06-2023, 05:12 PM | #457 | |
Grand Sorcerer
Posts: 11,789
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Alternatively, to control results you could use raw_field() explicitly to set what value you want for undefined. For example, you could say Code:
v = raw_field('#purchasecost', -100000.00) Finally, the last branch of the if shouldn't have a condition but should instead always return '$30.00 and up'. The test serves no purpose. |
|
01-07-2023, 01:42 AM | #458 |
Custom User Title
Posts: 8,841
Karma: 62032373
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
That worked.
Tweaking my #kobopath template: Code:
program: if $#kobostatus then # Returns 'Fanfiction/Fandom' tag for fanfics if $$#booktype == 'Fanfiction' then list_re($tags, ',', '^Fanfiction.(.*)$', 'Fanfiction/\1') # Returns 'PDF' for PDFs elif $formats == 'PDF' then 'PDF' # Returns 'Loans' for Loans elif 'OVERDRIVE' in $formats then 'Loans' # Returns booktype for others else $#booktype fi fi Due to my tagging practices: a) Fanfiction.Crossover is the always the first in the list; a) I only have multiple tags when one is 'Fanfiction.Crossover' b) I always prefer 'Crossover' for the first pick. So based on this, I thought of two separate solutions: Option #1: splitting the list: Code:
if $$#booktype == 'Fanfiction' then list_split(list_re($tags, ',', '^Fanfiction.(.*)$', 'Fanfiction/\1'), ',', 'ff'); ff_0 Option #2 A second check for 'Crossover' specifically. Here, I'm checking the taglike #fanficcat but I could also check Fanfiction.Crossover directly. Code:
if $$#booktype == 'Fanfiction' then if 'Crossover' in $#fanficcat then 'Fanfiction/Crossover' else list_re($tags, ',', '^Fanfiction.(.*)$', 'Fanfiction/\1') fi Another disavantage: This also relies on 'Crossover' not being in #fanficcat (or Fanfiction.Crossover in tags) when unneeded. Thanks to the way FFF works with multi-fandom stories, this has happened. Based on that second disadvantage, I'm inclined to go with option #1. Does this make sense? Last edited by ownedbycats; 01-07-2023 at 02:17 AM. |
01-07-2023, 05:39 AM | #459 | |
Grand Sorcerer
Posts: 11,789
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Code:
program: # Returns 'Fanfiction/Fandom' tag for fanfics booktype = 'Fanfiction'; tags = 'Fanfiction.Bar, Fanfiction.Crossover, Fanfiction.Foo'; if booktype == 'Fanfiction' then switch_if( '^Fanfiction\.Crossover$' inlist tags, 'Fanfiction/Crossover', # More tests in the order you want '^Fanfiction\.Foo$' inlist tags, 'Fanfiction/Foo', # ... # If none of the tests find a value then end up picking the first one. # If the list is empty then this will return the empty string. # You only need the list_re() if the list can contain values other than # Fanfiction.XXX. list_item(list_re(tags, ',', '^Fanfiction.(.*)$', 'Fanfiction/\1'), 0, ',') ) fi |
|
01-07-2023, 07:48 PM | #460 |
Custom User Title
Posts: 8,841
Karma: 62032373
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Thanks.
I converted one of my templates to get a better idea of how the switch_if works. Is this a good place to use one? Code:
program: switch_if( 'Cleanup' in virtual_libraries(), 'polish.png', 'Fanfiction' in $#booktype, list_split(icons_fanfic(), ':', 'ff'); ff_0, 'Documentations & Manuals' in $#booktype, 'helpbook.png', 'Loans' in virtual_libraries(), 'overdrive.png', 'Physical Books' in virtual_libraries(), 'paperbook.png', approximate_formats()=='PDF', 'pdfbook.png', 'omnibus' inlist $#admintags, 'bookshelf.png', 'book_open.png', ) Spoiler:
(at times like this, sometimes I wish there was some sort of split template editor so I could compare the results of two templates. That's probably not too useful generally though.) Last edited by ownedbycats; 01-07-2023 at 07:51 PM. |
01-08-2023, 03:27 AM | #461 |
Custom User Title
Posts: 8,841
Karma: 62032373
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Another example:
Code:
program: if is_marked() then m = is_marked(); switch_if( 'reading_list_send_to_device' in m, 'sync.png', 'reading_list_to_be_read' in m, 'list.png', 'fff' in m, 'download-metadata.png', 'marked.png' ) fi Original. (Trying to figure out why I used a 'contains' instead of 'in'.) Spoiler:
|
01-08-2023, 06:41 AM | #462 | ||
Grand Sorcerer
Posts: 11,789
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Comment: you often use 'in' instead of 'inlist'. Using 'in' can lead to errors because it can find substrings. For example, if you have virtual libraries named "Loans" and "Temp Loans", the "in" will find them both, while inlist will find only the one as long as you use regular expression anchors on the search value. Quote:
Example: Code:
program: m = is_marked(); switch_if( 'reading_list_send_to_device' == m, 'sync.png', 'reading_list_to_be_read' == m, 'list.png', 'fff' == m, 'download-metadata.png', m, 'marked.png', '' ) |
||
01-08-2023, 06:45 AM | #463 | |
Custom User Title
Posts: 8,841
Karma: 62032373
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
Quote:
|
|
01-08-2023, 06:46 AM | #464 | |
Grand Sorcerer
Posts: 11,789
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
|
|
01-08-2023, 06:55 AM | #465 |
Custom User Title
Posts: 8,841
Karma: 62032373
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
|
I do keep forgetting that virtual_libraries is a list though. I can see that especially messing up since I have 'read' and 'unread' vls.
Also, is there a difference between using approximate_formats and $formats for the =='PDF'? |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Library Management: various questions not worth their own thread | ownedbycats | Library Management | 153 | 05-14-2024 01:30 AM |
[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 |