View Single Post
Old 10-16-2011, 12:21 PM   #32
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 Noughty View Post
It's all english. How about extracting values instead or removing them? Like listing the values I want to keep and removing all others.
Use something like the following:
Code:
def evaluate(self, formatter, kwargs, mi, locals):
	# Tags is a multiple text field, so it is already split
	tags = set(mi.get('tags', []))
	# Shelf is a multiple text field, so it is already split
	shelf = set(mi.get('#shelf', []))
	
	# items that must be in the result
	must_have = set(['foo', 'bar', 'somethingElse', 'Comics',
		 'andSoOn', 'until I get tired', 'of adding things']) 

	# combine tags and shelf together
	ts = tags | shelf
	# get the items in the combined tags and shelf that are also in must_have
	res = must_have & ts
	return ', '.join(sorted(res))
The line "must_have & ts" returns a set containing items that are in both must_have and ts. This effectively extracts items in must_have from the set ts.

As for your question about functions in python, the definitive source is the python manual. It can be (is!) overwhelming. However, all the functions we have been using here are described in the section http://docs.python.org/library/stdty...-set-frozenset.
chaley is offline   Reply With Quote