Patterns and all that new fangled stuff ...

Max M maxm at mxm.dk
Wed Jul 3 08:20:55 EDT 2002


For the fun and education of it I have recently tried to refactor one of 
my projects using patterns.

I did expect the code to be longer and more verbose, but was a little 
surprised when I found out that it got exactly twice as long ...

But I know the code too well and cannot be objective about it, so I 
thought I would like to get a second opinion. Which code would you guys 
prefer to maintain down the line. The short "linear" version or the 
object oriented "pattern" version. Which one is the easiest to read?

It as an .asp project. I have chosen not to include the html part of it 
as it just inserts html generated from the Python code.

regards Max M

Btw. It is a page from a project management system


####################################################
# Short version first
####################################################

import html_tools, iisUtils, mx
import Tables
import Factory

req = iisUtils.request(Request)

# date start and stop
startDate, endDate = html_tools.getDateRange(req)
dateWidgets = html_tools.dateRangeWidgets(startDate, endDate)

table = []

# add simple calendar at top of graph
dayRange = html_tools.DayRange(startDate, endDate)
monthNumbers, dayNumbers, weekNumbers = html_tools.topCalendar(dayRange)
table.append(['<class="bread">Måned</class>', monthNumbers])
table.append(['<class="bread">Uge</class>', weekNumbers])
table.append(['<class="bread">Dag</class>', dayNumbers])

sager = Factory.factory('RM_Sagkart', 'Sag')
sager.getAll()
sager.sort('Sagsnavn')
for sag in sager:

     start = sag.StartDato
     slut  = sag.SlutDato
     attr = {'sagsId':sag.GlobalObjectID}
     attr.update({'startDay':start.day, 'startMonth':start.month,
             'startYear':start.year})
     attr.update({'endDay':slut.day, 'endMonth':slut.month,
             'endYear':slut.year})

     table.append(['<nobr>' + html_tools.url('case.asp', sag.getTitle(),
                                             **attr),
             sag.asGantRow(startDate, endDate, **attr)])
content = str(html_tools.Table(table))



####################################################
# Then the long version
####################################################

import html_tools, iisUtils, mx
import Tables
import Factory
import HtmlViewAdapter


class PageFacade:

     """
     This is where all the logic takes place on the page,
     and all the views are generated
     """

     def __init__(self, startDate, endDate):
         "Set up all objects and data used on the page"
         self.startDate = startDate
         self.endDate = endDate
         self.dayRange = html_tools.DayRange(
                           self.startDate, self.endDate)
         self.sager = Factory.factory('RM_Sagkart', 'Sag')
         self.sager.getAll()
         self.sager.sort('Sagsnavn')

     def dateWidgets(self):
         return html_tools.dateRangeWidgets(startDate, endDate)

     def dateDict(self):
         s, e = self.startDate, self.endDate
         return {
             'startYear':s.year, 'startMonth':s.month, 'startDay':s.day,
             'endYear':e.year, 'endMonth':e.month, 'endDay':e.day
         }

     def content(self):
         table = []
         ta = table.append
         # add simple calendar at top of graph
         monthNumbers, dayNumbers, weekNumbers = html_tools.topCalendar(
                                                           self.dayRange)
         ta(['<class="bread">Måned</class>', monthNumbers])
         ta(['<class="bread">Uge</class>', weekNumbers])
         ta(['<class="bread">Dag</class>', dayNumbers])
         for sag in self.sager:
             sagHtmlView = sagHtmlViewAdapter(sag)
             ta([sagHtmlView.title(), sagHtmlView.gantRow()])
         return str(html_tools.Table(table))



class sagHtmlViewAdapter(HtmlViewAdapter.HtmlViewAdapter):

     "Creates adapter to view sag in html"

     def getAttrs(self):
         attrs = {'sagsId':self._obj.GlobalObjectID}
         attrs.update(page.dateDict())
         return attrs

     def title(self):
         return '<nobr>' + html_tools.url(
             'case.asp', self._obj.Sagsnavn or '???', **self.getAttrs()
             )

     def gantRow(self):
         attrs = {'sagsId':self._obj.GlobalObjectID}
         attrs.update(page.dateDict())
         return self._obj.asGantRow(
             page.startDate, page.endDate, **self.getAttrs()
             )


###############################
# This gets the initial input from the request,
# calls the Pagefacade and starts it all

req = iisUtils.request(Request)
startDate, endDate = html_tools.getDateRange(req)
page = PageFacade(startDate, endDate)




More information about the Python-list mailing list