Need Help in Python

Daniel 'Dang' Griffith noemail at noemail4u.com
Fri May 7 09:53:03 EDT 2004


On Thu, 6 May 2004 07:57:50 -0700, Cousin Stanley
<CousinStanley at HotMail.Com> wrote:

>On Thu, 06 May 2004 12:12:37 GMT, Daniel 'Dang' Griffith wrote:
>
>> """ Starter module for drawing boxes with characters.
>>     No warranty or suitability for use is expressed or implied.
>>     Daniel 'Dang' Griffith sez, "Have a nice day!".
>> """
>
>Daniel .... 
>
>  Thanks for a nice generalization .... 
>
>  Preliminary tests indicate a problem 
>  with indent strings NOT being applied to the FIRST, top, line 
>  in the returned string ....
>
>  I'll try to find the problem ....
>  
That was intentional, as mentioned in the comments (perhaps 
poorly).  The caller is responsible for positioning the 
cursor at the top left corner, which means the first line 
should not be indented.  This allows things like this (excuse 
my poor ASCII art manual 
example:/---\
        |   |
        |   |
        |   |
        \---/

Nonetheless, it's Friday.  Maybe this should go to the Useless Python
site.  http://www.uselesspython.com/oldindex.html  (Did anyone check? 
Maybe there's something similar already there!)  Here's an enhanced
version that a) optionally prefixes the top line (the caller passes
True after the specified indentation string), and b) fixes a bug if
the fill value was longer than 1 character.
    --dang
""" Starter module for drawing boxes with characters.
    No warranty or suitability for use is expressed or implied.
    Daniel 'Dang' Griffith sez, "Have a nice day!".
"""

# for reference
boxchars = [chr(i) for i in xrange(129, 219)]

# handy abbreviations; roll your own naming convention
UL = upperleft = topleft                 = chr(218)
LL = lowerleft = botleft = bottomleft    = chr(192)
LR = lowerright = botright = bottomright = chr(217)
UR = upperright = topright               = chr(191)
vert = vertical                          = chr(179)
horiz = hor = horizontal                 = chr(196)

nl = newline = '\n'

# precompute the invariants
top = '%%s%c%%s%c' % (UL, UR)
mid = '%%s%c%%s%c\n' % (vert, vert)
bot = '%c%%s%c' % (LL, LR)
all = '%s\n%%s%%s%s' % (top, bot)

def box(w, h, fill=' ', indent='', top_also=False):
    """ Return a string that appears as an 'box' when printed.
        This is, of course, highly depending on the local
        character representation.
        The box is filled with spaces by default.
        The box is left-aligned by default.  To indent it, provide
        the indentation string.  This will be added to the middle 
        and bottom of the box.  
        By default, the caller is responsible for drawing the box 
        at the appropriate starting position.  To have to top
        line of the box indented, pass True as the last agrument.
    """
    # exceptional cases
    if w < 1:
        raise ValueError("A box must have a width of at least 1.")
    if h < 1:
        raise ValueError("A box must have a height of at least 1.")
    # pathological cases--return value is not *really* a box
    if w == 1:
        return h * (vert + nl)
    if h == 1:
        return horiz * w
    # normal case
    if not fill:
        fill = ' '
    fill = (fill * (w - 2))[:w-2]
    end = horiz * (w - 2)
    return all % (top_also and indent or '', end, (h - 2) * (mid %
(indent, fill)), indent, end)
#   --dang



More information about the Python-list mailing list