Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: Structured writing to console, such as a table

Reply
Thread Tools

Re: Structured writing to console, such as a table

 
 
Bob Gailer
Guest
Posts: n/a
 
      09-02-2003
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
http://www.velocityreviews.com/forums/(E-Mail Removed)
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

 
Reply With Quote
 
 
 
 
Paul Moore
Guest
Posts: n/a
 
      09-02-2003
Bob Gailer <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
When writing html table to div, the data from table is unformatted sil Javascript 4 02-07-2010 03:46 PM
Encrypt/Decrypt a structured file kevininstructor@state.or.us ASP .Net 1 09-25-2004 05:25 AM
Using xml source in datagrid -- possible with non-structured data? KatB ASP .Net 3 10-29-2003 07:09 AM
Structured writing to console, such as a table Raaijmakers, Vincent (IndSys, GE Interlogix) Python 3 09-02-2003 09:42 AM



Advertisments