Another Sets Problem

Steve Holden steve at holdenweb.com
Tue Dec 29 08:33:27 EST 2009


Victor Subervi wrote:
> On Mon, Dec 28, 2009 at 4:23 PM, Steve Holden <steve at holdenweb.com
> <mailto:steve at holdenweb.com>> wrote:
> 
>     There is only one way for the piece of code you quote to print nothing
>     (unless you suspect a bug in the Python interpreter, but the probability
>     of that is so low compared with the probability of your making a mistake
>     in interpretation of the data that I am going to ignore it).
> 
>     Hence my deduction. If types[x][0:3] != 'set' then the else clause would
>     definitely print something. Hence types[x][0:3] == 'set', and the for
>     statement is executing. But again, if field were anything other than an
>     empty container it would produce printed output.
> 
>     But if it's empty, no printed output would be produced:
> 
>     >>> field = set([])
>     >>> for f in field:
>     ...   print "Steve is wrong"
>     ...
>     >>>
> 
>     Does this make sense?
> 
> 
> Boy, does it! It looks great...on paper. Here's some revised code to debug:
> 
I am unser how you expect anyone to "debug" an isolated piece of code
like this with an incomplete description of the data it's processing.

That's a bit like being told you have to produce something green, and
then when you do, being told "no, not that green, a light green". So you
produce something a lighter gree and then being told "no, a slightly
redder green". And so on.

In other words, you aren't giving us the whole picture here, and without
the whole picture you will only ever get bits of the answer.

> #              print 'XXX', types[x]
>               elif types[x][0:3] == 'set':
>                 for f in field:
>                   print '<td>AAA%s</td>\n' % (field)
>                 else:
>                   print 'YYY'
>  
> 1) If I uncomment the commented line, it throws this error:
> 
What happens if you *don't* uncomment the commented line?

> [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58]   File
> "/var/www/html/angrynates.com/cart/enterProducts2.py
> <http://angrynates.com/cart/enterProducts2.py>", line 138, referer:
> http://angrynates.com/cart/enterProducts.py
> [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58]     elif
> types[x][0:3] == 'set':, referer:
> http://angrynates.com/cart/enterProducts.py
> [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58]        ^,
> referer: http://angrynates.com/cart/enterProducts.py
> [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58] SyntaxError:
> invalid syntax, referer: http://angrynates.com/cart/enterProducts.py
> [Tue Dec 29 00:58:10 2009] [error] [client 208.84.198.58] Premature end
> of script headers: enterProducts2.py, referer:
> http://angrynates.com/cart/enterProducts.py
> 
I am presuming that this is copied from an Apache error log, though you
don't bother to say so.

The issue here is that you can't just stick print statements in anywhere
you like. Consider the following Python:

if a == 3:
    print "A is 3"
print "Debugging"
elif a == 4:
    print "A is 4"
else:
    print "A is neither 3 nor 4"

If you comment out the 'print "Debugging"' line you have a perfectly
acceptable program. If you don't then you have something that is close
to Python, but not close enough for the interpreter to be able to
compile it.

What's happening with your (CGI?) program is that the syntax error you
have induced by inserting a random print statement causes an error
message to be produced instead of your program's output; this doesn't
look like HTTP headers. So the web server complains that your program
has produced output that shouldn't be sent to any self-respecting browser.

I further presume (though again I don't believe you bother to report it
anywhere) that you see an "Error 500" or similar message in your web
browser when you try and access the web page.

> 2) AAA prints out before the following fields:
> AAASet(['Extra-small'])
> AAASet(['purple:50404D'])
> 
> 3) YYY *also* prints out twice!!
> 
> Here's the complete code once again:
> 
[complete code snipped]
> 
> TIA,
> beno
> 
Sorry, I don't intend to read all that code when the error you are
reporting is in fact a clear demonstration that you need to further
understand the principles of both Python and the web before you continue
on your journey.

You are trying to run before you can walk. Don't be surprised when the
result is an intimate contact between your face and the ground.

The first thing I would suggest is that you learn to write code that
uses multiple layers, calling functions to achieve higher-level ends. At
the moment your code is "monolithic" (a single huge chunk), which makes
it very difficult to separate out the bits that work from the bits that
don't.

This is not "a sets problem", this is a "lack of understanding problem".
Like any skill, programming is a rewarding talent once mastered. Like
most worthwhile achievements it demands a methodical approach and a
rigorous attention to detail if you are to master it properly.

The first thing you *must* do is instil in yourself a belief that *there
is always a logical explanation for what happens* in your programs. Then
you can go about debugging them in a methodical way.

regards
 Steve

-- 
Steve Holden           +1 571 484 6266   +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010  http://us.pycon.org/
Holden Web LLC                 http://www.holdenweb.com/
UPCOMING EVENTS:        http://holdenweb.eventbrite.com/



More information about the Python-list mailing list