[Tutor] attribute error - quick addition

Sara Johnson sarliz73 at yahoo.com
Sun Jul 29 22:49:58 CEST 2007

Sorry...I forgot a few more lines at the end of the code.  Starts with "outfile2write..."
I also added outfile2.sort()    

Is this the correct traceback?  Output okay except it will not sort alphabetically.
Traceback (most recent call last):
  File "./mymods.py", line 83, in ?
AttributeError: 'file' object has no attribute 'sort'
Here's the code.  I did not write most of this, I'm only modifying it.  In fact, I'm only responsible for the last two lines.
#reverse the dictionary of dictionaries, making new dictonary z:
z={} #initialize empty dictionary
skeys=h[keys[0]].keys() #get the "sub-keys"
for i in skeys: #use sub-keys as new main-keys
        for j in keys: #use main-keys as new sub-keys
#use the new dictionary to find number of missing values:
print "\n fraction of sites with missing measurements:"
for key in skeys:
        outstring="%s has %4.1f%% missing" % (key,100*fracmiss)
        if fracmiss>0.:
                print outstring

        outfile2.write(outstring+'\n') #notice explicit newline \n

----- Original Message ----
From: Alan Gauld <alan.gauld at btinternet.com>
To: tutor at python.org
Sent: Sunday, July 29, 2007 2:23:55 AM
Subject: Re: [Tutor] attribute error

"Sara Johnson" <sarliz73 at yahoo.com> wrote

> However, is there an indentation error here?

Not that I can see. Is there an error? If so can you send the 
Its very hard to answer questions without any context.

> I may just be too frustrated to see it.
> for key in skeys:
>         fracmiss=1.*numberMissing(z[key].values())/nsites #note 
> decimal multiplication, 1.*

This is a mess. Why have that long comment when all that's needed
is to add the zero?! (and some spaces to make it legible...)

        fracmiss = 1.0 * numberMissing( z[key].values() ) / nsites
        outstring = "%s has %4.1f%% missing" % (key, 100 * fracmiss)
        if fracmiss > 0.0:
                print outstring

It looks OK apart from the poor style but I don't see anything that is
an error. What are you seeing?

On the style front, if you want to force a result to a float it's more
conventional to use the float() conversion function:

        fracmiss = float( numberMissing( z[key].values() ) / nsites )

And comparing to 0.0 doesn't really add anything except two 
it could just as well be plain old 0.

        if fracmiss > 0:


Alan G.

Tutor maillist  -  Tutor at python.org

Pinpoint customers who are looking for what you sell.

Yahoo! oneSearch: Finally, mobile search 
that gives answers, not web links. 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070729/b4625fa3/attachment.htm 

More information about the Tutor mailing list