Re: [Tutor] 2d list index inverting?

2010-05-26 Thread Dave Angel

Hugo Arts wrote:

On Wed, May 26, 2010 at 3:13 AM, Alex Hall  wrote:
  

Hello all,
I have a 2d list being used for a battleship game. I have structured
the program so that it uses a grid class, which implements this array
along with a bunch of other methods and vars. For example, to get at
the top left square, you would say:
Grid.getSquareAt(0,0)
and inside getSquareAt is simply:
 def getSquareAt(self, x, y):
 return self.b[x][y] #b is the internal 2d list for the class

However, I am getting very confused with indexing. I keep getting
errors about list index out of range and I am not sure why. I have a
feeling that using 2d lists is supposed to go like a matrix
(row,column) and not like a coordinate plane (column, row).



A 2D list doesn't really exist. What you're using is just a list whose
elements are also lists. A nested data structure. And whether those
sub-lists should be the rows or the columns? It doesn't matter. A list
is just a list. Sequential data elements. It doesn't care whether it
represents a row or a column. What are 'row' and 'column' anyway? just
words designating some arbitrary notion. Conventions. You can swap one
for the other, and the data remains accessible. As long as you're
consistent, there's no problem.

The real problem is something else entirely. Somewhere in your code,
you are using an index that is greater than the size of the list.
Perhaps you're not consistent, somewhere. Mixing up your row/column
order. Perhaps something else is amiss. No way to tell from the
snippet.

Hugo

  
My question would be how are you creating these lists, and how you're 
updating them.  If you're doing a 20x20 board, are you actually creating 
20 lists, each of size 20, in instance attribute b ?  Do you do that in 
the __init__() constructor?  Or are you doing some form of "sparse 
array" where you only initialize the items that have ships in them?


As for updating, are you always doing the update by assigning to 
self.b[row][col]  ?


You could always add a try/catch to the spot that crashes, and in the 
catch clause, temporarily print out the subscripts you're actually 
seeing.  As Hugo says, you could simply have values out of range.



DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2d list index inverting?

2010-05-26 Thread spir ☣
On Tue, 25 May 2010 21:47:19 -0400
Alex Hall  wrote:

> I thought so, but I was hoping you would not say that as this means a
> logic bug deep in my code, and those are the hardest to track down...

No, they're not. On the contrary. Logical bugs are easily diagnosed by printing 
out relevant values at the right place in your code. Just do it and in 5 mn 
your code works as expected.

Denis


vit esse estrany ☣

spir.wikidot.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 2d list index inverting?

2010-05-26 Thread Hugo Arts
2010/5/26 spir ☣ :
>
> No, they're not. On the contrary. Logical bugs are easily diagnosed by 
> printing out relevant values at the right place in your code. Just do it and 
> in 5 mn your code works as expected.
>

And wormholes are easily stabilized by lining their interior with
exotic matter ;-)

Seriously though, deciding what the relevant values and the right
places are is not always as trivial as that sentence implies. Out of
the three types of errors generally recognized (compiler errors,
run-time errors, logic errors), the logic error is the hardest to
debug, precisely because it is difficult to locate.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Successfully trapping the [505 file not found] error

2010-05-26 Thread Tim Johnson
Using Python 2.6.2 on Slackware 13.0 32-bit.
I'm using Python as a command-line application to do an FTP download
which processes a file of jpg filenames, downloading each. Not all
of the file names can be found.

I'm having a problem trapping *all* of the file not found errors.
As an example my application (which is heavily logged) will
successfully handle the 'not found' exeception multiples of time,
continuing with the download process, and then inexplicably (to me)
fails.

What follows is the exception handling snippet:
## --
except ftplib.error_perm, e:
msg = "Couldn't get %s  %s" % (fname,str(sys.exc_info()[1]))
log.add(msg)
more-code-follows
## --
When the code above is successful, the application simply
logs the exception and continues.
When the code above fails the traceback is as follows:

## --
Traceback (most recent call last): 
<... traceback stack of program locations removed ...>
error_perm: 550 file not found.
ERROR MESSAGE: 550 file not found.
ERROR TYPE: 
## --

Is there something that can improve my error handling process here
so that my application safely and consistantly continues downloading
so that the entire download process is completed?

-- 
Tim 
tim at johnsons-web.com or akwebsoft.com
http://www.akwebsoft.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Successfully trapping the [505 file not found] error

2010-05-26 Thread Luke Paireepinart
>
> What follows is the exception handling snippet:
> ## --
> except ftplib.error_perm, e:
>    msg = "Couldn't get %s  %s" % (fname,str(sys.exc_info()[1]))
>        log.add(msg)
>        more-code-follows
> ## --
> When the code above is successful, the application simply
> logs the exception and continues.
> When the code above fails the traceback is as follows:
>

Are you sure you aren't doing anything with the ftp object in the
"more code follows"?
You are probably creating another error of the same type while
processing your exception, so the second one doesn't get caught.

like this (except with ftp obviously):
try:
f = open('in.txt')
except IOError:
y = open('log.txt', 'w')
y.write('failed!')
y.close()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Successfully trapping the [505 file not found] error

2010-05-26 Thread Tim Johnson
* Luke Paireepinart  [100526 15:37]:
> 
> Are you sure you aren't doing anything with the ftp object in the
> "more code follows"?
> You are probably creating another error of the same type while
> processing your exception, so the second one doesn't get caught.

  :) Ain't it nice to have a second set of eyes! You are correct.
 I'm attempting to reconnect without the exception handling.

  Thanks
-- 
Tim 
tim at johnsons-web.com or akwebsoft.com
http://www.akwebsoft.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor