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 06-04-2023, 06:52 AM   #631
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: 11,801
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
...

This removes the 'Culture & Regions' items from split_tags, extracts the lastmost item, and merges them back in.

However, it only works with one item. After splitting the culture_tags list with ',', how do I process each item separately?

Example: Fiction.Cultures & Regions.Canada.Western Canada, Fiction.Cultures & Regions.Metis, Fiction.Historical Fiction
Preferred result (after sorting): Historical Fiction, Metis, Western Canada
Actual result: Historical Fiction, Metis
It is hard to understand what you are trying to do. What you say the code does and what the code actually does are different.

Reading what you say and the code, it appears that:
  • if a tag contains the item "Cultures & Regions" then you want the last item.
  • otherwise you want all items split into individual tags.
However, the examples don't agree with this. Your code will include both the "Fiction" and "Historical Fiction" of "Fiction.Historical Fiction" while your example output does not. Which is right?

This template does one version of what you might be asking for.
Code:
program:
	tags = 'Fiction.Cultures & Regions.Canada.Western Canada, Fiction.Cultures & Regions.Metis, Fiction.Historical Fiction, Fiction.Bar.Mumble';
	new_tags = '';

	for t in tags:
		if '\.Cultures & Regions\.' in t then
			nt = list_item(t, -1, '.')
		else
			nt = re(t, '\.', ',')
		fi;
		new_tags = list_union(new_tags, nt, ',')
	rof;
	list_sort(new_tags, 0, ',')
The output is
Code:
Bar, Fiction, Historical Fiction, Metis, Mumble, Western Canada
chaley is offline   Reply With Quote
Old 06-04-2023, 07:14 AM   #632
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: 8,873
Karma: 62040409
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Quote:
Originally Posted by chaley View Post
It is hard to understand what you are trying to do. What you say the code does and what the code actually does are different.

Reading what you say and the code, it appears that:
  • if a tag contains the item "Cultures & Regions" then you want the last item.
  • otherwise you want all items split into individual tags.
However, the examples don't agree with this. Your code will include both the "Fiction" and "Historical Fiction" of "Fiction.Historical Fiction" while your example output does not. Which is right?
That's because I left out part of the template and forgot to account for it in the examples.

Code:
program:

	if 
		'^(Fiction|Nonfiction|Magazines & Publications)' in $#booktype 
	then 
## Remove 'Culture & Regions' items as they're processed differently
		split_tags = list_re_group($tags, ',', '.', '(^.*Cultures & Regions.*$)', '');
## Split tags or return an empty
		split_tags = re(split_tags, '\.', ',') 
	else
		split_tags = ''
	fi;

## Cultures & Regions - list_item with a '.' separator to get the lastmost item
	cultures_tags = list_re($tags, ',', 'Cultures & Regions', '');
	cultures_tags = list_item(cultures_tags, -1, '.');
## Re-merge this with split_tags
	split_tags = list_union(split_tags, cultures_tags, ',');

## Removing a few unwanteds and sorting
	cleaned_tags = list_sort(list_difference(
		split_tags,
		'[Cleanup], Fiction, Nonfiction, Magazines & Publications, Topics',
		','), 0, ',');

## Add 'Omnibus' if applicable
	if 
		'omnibus' 
	in 
		$#admintags 
	then 
		cleaned_tags  = 'Omnibus' & if cleaned_tags then ', ' & cleaned_tags fi
	fi;

	cleaned_tags
ownedbycats is online now   Reply With Quote
Advert
Old 06-11-2023, 08:37 PM   #633
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: 8,873
Karma: 62040409
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
What would be the best way to remove excessive whitespaces in the middle of a value? (Calibre seems to remove them at the beginnings and endings.)

I was thinking something like re checking for two or more spaces but wasn't sure if there was a better way.

Last edited by ownedbycats; 06-12-2023 at 12:30 AM.
ownedbycats is online now   Reply With Quote
Old 06-12-2023, 12:28 AM   #634
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: 8,873
Karma: 62040409
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Additional question:

I have this template for listing the first letter (or 0-9 if numeric)

Code:
{title_sort:'contains(t=uppercase(re($, '^\W*(\w).*$','\1')), '\d', '0-9', t)'}
I'd like to use transliterate() to standardize the letters (currently accented characters shown separately). How would I add that?

Last edited by ownedbycats; 06-12-2023 at 12:31 AM.
ownedbycats is online now   Reply With Quote
Old 06-12-2023, 05:37 AM   #635
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: 11,801
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
What would be the best way to remove excessive whitespaces in the middle of a value? (Calibre seems to remove them at the beginnings and endings.)

I was thinking something like re checking for two or more spaces but wasn't sure if there was a better way.
I would use
Code:
re(val, '\s+')
Quote:
Originally Posted by ownedbycats View Post
Additional question:

I have this template for listing the first letter (or 0-9 if numeric)

Code:
{title_sort:'contains(t=uppercase(re($, '^\W*(\w).*$','\1')), '\d', '0-9', t)'}
I'd like to use transliterate() to standardize the letters (currently accented characters shown separately). How would I add that?
Code:
{title_sort:'contains(t=transliterate(uppercase(re($, '^\W*(\w).*$','\1'))), '\d', '0-9', t)'}
chaley is offline   Reply With Quote
Advert
Old 06-17-2023, 05:35 PM   #636
estherflails
Member
estherflails began at the beginning.
 
Posts: 23
Karma: 10
Join Date: Jul 2021
Device: none
Hello! Does switch_if have a [pattern, value] limit? When I tried to add over 20 pairs, it started returning the first value even if it was false.

This worked (it has 20 pairs):
Code:
count = raw_field('#chapters');

chapters_count = switch_if(count>=95, 95, count>=90, 90, count>=85, 85, count>=80, 80, count>=75, 75, count>=70, 70, count>=65, 65, count>=60, 60, count>=55, 55, count>=50, 50, count>=45, 45, count>=40, 40, count>=35, 35, count>=30, 30, count>=25, 25, count>=20, 20, count>=15, 15, count>=10, 10, count>=5, 5, count>=1, 1, 0);
This didn't (it has 21 pairs):
Code:
count = raw_field('#chapters');

chapters_count = switch_if(count>=100, 100, count>=95, 95, count>=90, 90, count>=85, 85, count>=80, 80, count>=75, 75, count>=70, 70, count>=65, 65, count>=60, 60, count>=55, 55, count>=50, 50, count>=45, 45, count>=40, 40, count>=35, 35, count>=30, 30, count>=25, 25, count>=20, 20, count>=15, 15, count>=10, 10, count>=5, 5, count>=1, 1, 0);
Or actually, it did work when 'count' was '1', but only then.
estherflails is offline   Reply With Quote
Old 06-17-2023, 06:14 PM   #637
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: 8,873
Karma: 62040409
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
When I tested your 21-pair template with #chaptercount, it produced the correct results.
ownedbycats is online now   Reply With Quote
Old 06-17-2023, 06:43 PM   #638
estherflails
Member
estherflails began at the beginning.
 
Posts: 23
Karma: 10
Join Date: Jul 2021
Device: none
For some reason it doesn't work for me :/ I even updated Calibre to the newest version but it didn't help. Any idea what the problem could be?
estherflails is offline   Reply With Quote
Old 06-17-2023, 07:32 PM   #639
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: 11,801
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
When I tested your 21-pair template with #chaptercount, it produced the correct results.
Quote:
Originally Posted by estherflails View Post
For some reason it doesn't work for me :/ I even updated Calibre to the newest version but it didn't help. Any idea what the problem could be?
The problem is you are using text relational operators ('>=') to compare numeric values. With a text compare, 100 is less than 4 because it does a lexical comparison. You must use numeric relationals, as in '>=#'. See "Relational operators" section in The calibre template language.

Here is your template, corrected.
Code:
count = raw_field('#chapters');

chapters_count = switch_if(
    count >=# 100, 100, count >=# 95, 95, count >=# 90, 90, 
    count >=# 85, 85, count >=# 80, 80, count >=# 75, 75, 
    count >=# 70, 70, count >=# 65, 65, count >=# 60, 60, 
    count >=# 55, 55, count >=# 50, 50, count >=# 45, 45, 
    count >=# 40, 40, count >=# 35, 35, count >=# 30, 30, 
    count >=# 25, 25, count >=# 20, 20, count >=# 15, 15, 
    count >=# 10, 10, count >=# 5, 5, count >=# 1, 1, 0);
chaley is offline   Reply With Quote
Old 06-17-2023, 08:13 PM   #640
estherflails
Member
estherflails began at the beginning.
 
Posts: 23
Karma: 10
Join Date: Jul 2021
Device: none
Quote:
Originally Posted by chaley View Post
The problem is you are using text relational operators ('>=') to compare numeric values. With a text compare, 100 is less than 4 because it does a lexical comparison. You must use numeric relationals, as in '>=#'. See "Relational operators" section in The calibre template language.
Thank you, I had no idea this was a thing. Learned something new today
estherflails is offline   Reply With Quote
Old 06-17-2023, 08:18 PM   #641
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: 8,873
Karma: 62040409
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
I'm still curious as to why it worked as expected for me but not estherflails.
ownedbycats is online now   Reply With Quote
Old 06-17-2023, 08:35 PM   #642
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: 11,801
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
I'm still curious as to why it worked as expected for me but not estherflails.
Probably because of the choice of input values. Using lexical compares, the only values that fail are ones that are not 2 digits: '100' and '1'-'9'.

You could make it work lexically by formatting the test value with leading zeros.

Last edited by chaley; 06-17-2023 at 08:42 PM. Reason: Changed '0'-'9' to '1'-'9' because zero would work unless the value is zero padded.
chaley is offline   Reply With Quote
Old 06-17-2023, 08:38 PM   #643
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: 11,801
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by estherflails View Post
Thank you, I had no idea this was a thing. Learned something new today
This is a consequence of the template language being untyped, or perhaps better said typed only as string. Because it doesn't know the difference between a string and a number, you must tell it what to do by using different operators.
chaley is offline   Reply With Quote
Old 07-22-2023, 11:27 AM   #644
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: 8,873
Karma: 62040409
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
I have a template that produces a list of dates all formatted the same (e.g. '2020-04-16, 2023-07-10').

How do I apply format_date() to these?

Last edited by ownedbycats; 07-22-2023 at 11:38 AM.
ownedbycats is online now   Reply With Quote
Old 07-22-2023, 12:05 PM   #645
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: 11,801
Karma: 7029971
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
I have a template that produces a list of dates all formatted the same (e.g. '2020-04-16, 2023-07-10').

How do I apply format_date() to these?
Best would be to do it when you first build the list.

To do it after the fact, use something like this template:
Code:
program:
	date_list = '2020-04-16, 2023-07-10';
	formatted_list = '';
	for date in date_list:
		formatted_list = list_join(', ', formatted_list, ', ', format_date(date, 'dd-MMM-yyyy'), ', ')
	rof;
	formatted_list
Given your example input, it produces
Code:
16-Apr-2020, 10-Jul-2023
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 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


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


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