Dr. Who wrote: > I have a tool that outputs data in either html or text output. > > Currently I'm writing chucnks like: > > if html: > print '<html><body bgcolor="FFFFCC">' > print '<table border="1" bgcolor="CCCCFF" width="800">' > print '<tr><td colspan="2"><h2>' > print 'Differences %s: %s' % (htypestr, lbl1) > if html: > ...
I'd create two Formatter classes, one for HTML and one for text. It looks like, in your case, the HTML one should inherit from the text one. Something like: py> class TextFormatter(object): ... def print_differences(self, htypestr, lbll): ... print 'Differences %s: %s' % (htypestr, lbll) ... py> class HTMLFormatter(TextFormatter): ... def print_differences(self, htypestr, lbll): ... print '<html><body bgcolor="FFFFCC">' ... print '<table border="1" bgcolor="CCCCFF" width="800">' ... print '<tr><td colspan="2"><h2>' ... super(HTMLFormatter, self).print_differences(htypestr, lbll) ... print '</h2></td></tr></table></body></html>' ... py> formatter = TextFormatter() py> formatter.print_differences('test', 'one') Differences test: one py> formatter = HTMLFormatter() py> formatter.print_differences('test', 'one') <html><body bgcolor="FFFFCC"> <table border="1" bgcolor="CCCCFF" width="800"> <tr><td colspan="2"><h2> Differences test: one </h2></td></tr></table></body></html> Using this strategy, you would replace all your print statements with calls to a formatter object. Which formatter you use would be determined wherever you currently set 'html' to True or False. HTH, STeVe -- http://mail.python.org/mailman/listinfo/python-list