[prog] Adding Column Headings in Text File

Don Parris webdev at matheteuo.org
Sat Jul 2 13:38:39 EST 2005


On Sat, 2 Jul 2005 00:09:28 +0200
Almut Behrens <almut-behrens at gmx.net> wrote:

> On Fri, Jul 01, 2005 at 02:10:43AM -0400, Don Parris wrote:

<SNIP>

> > The function appears to be treating Results as a list, when it is
> > supposed to be a tuple (or so I thought).
> 
> Not quite... :)  It's kind of the other way round:  Results would need
> to be a list to be concat'able with [heading], which has become a list
> by putting the square brackets around it...
> Contatenation only works with equal types, so you could either write
> 
>       mbrPhone.write(indent( [heading] + list(Results),  ...
> or
>       mbrPhone.write(indent( (heading,) + Results,  ...
> 
> The former is concatenating two lists-of-tuples ('[heading]' is a list
> with one tuple), while the latter is concatenating two tuples-of-tuples
> (you need that weird '(heading,)' to make a tuple-of-tuples from one
> tuple...).  
> 

O.k., that got it!  I had tried [heading] and heading, but not (heading,). 
I hadn't thought through it like that.  Further, I was attempting
to convert heading to a list because I thought indent() somehow thought
Results was a list.  Results was really the tuple that could not be
concatenated to the list [heading].  

But why didn't indent() recognize heading (no brackets) as the tuple it was?
 It saw Results, but not heading, as a tuple.  Results was not stated as
(Results), but a simply Results. In my newbie way of thinking, indent()
should have known that heading - not [heading] - was also a tuple.

> Just to elaborate a little:
> 
> Starting with the following definitions
> 
>   heading = 'First Name', 'Last Name', 'Home Phone'
>   row1 = 'Row1 Col1', 'Row1 Col2', 'Row1 Col3'
>   row2 = 'Row2 Col1', 'Row2 Col2', 'Row2 Col3'
>   row3 = 'Row3 Col1', 'Row3 Col2', 'Row3 Col3'
> 
> the first parameter to the function indent() (which needs to be
> sequence-of-sequences) could be one of the following:
> (let's simply call that parameter 'data' here)
> 
>   data = heading, row1, row2, row3    # tuple-of-tuples
> 
> Or, with Results being
>   Results = row1, row2, row3          # tuple-of-tuples
>                                       # (that's what you have)
> you could concatenate
>   data = [heading] + list(Results)    # --> list-of-tuples
>   data = (heading,) + Results         # --> tuple-of-tuples
> 
> Or, with Results being
>   Results = [row1, row2, row3]        # list-of-tuples
> 
> you could concatenate
>   data = [heading] + Results          # --> list-of-tuples
>   data = (heading,) + tuple(Results)  # --> tuple-of-tuples
> 
> 
> Both "list-of-tuples" and "tuple-of-tuples" qualify as
> "sequence-of-sequences" -- with sequence being the more general term
> for both list (mutable) and tuple (immutable).
> 
> (Thus, theoretically, "list-of-lists" and "tuple-of-lists" would also
> qualify (among others), but as you already have tuples for the inner
> sequences, we've just omitted all those combinations, for brevity... ;)
> 
> Does that make sense?
> 
> Almut
> 

It's a bit of a tongue-twister, but yes, it makes sense. :) In my op, I
included the example from the recipe.  I thought the rows data was in the
form of a tuple, but I suppose it's actually a list, since they called
[labels], rather than (labels).  Anyway, that was what was throwing me off
base.

Now that the function works as it should, I can allow my users to get a
report, and either print it, or e-mail it off to some appropriate person. 
The recipe involved is an excellent example from the Python Cookbook.  I'll
eventually get a clue, but I am enjoying the learning experience.  I'm also
beginning to feel more comfortable with the terminology and actually writing
the code.  Despite the challenge this represents for me, I'm actually kind
of enjoying writing code.  I never would have dreamed it.


Don
-- 
evangelinux    GNU Evangelist
http://matheteuo.org/                   http://chaddb.sourceforge.net/
"Free software is like God's love - you can share it with anyone anytime
anywhere."


More information about the Programming mailing list