[Tutor] Alternative File I/O for Tuples (fwd)

Don Parris webdev at matheteuo.org
Tue Jun 28 18:26:38 CEST 2005

On Tue, 28 Jun 2005 05:50:54 -0400
Kent Johnson <kent37 at tds.net> wrote:

> Don Parris wrote:
> > Just getting back to this - Mondays are always hectic.  This recipe is
> > the one I saw and like.  It looks cool!  In my brief efforts tinkering
> > with it, I am not really getting very far.  I saved the recipe, and
> > import it into the file containing all my database functions.  In one of
> > the functions, I have the following:
> > 
> > I used the "from tbl_Tabs import *" approach to import the recipe.  If I
> > should be doing this differently, just say so.
> > 
> >     for record in Results:
> >         print '%-15s\t%-15s\t%-15s' % record    
> > # I still want to print to screen, then...
> > 
> >         mbrPhone = open('mbrPhone.txt', 'w')
> >         mbrPhone.write(indent(record, hasHeader=False,
> >         separateRows=True)) mbrPhone.close()
> The problem is you are just passing one record to indent(). It processes
> the whole table at once so you have to pass the list of records, i.e.
>         mbrPhone.write(indent(Results, hasHeader=False,
>         separateRows=True))
> and do this outside the loop or you will write the file once for every
> record. So your code should look like this:
>     for record in Results:
>         print '%-15s\t%-15s\t%-15s' % record    
> # I still want to print to screen, then...
>     # Notice different indent from your code
>     mbrPhone = open('mbrPhone.txt', 'w')
>     mbrPhone.write(indent(Results, hasHeader=False, separateRows=True))
>     mbrPhone.close()

O.k., I'm curious about the indentation issue here.  There is only one file,
and it has the correct information - looking at it from a text editor.  Is
my version overwriting the file every time it iterates then?  You'll get a
laugh out of this, because when I was playing with sending the file to the
printer, I did not dedent the printer call, and it did try to print a copy
of the file for each record.  

Needless to say, I learned the command "lprm" Saturday! I caught onto the
indentation, which fixed that.  However, I did not pay attention to the code
above - probably because I don't have 50-something files named
mbrPhone*.txt.  Go ahead and laugh, I did!

> > 
> > I first assumed that "record", and then "Results" could be substituted
> > for"rows", per the recipe.  I also tried "rows", and assigning "record",
> > and then "Results" to "rows".  O.k., you can laugh all you want.  I
> > tried playing with the various arguments, and found that to be of little
> > value.
> > 
> > The traceback refers to a type error: iteration over a non-sequence. 
> > The query returns a tuple, as I understand it.  I also understand a
> > tuple to be a sequence.  If this is really as simple as the 3 lines you
> > pointed out above, I know I'm missing something in the implementation.
> Yes, a tuple is a sequence. If you still get an error with the code above,
> please post the exact error message and traceback (copy / paste from the
> output), there is a lot of useful information in it that can be lost if
> you paraphrase.
> Kent


Here we go.  Global name "rows" is not defined.  My guess is that "rows"
needs to know that it should be holding "Results", but maybe it should
already know that.  I tried to say rows = Results, but that did not help.
This seems to come back to the difficulty I have with passing arguments

### Revised Code ###
    print 'Phone List'
    for record in Results:
        print '%-15s\t%-15s\t%-15s' % record
    # rows = Results did not work
    mbrPhone = open('mbrPhone.txt', 'w')
    mbrPhone.write(indent(rows, hasHeader=False, separateRows=False,
                              prefix='| ', postfix=' |'))

### Traceback ###
Traceback (most recent call last):
  File "ekklesia.py", line 165, in ?
  File "ekklesia.py", line 160, in Main
  File "ekklesia.py", line 31, in RunMenu
    if len(MenuList[sel]) == 3: MenuList[sel][1](MenuList[sel][2])
  File "ekklesia.py", line 32, in RunMenu
    else: MenuList[sel][1]()
  File "/home/donp/python/ekklesia/ekklesia_db.py", line 61, in mbr_Phone
    mbrPhone.write(indent(rows, hasHeader=False, separateRows=False,\
NameError: global name 'rows' is not defined

BTW, I really appreciate your patience and willingness to help me understand
this.  If I had ever dreamed that I would have a desire to program 20 years
after the fact, I would have stopped passing notes in Math class.  I do it
well, but hate it.  Yet, I find myself drawn further and further into the
code, actually wanting to know more about it - why it does what it does.

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

More information about the Tutor mailing list