View Single Post
Old 12-11-2022, 08:03 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,480
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Yes, that's where I figured to put it. Should've bee a little more clear. What I'm a bit confused on is whether it could be plugged into a template (presumably Action Chains' single-field edit) to convert an identifier from isbn10 to 13.
Sure. It is a template function like any other. You just make a GPM template to call it.
Code:
program: whatever_you_called_it()
You could instead make it a stored template instead of a template function.

Example:
Click image for larger version

Name:	Clipboard02.png
Views:	107
Size:	39.9 KB
ID:	198334

This template is slightly different from the function above, correcting a bug and making it return the existing isbn if it isn't 10 long.
Code:
def evaluate(book, context):
	isbn = book.identifiers.get('isbn', '')
	if len(isbn) != 10:
		# This isn't an isbn 10. Return it as it is.
		return isbn
	prefix = '978' + isbn[:-1]
	check = check_digit_13(prefix)
	return prefix + check

def check_digit_13(isbn):
	sum = 0
	for i in range(len(isbn)):
		c = int(isbn[i])
		if i % 2: w = 3
		else: w = 1
		sum += w * c
	r = 10 - (sum % 10)
	if r == 10: return '0'
	else: return str(r)
Call it with
Code:
program: isbn10_to_13()
chaley is offline   Reply With Quote