Re: Structured writing to console, such as a table

Discussion in 'Python' started by Bob Gailer, Sep 2, 2003.

  1. Bob Gailer

    Bob Gailer Guest

    At 02:37 PM 9/1/2003 -0500, Raaijmakers, Vincent (IndSys, GE Interlogix) wrote:

    >Ok, perhaps a question on a newbie level.
    >
    >I try to create a simple 'write to a console application' where all the
    >items in a string
    >do have a variable size:
    >items = ["a", "bbbbbbbbb", "cc"]
    >Well, actually, I need to print a table as simple text, nice lined up in a
    >console.
    >So:
    >
    >
    >Item: Value: Another Value:
    >----------+----------+------------------
    >a | 1 | 2
    >bbbbbbbbb | 2 | 17
    >cc | 3 | 5
    >
    >My hope was that somewhere in python land an existing module was waiting
    >for me.
    >A module that also prints lines, headers....
    >
    >Unfortunately, I can't find it. Books, Google...
    >Before I reinvent this wheel... please give me some tips, references,
    >examples...


    In addition to other responses, consider this which uses nested %
    formatting to make format strings for reuse:

    class Table:
    seps = '- +|'
    def __init__(self, *columnWidths):
    if columnWidths:
    self.setWidths(columnWidths)
    def setWidths(self, columnWidths):
    self.columnWidths = columnWidths
    format1 = ("%%%%-%ss%%s"*len(columnWidths))[:-3]
    format2 = format1 % columnWidths
    self.nSeps = len(columnWidths) - 1
    self.hdrFormat = format2 % ((self.seps[1], )*self.nSeps)
    self.sepFormat = format2 % ((self.seps[2], )*self.nSeps)
    self.rowFormat = format2 % ((self.seps[3], )*self.nSeps)
    def printHdr(self, *heading):
    print self.hdrFormat % heading
    def printSep(self):
    print self.sepFormat % tuple([self.seps[0]*w for w in self.columnWidths])
    def printRow(self, *items):
    print self.rowFormat % items
    print

    tbl = Table(12, 12, 16)
    tbl.printHdr('Item:', 'Value:', 'Another Value:')
    tbl.printSep()
    tbl.printRow('a', 1, 2)
    tbl.printRow('bbbbbbb', 2, 17)
    tbl.printRow('cc', 3, 5)


    Bob Gailer

    303 442 2625


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003
     
    Bob Gailer, Sep 2, 2003
    #1
    1. Advertising

  2. Bob Gailer

    Paul Moore Guest

    Bob Gailer <> wrote in message news:<>...

    > In addition to other responses, consider this which uses nested %
    > formatting to make format strings for reuse:


    I like this one! Very simple, and the resulting code is easy to
    understand.

    Most of my code's complexity revolved around getting the column
    widths, which you don't address - this is good, as it makes the
    separation of issues clearer. One other issue which sometimes bites
    me, when I need to print database tables, is whether to truncate
    values which are too long for their field widths. It's another issue
    the OP didn't specify a preference over. No criticism there, it's just
    an illustration that for problems like this, knowing what you want is
    often the bigger issue than writing code to do it... (A friend of mine
    used to love quoting the example of functions to "ask for a yes/no
    response", where it's nearly always easier to write your own version
    than to work around the ways in which a canned version doesn't *quite*
    meet your needs...)

    In Python, reinventing wheels is often easy enough that it's not worth
    the design effort of making things like this into truly reusable
    library code. I can't work out if that's an advantage of Python :)

    Paul.
     
    Paul Moore, Sep 2, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raaijmakers, Vincent (IndSys, GE Interlogix)

    Structured writing to console, such as a table

    Raaijmakers, Vincent (IndSys, GE Interlogix), Sep 1, 2003, in forum: Python
    Replies:
    3
    Views:
    395
    Peter Abel
    Sep 2, 2003
  2. Miktor
    Replies:
    7
    Views:
    12,057
    Joe Bacigalupa
    Aug 9, 2005
  3. Replies:
    5
    Views:
    3,433
    James Kanze
    Mar 4, 2008
  4. Jabba Laci
    Replies:
    0
    Views:
    80
    Jabba Laci
    Apr 10, 2013
  5. Arnaud Delobelle

    Re: extract HTML table in a structured format

    Arnaud Delobelle, Apr 10, 2013, in forum: Python
    Replies:
    0
    Views:
    116
    Arnaud Delobelle
    Apr 10, 2013
Loading...

Share This Page