Functional Programming: WAS: Too Self Centered

beno zope at thewebsons.com
Thu Jan 9 01:20:52 EST 2003


At 04:17 PM 1/8/2003 -0800, you wrote:
>beno fed this fish to the penguins on Wednesday 08 January 2003 06:12
>am:
>
>
> >
> > """
> > 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'
> >
>
>         ALL of these are class-wide variables, they are not specific to any
>instance of the class. I suspect they should be the first statements in
>the __init__() function (and at that point they should have self. on
>them to initialize only the instance version)
>
>         Class-wide variables are useful if you have to share data between
>multiple instances -- and in that case the functions would use
>Testimonials.shared (for example) to access the class-wide data.
>
>
> >          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)
>
><blink><blink>
>
>         Is all that supposed to limit each display line to 30 characters?
>
>import string
>
>def format30(wrdlst):
>         ln = ""
>         while (wrdlst <> []) and ((len(ln) + len(wrdlst[0])) < 30):
>                 ln = ln + wrdlst[0] + " "
>                 del wrdlst[0]
>         return (ln, wrdlst)

Sweet. Thanks!


>#the following is all one line in the editor
>mystr = "This is a long list of nonsense for the purpose of testing out
>the format30 function"
>
>mywords = string.split(mystr)   #generate working word list
>while len(mywords) > 0:
>         (aline, mywords) = format30(mywords)
>         print aline
>
>
>[wulfraed at beastie wulfraed]$ python t.py
>This is a long list of
>nonsense for the purpose of
>testing out the format30
>function
>
>         Note: I did not code the case of one word being >30...
>
> >          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 += '
>
>         And here you repeat the confusing thing <G>
>
>         Even in that variant I can see changes:
>
>                 blank = string.rfind(self.allLines[:30], " ")
>                 (self.short, self.allLines) = (self.allLines[:blank],
>                                                 self.allLines[blank+1:] )

Nice.


> > def test():
> >          f = Testimonials()
> >          f.firstRead()
> >          f.subsequentRead()
> >          f.firstLine()
> >          f.subsequentLines()
> >          f.firstShorten()
> >          f.subsequentShorten()
> >          f.endTag()
> >          f.printit()
> >
>         I'd have gotten kicked out of my job if I coded processing like that
><G>  read the first line, read all the rest of the lines, do something
>to the first input line, do something to the rest of the input lines,
>format the first output line, format the rest of the output lines.
>
>         I'd likely have ended up with
>
>         f = Testimonials()
>         f.load(filename)        #where load reads all, strips \n, makes 
> one string
>         f.format()              #which bulds a list of length limited strings
>                                 #along with any surrounding tags
>         f.print()       #or this function puts the surrounding tags

Thank you VERY MUCH for your help!
beno







More information about the Python-list mailing list