View Single Post
Old 09-29-2018, 02:59 PM   #3
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,465
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by sweth View Post
Hmm. Is the answer maybe template functions? I was able to define a sum() function that appears to work (once I realized that args are passed in as unicode and needed to be converted before being added), but am a python newbie so the extraction of the Ns from an arbitrary number of tags is still befuddling me...
Given your use case, a template function is definitely the way to go.

Are you asking how to extract the N values in a template function? If the following is true:
  • The source of the values is always the field "tags".
  • The pattern is always 'recommended\..*\.[\d.]+'
Then something like the following should work for you:
Code:
def evaluate(self, formatter, kwargs, mi, locals):
	import re
	tags = mi.get('tags')
	v = 0
	if tags:
		for t in tags:
			m = re.match('recommended\..*\.([\d.]+)', t)
			if m:
				v = v + int(m.group(1))
	return v
chaley is offline   Reply With Quote