Python equivalent to *nix 'banner' problem

Looney, James B james.b.looney at lmco.com
Tue Oct 23 19:29:53 EDT 2007


 <<banner.dat>> 
I have written a Python script to duplicate the 'banner' program from
IRIX.  Attached is the generated data file.  This data is generated the
first time by using 'banner' on my IRIX machine.  Thereafter, the data
is simply used (and can be run from anywhere).  The data is a marshaled
copy of the characters generated by 'banner'.

Regardless of whether I run this from IRIX or Windows, I get the same
results.  I'm expecting the string "ab" to be displayed twice.  Instead,
the second time, I get ABB.  If I add a 3rd print line, I'll get ABBB.
I'm now stuck trying to understand what's going on.  It seems like
there's an overflow somewhere since it's overwriting my dictionary for
the letter 'a', but I don't know how it's doing it.

Does anyone see the problem, or have suggestions on how to fix it?
Thanks,
-JB

<output>
H:\play\python\banner>Banner.py ab
----------------------------------------

   ##   #####
  #  #  #    #
 #    # #####
 ###### #    #
 #    # #    #
 #    # #####

----------------------------------------
----------------------------------------

   ##   #####  #####
  #  #  #    # #    #
 #    # #####  #####
 ###### #    # #    #
 #    # #    # #    #
 #    # #####  #####

----------------------------------------
</output>

<code>
#!/bin/env /usr/local/openSource/bin/python

import os, sys, marshal
from pprint import pprint

class Banner:

   ourDataFile = "banner.dat"
   ourDataPath = '.' + os.sep

   if( 0 <= __file__.rfind( os.sep ) ):
      ourDataPath = __file__.rsplit( os.sep, 1 )[ 0 ] + os.sep

 
########################################################################
########
   def __init__( self ):
      self.myBannerDict = {}
      if( False == os.path.exists( Banner.ourDataPath +
Banner.ourDataFile ) ):
         print "Having to create data file for the first time.  This
will take longer than normal."
         self.createBannerMarshal()

      f = open( Banner.ourDataPath + Banner.ourDataFile, 'rb' )
      self.myBannerDict = marshal.load( f )
      f.close()

 
########################################################################
########
   def getTotalLength( self, letterList ):
      totalLength = 0
      for line in letterList:
         totalLength += len( line.strip( os.linesep ) )

      return totalLength

 
########################################################################
########
   def createBannerMarshal( self ):
      # Create my list of all allowed ASCII characters.
      for i in range( 32, 127 ):
         c = chr( i )
         self.myBannerDict[ c ] = os.popen( "banner '%s'" % c
).readlines()

         # Catch any of the shell specific guys that the shell didn't
like.
         if( 0 == self.getTotalLength( self.myBannerDict[ c ] ) ):
            self.myBannerDict[ c ] = os.popen( 'banner "%s"' % c
).readlines()

         length = 0
         prependSpace = ""
         for j in range( len( self.myBannerDict[ c ] ) ):
            self.myBannerDict[ c ][ j ] = self.myBannerDict[ c ][ j
].strip( os.linesep )
            if( length < len( self.myBannerDict[ c ][ j ] ) ):
               length = len( self.myBannerDict[ c ][ j ] )
               if( 0 == self.myBannerDict[ c ][ j ].find( '#' ) ):
                  prependSpace = " "

         if( 0 == length ):
            length = 8

         for j in range( len( self.myBannerDict[ c ] ) ):
            self.myBannerDict[ c ][ j ] = "%s%-*s" % ( prependSpace,
length, self.myBannerDict[ c ][ j ] )

         f = open( Banner.ourDataPath + Banner.ourDataFile, 'wb' )
         marshal.dump( self.myBannerDict, f )
         f.close()

 
########################################################################
########
   def getBanner( self, inputStr ):
      retVal = []
      for c in inputStr:
         banneredCharList = self.myBannerDict[ c ]
         if( 0 == len( retVal ) ):
            retVal = banneredCharList
         else: 
            for i in range( len( banneredCharList ) ):
               retVal[ i ] += banneredCharList[ i ]

      return os.linesep.join( retVal )
      
if( "__main__" == __name__ ):
   banner = Banner()
   print 40 * '-' + os.linesep + banner.getBanner( sys.argv[ 1 ] ) +
os.linesep + 40 * '-'
   print 40 * '-' + os.linesep + banner.getBanner( sys.argv[ 1 ] ) +
os.linesep + 40 * '-'
   #print 40 * '-' + os.linesep + banner.getBanner( sys.argv[ 1 ] ) +
os.linesep + 40 * '-'
</code>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20071023/0999651a/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: banner.dat
Type: application/octet-stream
Size: 10231 bytes
Desc: banner.dat
URL: <http://mail.python.org/pipermail/python-list/attachments/20071023/0999651a/attachment.obj>


More information about the Python-list mailing list