View Single Post
Old 12-06-2023, 06:09 AM   #9
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,476
Karma: 8025702
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by kjdavies View Post
How about

Code:
program:
    '.'.join(filter(None, ($#titlecount,$#pagecount,$series,$#pubseries,$#pubseries_index)))
? I think this should make a string concatenating only the non-None values in the list, and prevent leading or trailing '.'.
Problem: that intermixes python and the GPR template language. The template should be rewritten as
Code:
python:
def evaluate(book, context):
    vals = (filter(
                None, 
                (
                    book.get('#myint2'), 
                    book.get('#pagecount'), 
                    book.get('series'), 
                    book.get('#series'),
                    book.get('#series_index')))
                )
    vals = ((str(v) if isinstance(v, (int, float)) else v) for v in vals)
    return '.'.join(vals)
Note: the list_join() template function ignores empty values, not adding them to the result list. None won't be encountered unless you use raw_field() or $$#.
chaley is offline   Reply With Quote