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

Kent Johnson kent37 at tds.net
Wed Jun 29 18:37:45 CEST 2005

Don Parris wrote:
> On Wed, 29 Jun 2005 06:38:36 -0400
> Kent Johnson <kent37 at tds.net> wrote:
>>Don Parris wrote:
> However, it seems that the function doesn't like the
> value I give it.
> ### Using Results as the argument to indent() ###
> Traceback (most recent call last):
>   File "ekklesia.py", line 165, in ?
>     Main()
>   File "ekklesia.py", line 160, in Main
>     RunMenu(Menu_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 63, in mbr_Phone
>     prefix='| ', postfix=' |'))
>   File "/home/donp/python/ekklesia/tbl_Tabs.py", line 24, in indent
>     logicalRows = [rowWrapper(row) for row in rows]
>   File "/home/donp/python/ekklesia/tbl_Tabs.py", line 21, in rowWrapper
>     newRows = [wrapfunc(item).split('\n') for item in row]
> AttributeError: 'NoneType' object has no attribute 'split'
> ### end traceback ###
> My SQL query functions return a tuple, so indent() has to recognize that
> much.  

This is a tough one to interpret. The value being returned from wrapfunc() is expected to be a string, but instead it is None. Calling None.split() generates the AttributeError.

Looking at wrapfunc, in defaults to an identity function that just returns its argument. So it looks like the tuples you get back from MySQL have None values in them which is confusing indent().

Try with 
  wrapfunc=lambda x: x or ''
which will convert None to an empty string, or
  wrapfunc=lambda x: str(x)
which will convert None to the string 'None'

> I suspected this has to do with the wrapfunc argument.  I had dropped
> it at some point, thinking that would help me grasp the problem.  I fed it
> rows(using the rows= Results at the moment), and got this traceback:
> ### revised function call and traceback ###
> mbrPhone.write(indent(rows, hasHeader=False, separateRows=False,
>                                prefix='| ', postfix=' |'))wrapfunc=lambda
>                                x:wrap_onspace(rows, 12))

Take another look at the line above, the commas and parens aren't right.

> I've read the Python tutorial, Alan's tutorial, and have worked through some
> of the others as well.  It looks simple enough, but when I try things out
> for myself, I find it difficult to see how the examples apply in my
> situation.  Which is why I sought out this list.  My Guess is that I need to
> "just do it" for a while before it'll come to me.

"just doing it" is critical. Learning to program is like learning to write or learning a foreign language. It just can't be done by reading about it; the only way to learn is to do it. One thing to try is to just type in examples from a tutorial. Then think of variations on the example; try them out. Play with it, whatever crazy idea you have is good. This will give you experience with the basics and also some experience with error messages!

When you become more comfortable with the basics you should start to see how they fit together to make useful programs. Just in the snippets we are working on we have touched on looping, file I/O and function calls which should be topics in any tutorial.


More information about the Tutor mailing list