View Single Post
Old 10-13-2022, 04:43 PM   #56
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,462
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by un_pogaz View Post
Actually...
This is no longer the case. At least not on my repositories.
I have created a system that allows to easily call and use the functions currently loaded in the formatter.
Voilą, voilą.
Before I spend time on this I need to understand what problem you are solving.

You are adding the ability to call builtin formatter functions from python templates. Calling these functions requires you to accept that the functions return strings, even when that doesn't make sense given the underlying data. For example, by looking at the source for formats_sizes() and human_readable() (easily available), assuming I know python I see how to call the underlying db methods. Using that knowledge I can write your template as
Code:
python:
from calibre import human_readable

def evaluate(book, context):
    return ','.join([k+ ':' + human_readable(v['size']) 
                     for k,v in book.get('format_metadata', {}).items()])
which avoids the conversion to/from strings while building the base data.

The same function could be written in TPM as
Code:
program:
	sizes = formats_sizes();
	result = '';
	for fmt in sizes:
		list_split(fmt, ':', 'v');
		result = list_join(',', result, ',', v_0 & ':' & human_readable(v_1), ',')
	rof;
	result
The 'for' statement will do the same split() that your PTM template is doing. The list construction using list_join() is less efficient than your sample template, but not outrageously so.

----

Another question: TPM is quite efficient because it is 'compiled' into an abstract syntax tree. A python template calling a sequence of template functions won't be a lot faster than a GPM template doing the same thing. Many of the inefficiencies of TPM come from list handing, which your proposal doesn't address. What do I gain by using PTM? Does the gain justify the ongoing maintenance cost?

Another issue, probably not important: the arguments(), globals(), and set_globals() functions are implemented directly in the formatter. A user cannot call the functions in .funcs.
chaley is offline   Reply With Quote