A question about searching with multiple strings

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Oct 22 06:19:20 EDT 2005


On Fri, 21 Oct 2005 13:39:17 -0700, googleboy wrote:

> 
> 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)

I get very worried when I see code like that. It makes me stop and think
about what it does, and why you would want to do it. I worry about hidden
side effects. Instead of just groking the code instantly, I've got to stop
and think. You're taking a copy of the locals, deleting self from it, and
them updating self's dictionary with them... why? What do you hope to
achieve?

If I were project manager, and one of my coders wrote something like this,
I would expect him or her to have a really good reason for it. I'd be
thinking not only of hidden bugs ("what if there is something in locals
you don't expect?"), but every time a developer has to work on this class,
they have to stop and think about it.

Joel (of Joel On Software fame) talks about code looking wrong and
smelling dirty. This code might work. It might be perfectly safe. But
there's a whiff to this code.

http://www.joelonsoftware.com/articles/Wrong.html



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

You aren't using all_items. Why waste a lookup fetching it?

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

Would you like to give us a couple of examples of items from the CSV file?


> 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

And here we why global variables are Bad: without knowing what p is, how
are we supposed to understand this code?

> This has the limitation of only working over a single search item.

So you are searching items for items... I think you need to use a better
name for your class. What does class Item actually represent?

> 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.

That might work.
 
> I am not entirely sure how to store this subset of Items in such a way
> that I can make searches over it.

How about in a list?

> 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?  

See my next post (to follow).

> I Am also
> usure how to go about creating a function that will accept any number
> of parameters.

def func1(*args):
    for arg in args:
        print arg

def func2(mandatory, *args):
    print "Mandatory", mandatory
    for arg in args:
        print arg

Does that help?



-- 
Steven.




More information about the Python-list mailing list