A question about searching with multiple strings

googleboy mynews44 at yahoo.com
Fri Oct 21 16:39:17 EDT 2005


Hi there.

I have defined a class called Item with several (about 30 I think)
different attributes (is that the right word in this context?).  An
abbreviated example of the code for this is:

class Item(object):

	def __init__(self, height, length, function):
		params = locals()
		del params['self']
		self.__dict__.update(params)
	def __repr__(self):

		all_items = self.__dict__.items()
		return '%s,%s,%s' % (self.height, self.length, self.function)



I have a csv file that I use to store and retrieve all the info about
each Item, one item per line.

I have written a little peice of python that lets me search through all
Items (after reading them into a variable called all_items) and will
return matching results:



for item in all_items:

	strItem = str(item)

	m = re.search(p[i], strItem, flags = re.I)
	if m:
		height = getattr(item, "height")
		length = getattr(item, "length")
		function = getattr(item, "function")
		print "height is %s, length is %s and function is %s" % height,
length, function



This has the limitation of only working over a single search item.  I
want to be able to search over an uncontrollable number of search
strings because I will have people wanting to search over 2, 3 or even
(maybe) as many as 5 different things.

I was thinking that I would try to write a function that created a
sublist of Items if it matched and then run subsequent searches over
the subsequent search strings using this sublist.

I am not entirely sure how to store this subset of Items in such a way
that I can make searches over it.  I guess I have to initialize a
variable of type Item, which I can use to add matching Item's to,  but
I have no idea how to do that....(If it was just a list I could say
"sublist = []",  what do I use for self defined classes?  I Am also
usure how to go about creating a function that will accept any number
of parameters.

Any assistance with these two questions will be greatly appreciated!

Thanks!

googleboy




More information about the Python-list mailing list