ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> python-Tutor
python-Tutor
RE: [Tutor] do_it()
by Edward Comber other posts by this author
Dec 11 2003 6:02PM messages near this date
Re: [Tutor] newbie evalpostfix [writing a infix parser in Python] | [Tutor] Re: Photo gallery
Fair enough, but you could have kept the list of words external to the
functions and used by both.

Also you can return two lists like...

def stop_n_go_words(terms):
	blah
	return stop_list, go_list

a_stop_list, a_go_list = stop_n_go_words(terms)

-----Original Message-----
From: tpc@[...].edu [mailto:tpc@[...].edu]
Sent: 11 December 2003 17:24
To: Eddie Comber
Cc: tutor@[...].org
Subject: Re: [Tutor] do_it()



hi Eddie,

I was mulling this over recently, and while it is true the overhead is
much higher with OOP, the payoff is easier to read and more functional
code.  Let me give you an example.  I recently wrote a search engine that
takes as user input a query and returns results from an audio and video
archive.  One of the things I needed to do was filter common, or stop,
words (e.g., who, is, what, the, of) from go words that would be used in
other functions to look up and return results in the index.  I also
wanted to print out the words filtered to the user, so I had to either
duplicate the body of code for a function to return both, or I could
write a class called Filter and have two methods in it,
getUserGoWords(self) and getUserStopWords(self) using the same logic.  The
non-OOP way of doing this would be:

<paste> 
def getUserGoWords(terms):
	stop_words_list = ['a', 'about', 'all', 'an', 'and', 'any', 'are',
'as', 'at', 'be', 'because', 'can', 'did', 'do', 'does', 'for', 'from',
'he', 'how', 'i', 'if', 'in', 'is', 'it', 'may', 'no', 'not', 'of', 'on',
'or', 'that', 'the', 'then', 'these', 'this', 'those', 'to', 'was', 'we',
'what', 'when', 'where', 'which', 'who', 'why', 'with']
	terms_list = terms.split()
	user_go_words = []
	for word in terms_list:
		if word.lower() not in stop_words_list:
			filtered_terms_list.append(word)
	return user_go_words

def getUserStopWords(terms):
	stop_words_list = ['a', 'about', 'all', 'an', 'and', 'any', 'are',
'as', 'at', 'be', 'because', 'can', 'did', 'do', 'does', 'for', 'from',
'he', 'how', 'i', 'if', 'in', 'is', 'it', 'may', 'no', 'not', 'of', 'on',
'or', 'that', 'the', 'then', 'these', 'this', 'those', 'to', 'was', 'we',
'what', 'when', 'where', 'which', 'who', 'why', 'with']
	terms_list = terms.split()
	user_stop_words = []
	for word in terms_list:
		if word.lower() in stop_words_list:
			user_stop_words.append(word)
	return user_stop_words
 </paste> 

and the OOP way of doing this would be:

class Filter:
	def __init__(self, terms):
		# replace ' with '' for MySQL INSERT
		terms = terms.replace("'", "''")
		# this is currently how I handle context searches in Audio Video Archive
		terms = terms.replace('"', "")
		stop_words_list = ['&', 'a', 'about', 'all', 'an', 'and',
'any', 'are', 'as', 'at', 'be', 'because', 'can', 'do', 'does', 'for',
'from', 'he', 'how', 'i', 'if', 'in', 'is', 'it', 'may', 'no', 'not',
'of', 'on', 'or', 'that', 'the', 'then', 'these', 'this', 'those', 'to',
'was', 'we', 'what', 'when', 'where', 'which', 'who', 'why', 'with']
	        terms_list = terms.split()
		self.user_go_words = []
		self.user_stop_words = []
        	for word in terms_list:
        		if word.lower() not in stop_words_list:
        			self.user_go_words.append(word)
        		else:
                                self.user_stop_words.append(word)

	def getUserGoWords(self):
	        return self.user_go_words

	def getUserStopWords(self):
	        return self.user_stop_words

Maybe there is another way of doing this so that one function can return
two values, but as far as I know there is no doubt in my mind which makes
more sense with respect to future readers of my code and the holy mantra
of programming, Keep It Short and Simple.

I hope that helps you.

On Thu, 11 Dec 2003, Eddie Comber wrote:

>  I see that often when people write OO code they have a main() something
>  like:
> 
>  obj = myObject(param, param, param)
>  obj.do_it()  #or obj.action() etc
> 
>  Are there any commanding advantages to making this sort of functionality
>  into an object?
> 
>  The above example is easier written and used as:
> 
>  myfunc(param, param, param)
> 
>  I ask because I have just written a MAKE utility, and having started off
as
>  an object I really don't know what the advantages are above a simple
>  function.
> 
>  Best,
>  Eddie.
> 
> 
>  _______________________________________________
>  Tutor maillist  -  Tutor@[...].org
>  http://mail.python.org/mailman/listinfo/tutor
> 


_______________________________________________
Tutor maillist  -  Tutor@[...].org
http://mail.python.org/mailman/listinfo/tutor

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved