Redundant code in multiple methods
Rob Conner
rtconner at gmail.com
Fri Sep 9 15:04:06 EDT 2005
No you don't need to know Zope to help me. The whole reason I'd even
want to do this is because of Zope though. I made a Zope product, and
now want to perfect it.
some simple example code...
<code>
class User:
def View(self):
# play with data here
myHtmlDoc = "pretend this is a uper profile"
return myHtmlDoc
index_html = View
def Edit(self):
# play with data here
myHtmlDoc = "editing the user"
return myHtmlDoc
</code>
So when visiting "website.com/User" zope will call User.index_html() or
when you visit "website.com/User/View" zope will call User.View() In
all of the testing/learning I've done on Zope I'm pretty sure that last
item (index_html or View) must be a method, but possible it only needs
to have an __doc__ attribute (otherwise Zope will Error)
The problem comes when I want to have code put into every method.
Perhaps a counter I want to increment on every visit to a User page.
I can do this..
<snippet>
def View(self):
incrementCounter()
# play with data here
myHtmlDoc = "pretend this is a uper profile"
return myHtmlDoc
index_html = View
def Edit(self):
incrementCounter()
# play with data here
myHtmlDoc = "editing the user"
return myHtmlDoc
</snippet>
... but in reality in my real code that one counter increment line ends
up being 20 lines long. An al lot of that "counter code" is actaully
setting up variables I'd like to access within the scope of the given
method. So if you follow me so far, I was wondering how I might change
things to only have one place where I have to maintain the "setup my
method" code, which is pretty much a lot of the same code typed over
and over into all of the methods.
(for a more real life example of things) -
<snippet>
def View(self):
REQUEST = self.REQUEST
SESSION = REQUEST.SESSION
dbConnection = self.getDBConnection()
logger = self.getLogger()
trackStatsHere()
# set up some local variables here
# change some global variables here
try:
myHtmlDoc = """make the htmldocument here using all
of the previous variables"""
# play with data here
return myHtmlDoc
except:
raise "handle the error here"
finally:
dbConnection.close()
index_html = View
def Edit(self):
REQUEST = self.REQUEST
SESSION = REQUEST.SESSION
dbConnection = self.getDBConnection()
logger = self.getLogger()
trackStatsHere()
# set up some local variables here
# change some global variables here
try:
myHtmlDoc = """make the htmldocument here using all
of the previous variables"""
# play with data here
return myHtmlDoc
except:
raise "handle the error here"
finally:
dbConnection.close()
</snippet>
I would ideally like to do something such as this this, or something
where I don't have all of that redundant code.
<snippet>
def __allmethods__(self, methodname):
"gets called when all methods are called"
REQUEST = self.REQUEST
SESSION = REQUEST.SESSION
dbConnection = self.getDBConnection()
logger = self.getLogger()
trackStatsHere()
# set up some local variables here
# change some global variables here
try:
methodname(localvariables)
except:
raise "handle the error here"
finally:
dbConnection.close()
def View(self, localvariables):
myHtmlDoc = """make the htmldocument here using all
of the previous variables"""
# play with data here
return myHtmlDoc
index_html = View
def Edit(self):
myHtmlDoc = """make the htmldocument here using all
of the previous variables"""
# play with data here
return myHtmlDoc
</snippet>
__getattr__ almost does the trick but not quite. So any suggestions on
how to streamline my code here and make it slightly more maintainable.
More information about the Python-list
mailing list