View Single Post
Old 08-11-2016, 03:39 PM   #7
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,463
Karma: 8025600
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by readin View Post
But I'm stuck on this one:

Code:
name: icon_if_val
arg count: 3
doc: icon_if_val(field, val, icon) -- if 'field' contains 'val', returns icon filename ('icon' + '.png'), else returns 'blank.png'
program code:
def evaluate(self, formatter, kwargs, mi, locals, field, val, icon):
	import re
	if re.search(val, mi.get(field)):
		return icon + '.png'
	return 'blank.png'
Result:
EXCEPTION: expected string or buffer
You didn't supply a call example, but I can see one potential problem. If 'field' is like tags then the result from mi.get will be an array (in python a 'list'), not a string. If you know that 'field' is always 'like tags' then you can solve this problem using
Code:
	import re
	f = ','.join(mi.get(field))
	if re.search(val, f):
If you don't know then you can do something like
Code:
	import re
	f = mi.get(field)
	if isinstance(f, list):
		f = ','.join(f)
	if re.search(val, f):
We are getting very technical here.
chaley is offline   Reply With Quote