Class data being zapped by method

Simon Forman rogue_pedro at yahoo.com
Tue Aug 8 20:05:10 EDT 2006


Kevin M wrote:
> Figures. I'll try to complicate it sufficiently ;)
>
>
> [edit] I was going to try to sum up what goes on, but I realized that I
> was retyping what I already programmed in an effort to better
> illustrate exactly what I'm doing. Pastebin it is. Don't fear, it's
> only around 200 lines total.
>
> Class file -- http://pastebin.4programmers.net/640
> Main file -- http://pastebin.4programmers.net/639
>
> PLEASE don't worry about any file parsing stuff. That's all working
> well. The problem is that I'm getting IndexError exceptions in my
> _a_count and _b_count methods when they're called from analyze(). I
> added some code to increase verbosity, and it turns that the count
> methods within the Test class are operating on EMPTY lists ( [] ), so
> naturally they can't index anything. This is the core of the problem,
> especially considering that line 98 in main.py works as expected.
>
> Once again, thank you VERY VERY much for addressing this situation,
> everyone
>

Not that this has anything to do with your actual question, but there
are a number of small details that I noticed while reading your code:

1.) Why are you removing the .pyc file?

2.) Reading lines from a file is better done like so:

arrLines = open('datafiles/'+filename+'.tabdata').readlines()

and the 'r' flag is the default, you can omit it.

3.) You can create the Test instances like so:

arrTests = [Test() for i in range(cntTests)]

4.) You don't need "saveout = sys.stdout", sys.__stdout__ is already
this.

5.) In "check = 0.6 * float(depth)" the call to float is redundant and
can be eliminated.

6.) In "sumx = sum([x[0] for x in self.data])", etc.. you can leave out
the []'s.  There's no need to create a list, the ()'s in the call to
sum() suffice to make a generator comprehension.


FWIW, if you're still having trouble later I'll try to take another
look at your code.  Print statements and debuggers are your friends,
and John Machin's advice seems good to me.

Peace,
~Simon




More information about the Python-list mailing list