Functional Programming: WAS: Too Self Centered

beno zope at thewebsons.com
Wed Jan 8 09:12:58 EST 2003


At 09:06 AM 1/8/2003 -0500, you wrote:
> > >Your program will blow up reading a large file because of your
> > >recursive use of "subsequentRead(self)" and
>"subsequentLines(self)".
> > >The form of recursion you are using is called "tail-recursion".
> > >Python does not optimize tail-recursion.  Outside of the LISP
>world,
> > >tail-recursion is not used (and rarely optimized).  A "while" loop
> > >would be better.
> >
> > Hmmm. I'm really sold on the philosophical merits of *Functional
> > Programming*.  But I appreciate any feedback you have concerning
> > optimization here. I need to stick with Python because I'm addicted
>to Zope ;)
>
>Actually, I believe one of your recursions could be replaced with
>.readlines() but I was not exactly sure of the external specification
>of your class.

I don't understand what you mean by *external specification of my class*. 
But here's the entire script if you'd care to comment.
Thanks!
beno

"""
Testimonials.py
This script turns testimonials in individual files into drop-down select 
style boxes
that can be inserted into HTML pages. Said files must be located in the
*testimonials* folder (within a language folder if using Easy Cart) in your 
Web site
folder. The first line of each such file must contain *only* the name of 
the person
giving the testimony.
"""

import string

class Testimonials:


         count = 0
         countOne = 0
         total = 0
         lines = []
         name = ''
         readFile = []
         allLines = ''
         shortLines = []
         shortTemp = ''
         shortTemp1 = ''
         shortTemp2 = ''
         blank = 0
         __file = []
         clickForMore = 'no'

         def __init__(self):
                 self.readFile = open('test1','r')
                 while self.readFile.readline() != '':
                         self.count = self.count + 1
                 self.total = self.count
                 self.readFile.close()

         def firstRead(self):
                 self.readFile = open('test1','r')
                 self.name = self.readFile.readline()
                 self.count = self.count - 1

         def subsequentRead(self):
                 if self.count != 0:
                         self.lines.append(self.readFile.readline())
                         self.count = self.count - 1
                         self.subsequentRead()
                 else:
                         pass

         def firstLine(self):
                 self.allLines += self.lines[0]

         def subsequentLines(self):
                 if self.total > 2:
                         self.total = self.total - 1
                         self.countOne = self.countOne + 1
                         self.allLines += self.lines[self.countOne]
                         self.subsequentLines()
                 else:
                         table = string.maketrans('\n', ' ' )
                         self.allLines = string.translate(self.allLines,table)

         def firstShorten(self):
                 self.shortTemp = self.allLines[:30]
                 blank = string.rfind(self.shortTemp,' ')
                 self.shortTemp1 = self.shortTemp[:blank]
                 self.shortTemp2 = self.shortTemp[blank + 1:]
                 self.allLines = self.shortTemp2 + self.allLines[30:]
                 table = string.maketrans('\n', ' ' )
                 self.name = string.translate(self.name,table)
                 if self.clickForMore == 'no':
                         self.theTestimonial = '<select>\n <option 
value="pass">See what ' \
                                                 + self.name + 'has to 
say!</option>\n <option ' + \
                                                 'value="pass">&#xa0;</option>\n'
                 else:
                         self.theTestimonial = '<select name=testimonial 
onChange="' + \
                                                 'testimonials(this.form)">\n 
<option value="pass">' + 'See what ' \
                                                 + self.name + 'has to 
say!</option>\n <option value="pass">' + \
                                                 '&#xa0;</option>\n'
                 self.theTestimonial += ' <option value="pass">' + 
self.shortTemp1 \
                                                 + '</option>\n'

         def subsequentShorten(self):
                 if self.allLines != '':
                         self.shortTemp = self.allLines[:30]
                         blank = string.rfind(self.shortTemp,' ')
                         self.shortTemp1 = self.shortTemp[:blank]
                         self.shortTemp2 = self.shortTemp[blank + 1:]
                         self.allLines = self.shortTemp2 + self.allLines[30:]
                         self.theTestimonial += ' <option value="pass">' + 
self.shortTemp1 + '</option>\n'
                         self.subsequentShorten()
                 else:
                         pass

         def endTag(self):
                 if self.clickForMore == 'no':
                         self.theTestimonial += ' <option 
value="pass">&#xa0;</option>\n' + \
                                                 ' <option value="pass">' + 
self.name + '</option>\n</select>'
                 else:
                         self.theTestimonial += ' <option 
value="pass">&#xa0;</option>\n' + \
                                                 ' <option value="pass">' + 
self.name + '</option>\n' + \
                                                 ' <option 
value="pass">&#xa0;</option>\n'
                         self.theTestimonial += ' <option 
value="pass">CLICK FOR MORE' + \
                                                 ' 
TESTIMONIALS!</option>\n</select>'

         def printit(self):
                         print self.theTestimonial


def test():
         f = Testimonials()
         f.firstRead()
         f.subsequentRead()
         f.firstLine()
         f.subsequentLines()
         f.firstShorten()
         f.subsequentShorten()
         f.endTag()
         f.printit()








More information about the Python-list mailing list