How to print a sorted list as a multi-column table

Sverker Nilsson sn at sncs.se
Fri May 23 11:30:36 EDT 2008


Hi all,

I would like to ask about opinions about the best way to format sorted
tables of items for interactive use. I have begun to add interactive
help to Guppy/Heapy (http://guppy-pe.sourceforge.net) because it lacks
the usual ways for introspection (see for example
http://www.pkgcore.org/trac/pkgcore/doc/dev-notes/heapy.rst) which is
due to Heapy's use of dynamic things like __getattr__ and to some
extent properties.

There seems to be no (good or otherwise) way to hook into the standard
help() (?) (At least not before 2.6 where I recall seeing a __dir__
magic method may be introduced, but I want it to be backwards
compatible to at least 2.4, preferably to 2.3.)

So I am about to roll my own help system. It will be based on having
the API objects supplying a standardized '.help' attribute which can
be used interactively, and will limit the output it prints to say 15
or 20 rows at a time.

That's for a background. Now what I was stumbling on which pumped up
enough adrenalin to make me begin to write this ---

Why are tables formatted like the following, when sorted? (Both in
linux eg ls, ftp help, and in Python help() when listing (eg)
modules))

(1)

a  g  m  s
b  h  n  t
c  i  o  u
d  j  p  v
e  k  q
f  l  r

Wouldn't it be more natural to just sort them like this:

(2)

a  b  c  d
e  f  g  h
i  j  k  l
m  n  o  p
q  r  s  t
u  v

What's the rationale for the choice of (1)?

In a pager, if you want to limit the number of lines output at a time,
then yo'd see with (1) (if artifically limiting output to 2 lines):

a  g  m  s
b  h  n  t

So to see the f item you would have to scroll down all the way.  The
number of times you would have to scroll down is in completely
unrelated to the item's position in sort order. That seems to defeat
the purpose of sorting in the first place. It feels strange, to me at
least. Anybody had the same feeling?

Well, what's the rationale (if any) for using the layout (1)?
Wouldn't layout (2) be better? And/or would it be confusing /
non-pythonic / non-unlixonic if a program used the (2) layout instead?

Have a (:nice:) weekend :-)

Sverker



More information about the Python-list mailing list