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 03-26-2022, 12:58 PM   #361
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,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Question: What would be the best way to take an integer (say, from days_between) and then turn that into "x years, x months, x days"?

I started trying to make a template using fractional_part and I think multiply but it didn't come out right.
The biggest problem is that years and months are not fixed length. Is the year a leap year? How many days are in the month?

If you are willing to fix the number of days in a year to 360 and the number of days in a month to 30 then the computation is easy.
days= whatever;
years = floor(days/360);
months = floor (mod(days, 360)/30);
days = days - ((years*360) + (months * 30))
chaley is offline   Reply With Quote
Old 03-26-2022, 01:48 PM   #362
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,035
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Yes, I wasn't going for an exact number -- just a rough idea of how long it would be. Thanks.
ownedbycats is offline   Reply With Quote
Advert
Old 03-26-2022, 03:25 PM   #363
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,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Yes, I wasn't going for an exact number -- just a rough idea of how long it would be. Thanks.
I got back in front of a computer. Here is a tested version.
Code:
program:
	days = 912;
	years = if (x = floor(days/360)) > 0 then x else '' fi;
	months = if (x = floor(mod(days, 360)/30)) > 0 then x else '' fi;
	days = if (x = days - ((years*360) + (months * 30))) > 0 then x else ''fi;
	strcat(
		if years then strcat (years, if years == 1 then ' year ' else ' years ' fi) fi,
		if months then strcat (months, if months == 1 then ' month ' else ' months ' fi) fi,
		if days then strcat (days, if days == 1 then ' day ' else ' days ' fi) fi
	)
The repeated code to make the plural bothers me, as it has before. I am going to look at defining functions. Something like this, which doesn't yet work of course.
Code:
program:
	days = 912;
	years = if (x = floor(days/360)) > 0 then x else '' fi;
	months = if (x = floor(mod(days, 360)/30)) > 0 then x else '' fi;
	days = if (x = days - ((years*360) + (months * 30))) > 0 then x else ''fi;

	function to_plural(v, str) {
		if v == 0 then return '' fi;
		p = if v == 1 then str else strcat(str, 's') fi;
		strcat(v, ' ', p, ' ')
	}

	strcat(
		to_plural(years, 'year'),
		to_plural(months, 'month'),
		to_plural(days 'day')
	)
The keyword "function" creates a "temporary" stored template with the function name. The arguments() call will be automatically generated from the argument list. For various reasons these temporary stored templates can't be recursive, which is true of 'real' stored templates as well.

I haven't worked out what happens if there is a name collision. It is likely that I won't allow them to avoid nested symbol tables and all that comes with those.

I am also getting tired of writing
Code:
strcat('foo', 'bar')
and am considering making an inline form, possibly
Code:
'foo' & 'bar'
chaley is offline   Reply With Quote
Old 03-26-2022, 07:19 PM   #364
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,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
This now works. I need to play a bit more with it.
Code:
program:
	days = 2112;
	years = floor(days/360);
	months = floor(mod(days, 360)/30);
	days = days - ((years*360) + (months * 30));

	def to_plural(v, str):
		if v == 0 then return '' fi;
		return v & ' ' & (if v == 1 then str else str & 's' fi) & ' '
	fed;

	to_plural(years, 'year') & to_plural(months, 'month') & to_plural(days,'day')
The '&' operator (strcat) is there and used in the above template.

I decided to use the same syntax to define functions as used everywhere else, defining the function body with def/fed instead of {}. This is like if/fi and for/rof. As well as being consistent it avoids some problems with matching blocks and also makes using semicolons more consistent.
Attached Thumbnails
Click image for larger version

Name:	Clipboard01.jpg
Views:	522
Size:	42.3 KB
ID:	192963  
chaley is offline   Reply With Quote
Old 03-28-2022, 03:00 PM   #365
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,035
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Thanks. That'll be useful.

New question:

Code:
program:

	if $#fanficcat then 'Fanfiction'
	elif $#manualtype then 'Documentation & Manuals'
	else re(sublist($tags, 0, 1, ','), '^(.*?)($|\..*$)', '\1')
	fi
I use this to semi-automatically set an enumerated #booktype column.

It mostly works unless I have the [Cleanup] tag, which is always listed first and not in the enumerated list.

Is there a way to do either of the following?

1. Tell it to skip to the next tag if it finds [Cleanup].
2. Tell it to search for the first (foo|bar) tag, where the pattern matches the enumerated values.

I suspect the latter would be a better option as I sometimes don't clean up bulk-imported tags first, so something like 'Animals' or 'Biography' would come up before Fiction|Nonfiction.

Thanks.

Last edited by ownedbycats; 03-28-2022 at 03:04 PM.
ownedbycats is offline   Reply With Quote
Advert
Old 03-28-2022, 03:38 PM   #366
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,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
New question:
[...]
Is there a way to do either of the following?

1. Tell it to skip to the next tag if it finds [Cleanup].
2. Tell it to search for the first (foo|bar) tag, where the pattern matches the enumerated values.

I suspect the latter would be a better option as I sometimes don't clean up bulk-imported tags first, so something like 'Animals' or 'Biography' would come up before Fiction|Nonfiction.

Thanks.
First issue: the idea of "first". There is no guaranteed order of the tags in the list. Second: there isn't a template function that returns the valid values for an enumerated column.

If you mean "match any of a list of tags such as "Fiction|Nonfiction" then you can do that.
Code:
program:

	if $#fanficcat then 'Fanfiction'
	elif $#manualtype then 'Documentation & Manuals'
	else
		t = list_intersection($tags, "Fiction, Nonfiction", ',');
		re(sublist(t, 0, 1, ','), '^(.*?)($|\..*$)', '\1')
	fi
That said, I don't know what the re() is doing so I don't know whether what I suggest can work. It looks like it is trying to isolate the first part of a hierarchical tag. If so then this might work by getting the first of an acceptable value:
Code:
program:

	if $#fanficcat then 'Fanfiction'
	elif $#manualtype then 'Documentation & Manuals'
	else
		sublist(list_intersection(subitems($tags, 0, 1),  "Fiction, Nonfiction", ','), 0, 1, ',')
	fi

Last edited by chaley; 03-28-2022 at 05:02 PM.
chaley is offline   Reply With Quote
Old 03-28-2022, 04:38 PM   #367
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,035
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Yes, I forgot to mention it was to get the first section of a hierarchical tag. The second one works well. Thank you.
ownedbycats is offline   Reply With Quote
Old 04-10-2022, 12:58 AM   #368
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,035
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Question: I swapped a bool column to a psuedobool composite returning yes/no. This broke existing check_yes_nos. What's the best option to replace it?

$#column == 'Yes' is what I'm using for now.

edit: I am curious as to whether check_yes_no could be extended to bool columns checking for specifically yes/true and no/false, similar to the checkmarks option. Probably too much effort tho.

Last edited by ownedbycats; 04-10-2022 at 01:31 AM.
ownedbycats is offline   Reply With Quote
Old 04-10-2022, 05:29 AM   #369
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,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Question: I swapped a bool column to a psuedobool composite returning yes/no. This broke existing check_yes_nos. What's the best option to replace it?

$#column == 'Yes' is what I'm using for now.

edit: I am curious as to whether check_yes_no could be extended to bool columns checking for specifically yes/true and no/false, similar to the checkmarks option. Probably too much effort tho.
You can do this easily with a stored template that mimics what check_yes_no() does. Like this:

The stored template:
Code:
program:
	arguments(column, is_undefined, is_false, is_true);
	f = field(column);
	if is_undefined == 1 && !f then return 'yes' fi;
	if is_false == 1 && f == 'no' then return 'yes' fi;
	if is_true == 1 && f == 'yes'then return 'yes' fi
The template assumes that the column returns '' for undefined, 'yes' for true, and 'no' for false. Of course it can be adapted to other values. It works with any column type that returns those values, including bool columns.

Example use:
Code:
program:
	my_check_yes_no('#mybool', 0, 0, 1) & ' - ' & $#mybool & '.'
This shows the result of calling my_check_yes_no and the value in the column itself, separated by a '-'.

If you don't care about exact compatibility with the builtin check_yes_no() then you can pass the field value instead of the field name. Also you can use '1' and '' instead of '1' and '0', which would be a bit faster because it wouldn't need the '== 1' part of the test.
chaley is offline   Reply With Quote
Old 04-10-2022, 07:35 AM   #370
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,035
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Thanks. Is the yes/no case-sensitive? It doesn't seem to be but helpful to know.
ownedbycats is offline   Reply With Quote
Old 04-10-2022, 08:55 AM   #371
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,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Thanks. Is the yes/no case-sensitive? It doesn't seem to be but helpful to know.
All the string comparison operators are not case sensitive.
chaley is offline   Reply With Quote
Old 04-10-2022, 10:07 PM   #372
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,035
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Would the latter one be a bit better for performance? $#kobostatus is enumerated.

Code:
program:

	if $#kobostatus=='[Send to Device]' then 'No'
	elif $#kobostatus then 'Yes'
	fi
Code:
program:

	if $#kobostatus
		then 
			if $#kobostatus == '[Send to Device]' then 'No'
			else 'Yes'
		fi
	fi

Last edited by ownedbycats; 04-10-2022 at 10:11 PM.
ownedbycats is offline   Reply With Quote
Old 04-11-2022, 04:27 AM   #373
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,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Would the latter one be a bit better for performance? $#kobostatus is enumerated.

Code:
program:

	if $#kobostatus=='[Send to Device]' then 'No'
	elif $#kobostatus then 'Yes'
	fi
Code:
program:

	if $#kobostatus
		then 
			if $#kobostatus == '[Send to Device]' then 'No'
			else 'Yes'
		fi
	fi
The second is faster if $#kobostatus is empty more often than not. Otherwise they are essentially equivalent.
chaley is offline   Reply With Quote
Old 04-16-2022, 07:40 AM   #374
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,035
Karma: 75555555
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Quick question: Is a days_between with the current date the only way to check "specified datetime column is more/less/equal to xx days ago" in a template?

If there isn't, I already am figuring out out a days_ago() stored template—though how would I set
a variable to choose between ==#|<=#|>=#?

Last edited by ownedbycats; 04-16-2022 at 07:57 AM.
ownedbycats is offline   Reply With Quote
Old 04-16-2022, 08:06 AM   #375
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,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Quick question: Is a days_between with the current date the only way to check "specified datetime column is more/less/equal to xx days ago" in a template?

If there isn't, I'll probably figure out a days_ago() stored template with variables for the column name and number of days.
It isn't the only way but it is the easiest way.

You can also do it with timestamps, using format_date(date, 'to_number') to get the timestamp. Get the timestamp for both dates and do the arithmetic. This lets you get down to differences of seconds. Example:
Code:
program:
	def seconds_between(d1, d2):
		t1 = format_date(d1, 'to_number');
		t2 = format_date(d2, 'to_number');
		return t1 - t2
	fed;

	def seconds_to_days(s):
		s / 86400
	fed;

	s = seconds_between(today(), '20220101');
	'seconds: ' & s & ', days; ' & seconds_to_days(s)
produces (today)
Code:
seconds: 9115307.01234293, days; 105.5012385687839
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 08:48 PM.


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