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-08-2023, 04:02 PM   #466
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,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Also, is there a difference between using approximate_formats and $formats for the =='PDF'?
No. The both refer to the formats in the database without looking at what is on the disk. The formats_*() functions look at the disk, which makes them much slower.
chaley is offline   Reply With Quote
Old 01-08-2023, 07:56 PM   #467
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: 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:

Click image for larger version

Name:	2023-01-08 23_05_12-Template tester.png
Views:	437
Size:	25.2 KB
ID:	198903

Last edited by ownedbycats; 01-08-2023 at 11:45 PM.
ownedbycats is offline   Reply With Quote
Advert
Old 01-09-2023, 02:31 AM   #468
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: 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.
ownedbycats is offline   Reply With Quote
Old 01-09-2023, 08:59 AM   #469
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,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
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.)
No. The only way to do it that I can think of is to make a list containing all possible items, then loop through that list using for. If the actual list contains the current item then add it to the result list, otherwise skip it. Something like this:
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:
edit: can I check just one column with a first_non_empty?
I don't understand. Are you asking if you can have only one rule? Yes.

Last edited by chaley; 01-09-2023 at 09:04 AM. Reason: Removed incorrect text in the last sentence.
chaley is offline   Reply With Quote
Old 01-09-2023, 09:03 AM   #470
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,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
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:
You misunderstand the syntax. The first argument is a value, perhaps a $column selector. The rest of the arguments are (pattern, value) pairs. The pattern is checked against the first argument. If it matches then the value is returned.

The syntax you are using is switch_if().
chaley is offline   Reply With Quote
Advert
Old 01-09-2023, 02:48 PM   #471
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: 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.
ownedbycats is offline   Reply With Quote
Old 01-09-2023, 04:13 PM   #472
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,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
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
	)
That works well. If you haven't already done it, put the checks in probability order.

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",
		''
	)
chaley is offline   Reply With Quote
Old 01-09-2023, 07:44 PM   #473
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: 11,044
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Quote:
Originally Posted by chaley View Post
That works well. If you haven't already done it, put the checks in probability order.
Specifically, I wanted to pick the "highest" rating from the string, hence the specific order. Explicit is first, even though it's the rating I have the fewest books of.
ownedbycats is offline   Reply With Quote
Old 01-10-2023, 09:22 PM   #474
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: 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.
ownedbycats is offline   Reply With Quote
Old 01-11-2023, 09:33 AM   #475
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,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
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.
This would really be sub-variant of switch, such as switch_list(). As proposed the function eliminates the second piece of the pair, the result, which I think is over-specialized. I think switch_list() is useful and will look at implementing it.

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), $,
    '')
chaley is offline   Reply With Quote
Old 01-11-2023, 04:51 PM   #476
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,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
EDIT: Perhaps as a custom function.
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 ''
The list match isn't a regex and is case sensitive. EDIT: it is trivial to make it case insensitive.

Calling it:
Code:
program:
	first_in_list($tags, ',', 'a, b, Space Opera, c')
When called with a book with tags "Science Fiction, Space Opera, onDevice" it returns "Space Opera".

Last edited by chaley; 01-11-2023 at 05:13 PM.
chaley is offline   Reply With Quote
Old 01-11-2023, 05:45 PM   #477
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: 11,044
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
That works! I'll use it until (if) switch_list() is added.
ownedbycats is offline   Reply With Quote
Old 01-16-2023, 07:03 AM   #478
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: 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.
ownedbycats is offline   Reply With Quote
Old 01-17-2023, 04:52 PM   #479
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: 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().
ownedbycats is offline   Reply With Quote
Old 01-17-2023, 05:12 PM   #480
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,453
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
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().
The template will internally throw an exception if a function doesn't exist. In the case of save_to_disk, this exception propagates out and the save fails.

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.
chaley is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
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


All times are GMT -4. The time now is 04:23 PM.


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