Re: Class data being zapped by method



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

.



Relevant Pages

  • Re: adding colums to text
    ... File is open for writing. ... Reading file into a linked list... ... Calculating sum and average. ... Temp file is open for writing. ...
    (comp.lang.c)
  • Re: show total in a list without the entries
    ... Engelbert schrieb: ... My "reply" here will warn people (reading down) ... Sum = 977 ... workplace: Cologne ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: show total in a list without the entries
    ... My "reply" here will warn people (reading down) ... information [and probably asking questions you've answered in your ... Sum = 977 ... workplace: Cologne ...
    (microsoft.public.sharepoint.windowsservices)
  • Re: max(), sum(), next()
    ... an exception also (I understand that behaviour is by design, ... You can't use a sum of 0 to indicate when data is missing, ... those cases where summing an empty list is meaningless. ... situations like empty lists rather than have it implicitly ...
    (comp.lang.python)
  • Re: While Bush and his posse yammer about the good economy
    ... Shouldn't there be a law invoked in that if you're too lazy to sum ... Umm, to be honest, I don't want to go through the trouble of reading ... "When citizens fear their government, ... "When citizens fear their government, you have tyranny; ...
    (rec.sport.pro-wrestling)