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-15-2021, 10:09 PM   #1
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: 10,970
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
Template: field vs raw_field

I have an integer column with formatting {0}%

Which of these is better practice?

Code:
program:
	if field('#percentread') == '0%' then '1'
	fi
Code:
program:
	if raw_field('#percentread') ==# '0' then '1'
	fi
I know the differences between lexical and numerical but this is exact match so i am not sure if there is any difference. I am using the second currently.

Last edited by ownedbycats; 03-16-2021 at 01:41 AM.
ownedbycats is offline   Reply With Quote
Old 03-16-2021, 05:04 AM   #2
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,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Which of these is better practice?
It depends.

The underlying problem is the same as you solve with check_yes_no(), what to do with undefined values. What do you want to happen in this case? Is 'undefined' the same as '0'? Is it something else? If defined, what comparisons are needed?

For integers the field() function returns the empty string if the book's value is not defined, otherwise (as you said) the formatted value. If undefined is to be treated as zero and you aren't doing any non-equal comparisons then you need
Code:
v = field('#percentread');
if !v || v == '0%' then
With raw_field() you have the choice of what is returned for undefined values. For example:
  • x is undefined: raw_field(x) returns 'none'
  • x is undefined: raw_field(x, 0) returns 0 (zero)
  • x is undefined: raw_field(x, -1000) returns -1000
  • x is undefined: raw_field(x, '') returns the empty string
  • x is undefined: raw_field(x, 'twas brillig') returns the string 'twas brillig'
  • x is defined: all the above raw_field() examples return x unformatted.
In the end, what you do is a matter of personal preference. You must understand what the code does for the possible range of values. You must be able to maintain it, which means understanding the code again in the future.

My preference is to use raw_field, setting the default to what makes sense in the context. That isolates me from the vagaries of formatting (which can change) and lets me use non-identical comparisons without thinking too much about it. If we assume that undefined values are to be treated as zero then this:
Code:
v = raw_field('lookup key', 0)
if v ==# 0 then
If later I look at this code then it is easy for me to know that the field is numeric and undefined values are assumed to be zero. It will work if later I decide that I want decimal percentages, e.g., 43.5% or 0.0%.

And yes, I know I get rather pedantic.
chaley is offline   Reply With Quote
Advert
Old 03-24-2021, 02:07 AM   #3
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: 10,970
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
On this topic:

Code:
program:
	if raw_field('#percentread') ==# '0' then '1'
	else raw_field('#percentread')
	fi
Running this on a book with #percentread undefined returns result as 1.What is the proper way to make it remain undefined?

I changed it to raw_field('#percentread', '1') and it remained undefined but that felt kludge. Empty string '' returned a 1 not sure why.

Last edited by ownedbycats; 03-24-2021 at 02:31 AM.
ownedbycats is offline   Reply With Quote
Old 03-24-2021, 04:37 AM   #4
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,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
On this topic:

Code:
program:
	if raw_field('#percentread') ==# '0' then '1'
	else raw_field('#percentread')
	fi
Running this on a book with #percentread undefined returns result as 1.What is the proper way to make it remain undefined?

I changed it to raw_field('#percentread', '1') and it remained undefined but that felt kludge. Empty string '' returned a 1 not sure why.
Here is what I would do.
Code:
program:
	v = raw_field('#myint', '');
	if !v then
		''
	elif v ==# 0 then
		'1'
	else
		 v
	fi
This is the same thing but not using the second parameter of raw_field.
Code:
program:
	v = raw_field('#myint');
	if v == 'none' then
		''
	elif v ==# 0 then
		'1'
	else
		 v
	fi
chaley is offline   Reply With Quote
Old 03-24-2021, 05:21 AM   #5
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: 10,970
Karma: 75337983
Join Date: Oct 2018
Location: Canada
Device: Kobo Libra H2O, formerly Aura HD
That works. What exactly does the !v do?
ownedbycats is offline   Reply With Quote
Advert
Old 03-24-2021, 05:27 AM   #6
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,444
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
That works. What exactly does the !v do?
The ! (bang) is the logical 'not' operator. "Bang v" returns True ('1') if v is False (''), or False ('') if v is True (anything but '').
chaley is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
custom field based on multiple value field for export nindotza Calibre 0 01-02-2021 08:37 AM
Using built-in template functions in a custom template function ilovejedd Library Management 4 01-28-2018 12:20 PM
Copy data from Title field to IDs field rlh3 Library Management 9 09-24-2012 08:12 AM
Default value for read field (Yes/No-field) opitzs Library Management 3 11-09-2011 07:17 AM
Template error value:unknown field null sfuller Calibre 4 06-13-2011 03:02 PM


All times are GMT -4. The time now is 12:38 AM.


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