Thank you, Wayne gave me a suggestion which helped me relook at things and
correct the problem I presented. This has added other food for thought for
me such as not allowing the program to accept negative inputs for the score
however, I will have a play to see if I can fix this first and then ask
again if I cannot.

Thanks again.

Lea

-----Original Message-----
From: tutor-bounces+lea-parker=bigpond....@python.org
[mailto:tutor-bounces+lea-parker=bigpond....@python.org] On Behalf Of
tutor-requ...@python.org
Sent: Saturday, 14 May 2011 12:58 PM
To: tutor@python.org
Subject: Tutor Digest, Vol 87, Issue 44

Send Tutor mailing list submissions to
        tutor@python.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.python.org/mailman/listinfo/tutor
or, via email, send a message with subject or body 'help' to
        tutor-requ...@python.org

You can reach the person managing the list at
        tutor-ow...@python.org

When replying, please edit your Subject line so it is more specific than
"Re: Contents of Tutor digest..."


Today's Topics:

   1. Re: short url processor (ian douglas)
   2. Help with exceptions please (Lea Parker)
   3. Re: short url processor (ian douglas)
   4. Re: Overriding a method in a class (Terry Carroll)


----------------------------------------------------------------------

Message: 1
Date: Fri, 13 May 2011 17:42:31 -0700
From: ian douglas <ian.doug...@iandouglas.com>
To: Alan Gauld <alan.ga...@btinternet.com>
Cc: tutor@python.org
Subject: Re: [Tutor] short url processor
Message-ID: <4dcdcff7.5010...@iandouglas.com>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"

On 05/13/2011 05:03 PM, Alan Gauld wrote:
> How do you know they are going to stdout? Are you sure they aren't 
> going to stderr and stderrr is not mapped to stdout (usually the 
> default). Have you tried redirecting stderr to a file for example?
>
> As I say, just some thoughts,
>

Thanks for your thoughts, Alan. I had done some testing with cmdline
redirects and forget which is was, I think my debug log was going to stdout
and the apache-style log was going to stderr, or the other way around...

After a handful of guys in the #python IRC channel very nearly convinced me
that all but 3 stdlib libraries are utter worthless crap, and telling me to
download and use third-party frameworks just to fix a simple logging issue,
I overrode log_request() and log message() as such:

class clientThread(BaseHTTPRequestHandler): #[[[

         def log_request(code, size):
                 return

         def log_message(self, format, *args):
                 open(LOGFILE, "a").write("%s\n" % (format%args))


... and now the only logging going on is my own, and it's logged to my
external file. Overriding log_request means that BaseHTTPServer no longer
outputs its apache-style log, and overriding log_message means my other
logging.info() and logging.debug() messages go out to my file as expected.

-id

-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20110513/b45aa5f8/attach
ment-0001.html>

------------------------------

Message: 2
Date: Sat, 14 May 2011 10:48:51 +1000
From: "Lea Parker" <lea-par...@bigpond.com>
To: <tutor@python.org>
Subject: [Tutor] Help with exceptions please
Message-ID: <001201cc11d0$b1912150$14b363f0$@bigpond.com>
Content-Type: text/plain; charset="us-ascii"

Hello

 

I have another assignment I am trying to fine tune. The idea is to make sure
exceptions work. I have come up with a problem when testing my code. When I
enter a golf score for a player the program returns the 'error that has
occurred'. I only want it to do this if someone doesn't enter an number.

 

Could I have some comments on my code for the first program of my assignment
if you have some time please. 

 

"""Assignment Question:  The Springfork Amateur Golf club has a tournament
every weekend.

The club president has asked you to write two programs:

1. A program that will read each player's name and golf score as keyboard

input and then to save these records in a file named golf.txt. (Each record

will have a field name for the player's name and a field for the player's

score.

2. A program that reads the records from the golf.txt and displays them."""

 

 

 

def main():

 

    try:

        # Get the number of players

        golf_scores = int(raw_input('How many players playing this weekend?
'))

 

        # Open a file to hold player names and scores

        player_data = open('golf.txt', 'w')

 

        # Get the players name and score and write it to the file

        for count in range(1, golf_scores + 1):

            # Get the name name and score for player

            print 'Enter the name and score for player #' + str(count)

            name = raw_input('Name: ')

            score = int(raw_input('Score: '))

        

            # Write the data as a record to the file golf.txt

            player_data.write(name + '\n')

            player_data.write(score + '\n')

 

        # Display a blank line

        print

 

        # Close the file

        player_data.close()

        print 'Players and their score have been written to golf.txt.'

 

    except IOError:

        print 'An error occured trying to write information to file.'

 

    except ValueError:

        print 'A numberic value must be entered.'

 

    except:

        print 'An error occured.'

     

 

 

# Call the main function

main()

 

Thanks in advance.

Lea

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.python.org/pipermail/tutor/attachments/20110514/d9903fbc/attach
ment-0001.html>

------------------------------

Message: 3
Date: Fri, 13 May 2011 17:49:29 -0700
From: ian douglas <ian.doug...@iandouglas.com>
To: Tutor@python.org
Subject: Re: [Tutor] short url processor
Message-ID: <4dcdd199.6070...@iandouglas.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

On 05/13/2011 05:03 PM, Alan Gauld wrote:
> As I say, just some thoughts,
>

I *am* curious, Alan, whether you or anyone else on the list are able to 
help me make this a little more efficient:

                 cur.execute("SELECT short_url,long_url FROM short_urls")
                 giant_list = cur.fetchall()

                 for i in giant_list:
                         if i[0]:
                                 if i[1]:
                                         mc.set(i[0], i[1])


At present, we have about two million short URL's in our database, and 
I'm guessing there's a much smoother way of iterating through 2M+ rows 
from a database, and cramming them into memcache. I imagine there's a 
map function in there that could be much more efficient?

v2 of our project will be to join our short_urls table with its 'stats' 
table counterpart, to where I only fetch the top 10,000 URLs (or some 
other smaller quantity). Until we get to that point, I need to speed up 
the restart time if this script ever needs to be restarted. This is 
partly why v1.5 was to put the database entries into memcache, so we 
wouldn't need to reload the db into memory on every restart.

Thanks,
Ian



------------------------------

Message: 4
Date: Fri, 13 May 2011 19:57:42 -0700 (PDT)
From: Terry Carroll <carr...@tjc.com>
To: tutor@python.org
Subject: Re: [Tutor] Overriding a method in a class
Message-ID: <alpine.lrh.2.00.1105131946260.18...@aqua.rahul.net>
Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed

On Fri, 13 May 2011, David Knupp wrote:

> I think the problem is this bit about overriding an object's internal 
> method with one that is defined externally.

Yes, that describes the phenomenon.

> My gut feeilng is that you'd 
> now have to explicitly pass the object (i.e., self) as well as the 
> string, i.e.:
>
> B.addstuff(B, "WXYZ")
>
> ...which seems clunky.

But worse, it would mean that you couldn't have the default un-overridden 
method for those cases where you don't need to override it.

> I'm not familiar with this particular technique. Is it common to do 
> something like this?

Certainly the problem is common.

For my specific case, I'm going to go with a Plan B of using callbacks; 
and provide default unbound callbacks present in the module, but not 
defined in the class itself.

But I'd still like to have a better understanding of how the call gets 
transmuted from a two-argument call to a one-argument call based upon the 
target.  I suspect something along these lines is going on:

<begin uninformed speculation>
When a bound method is called, Python inserts a reference to self as an 
additional first argument, before the other arguments.  It does not do this 
with unbound methods.

In my first case, when I created object A, the name add_the_stuff 
references the bound method add_the_stuff as defined in the Thing class. 
When add_stuff calls add_the_stuff, because add_the_stuff references a 
bound method, Python does the automagical insertion of the self argument.

In my second case, when I created the object B, the name add_the_stuff 
*initially* references the bound method add_the_stuff; but then, when I 
override, I make B.add_the_stuff reference the unbound external method. 
Now, when addstuff calls add_the_stuff, Python sees that it is referencing 
an unbound method, and does not insert the self reference into the 
argument list.
<end uninformed speculation>

Now I'll wait for one of the experts to edify me.

> On Fri, 13 May 2011, Terry Carroll wrote:
>
>> I have a pretty basic point of confusion that I'm hoping I can have 
>> explained to me.  I have a class in which I want to override a method,
and 
>> have my method defined externally to the class definition invoked
instead. 
>> But when I do so, my external method is invoked with a different argument

>> signature than the method it overrides.
>> 
>> (I'll illustrate with a toy example named toy.py that maintains a list of

>> strings; the actual use case is a wxPython drag-and-drop shell that I
find 
>> I keep re-using over and over, so I decided to try to turn it into a 
>> general-purpose module for my own use.)
>> 
>> ### example 1 begin
>> 
>> class Thing(object):
>>    def __init__(self):
>>        self.stuff = []
>>    def addstuff(self, text):
>>        self.add_the_stuff(text)
>>    def add_the_stuff(self, s1):
>>        self.stuff.append(s1)
>> 
>> A = Thing()
>> A.addstuff("ABCDEFG")
>> print A.stuff
>> 
>> ### example 1 end
>> 
>> So far, this works as expected.  addstuff invokes add_the_stuff; and the 
>> line "print A.stuff" prints out as ['ABCDEFG'], as expected.
>> 
>> Now, here's where I am getting befuddled, with the following additional 
>> lines:
>> 
>> ### example, continued
>> def addlower(self, s2):
>>    self.stuff.append(s2.lower()) # add it as lower case
>> 
>> B = Thing()
>> B.add_the_stuff=addlower
>> B.addstuff("WXYZ")
>> print B.stuff
>> ### end
>> 
>> My *intent* here is to patch the Thing object named B so that the B's 
>> add_the_stuff method is replaced with this additional addlower method
that 
>> I define external to the object.  My expectation would be that, just as 
>> add_the_stuff method was called with two arguments (self and the string),

>> the patched-in addlower would also get called the same way.
>> 
>> What I *expect* is to see ['abcdefg'] printed.  What I get is:
>> 
>> Traceback (most recent call last):
>>  File "E:\Personal\py\DragDrop\toy.py", line 22, in <module>
>>    B.addstuff("WXYZ")
>>  File "E:\Personal\py\DragDrop\toy.py", line 7, in addstuff
>>    self.add_the_stuff(text)
>> TypeError: addlower() takes exactly 2 arguments (1 given)
>> 
>> I'm assuming I'm missing some fundamental concept.  What is it?
>> _______________________________________________
>> Tutor maillist  -  Tutor@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>> 
>> 
>


------------------------------

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


End of Tutor Digest, Vol 87, Issue 44
*************************************

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

Reply via email to