[Tutor] Learning about callbaks

2007-12-29 Thread Michael Bernhard Arp Sørensen
Hi there.

I want to learn about callbacks because we use it at work in our software.

I there a short hello world-like version of a callback example?

-- 
Med venlig hilsen/Kind regards

Michael B. Arp Sørensen
Programmør / BOFH
I am /root and if you see me laughing you better have a backup.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Learning about callbaks

2007-12-29 Thread Michael Bernhard Arp Sørensen
Greetings, my master.

I'm writing a game based on curses.

I have my own screen object and several child objects to handle sub windows
with e.g. menues, board/map/views and log outputs. All user input is done
with screen.getch and later sent to the dynamic menu for selecting menu
points.

My imidiate problem is when I select Quit from the menu, I need to send
the string back to the caller/parent class for evaluation.

Later I will need to design all the menues and the related methods, other
sub windows in different threads for individual updates. But first I need a
working UI.

Off topic: I must say that I'm amazed by this tutor thing. To really have my
own tutor in this new programming language I'm learning, is kinda blowing
my mind. I hope I can repay the python community some day when I'm smart
enough. :-)

Thanks in advance

/karneevor

On Dec 29, 2007 6:39 PM, Alan Gauld [EMAIL PROTECTED] wrote:


 Michael Bernhard Arp Sørensen [EMAIL PROTECTED]
 wrote

  I want to learn about callbacks because we use it at work in our
  software.

 Can you be more specific about what you want to know. Callbacks are
 used in many different ways from event handling methods in a GUI
 to network programming to simulating synchronous protocols over
 an asynchronous connection.

  I there a short hello world-like version of a callback example?

 See almost any GUI tutorial.
 The recent thread Closing GUI program had the following example
 from Michael Goldwasser

 #---
 from Tkinter import Tk,Label

 def onClose():
root.destroy()   # stops the main loop and interpreter

 root = Tk()
 root.protocol(WM_DELETE_WINDOW, onClose)  # handle event when window
 is closed by user
 z = Label(root, text=Hello World!)
 z.grid()
 root.mainloop()
 #---


 In this example the onClose() event handler is a callback function.

 The folowing pseusdo code shows how the principle can be used for
 asynchronous network programming:

 waiting = {} # list of objects awaiting responses
 id = 0

 def sendToServer(msg, callback)
   msgSent = prepareMessage(msg)
   id = server.send(msgSent)
   waiting[id] = (msg, callback)

 def func1()
 msg = prepareData()
 sendToServer(msg, func1_cont)

 def func1_cont(original, result)
 x,y,z = result.getValues()
 processData(x,y,z,original.p,original.q)

 while server.listen()
  msg = server.recv()
  id = msg.getID()
  oldMessage = waiting[id][0]
  callback =  waiting[id][1]
  callback(oldmessage, msg)
  del(waiting[id])

 In this example we can think of the main application calling func.
 func1 needs to send a message to a server and process the response
 but the server has an asynchronous protocol so we split the function
 into func1 and func1_cont at the point of calling the server. Then
 when the server send us the response we pull the stored state out
 of the dictionary and combine it with the server data to complete
 the func1 processing via the func1_cont callback.

 In practice we'd probably store the date/time with the transaction
 data so that we can check for timeouts etc in a separate thread...

 The important thing with all callbacks is that you match up the
 data expected by the callback with the data actually available
 at the point of calling it. In this case we take the architectural
 decision to pass callback functions the old and new data structures.
 We could alternatively have passed the transaction id and let the
 callback retrieve (and delete) the data from the waiting list.

 I hope that all makes sense.

 --
 Alan Gauld
 Author of the Learn to Program web site
 http://www.freenetpages.co.uk/hp/alan.gauld


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




-- 
Med venlig hilsen/Kind regards

Michael B. Arp Sørensen
Programmør / BOFH
I am /root and if you see me laughing you better have a backup.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Careful Dictionary Building

2007-12-29 Thread Reed O'Brien

On Dec 28, 2007, at 11:29 AM, doug shawhan wrote:


*sigh* Ignore folks. I had forgotten about .has_key().


.has_key() is deprecated in 2.6 and goes away in 3.0 IIRC

You should use

record in D

or

D.get(record)




On Dec 28, 2007 11:22 AM, doug shawhan [EMAIL PROTECTED] wrote:
I'm building a dictionary from a list with ~ 1M records.

Each record in the list is itself a list.
Each record in the list has a line number, (index 0) which I wish  
to use as a dictionary key.


The problem: It is possible for two different records in the list  
to share this line number. If they do, I want to append the record  
to the value in the dictionary.


The obvious (lazy) method of searching for doubled lines requires  
building and parsing a key list for every record. There must be a  
better way!


dict = {}
for record in list
if record[0] in dict.keys ():
dict[ record[0] ].append( record )
else:
dict[ record[0] ] = [record]

Once you get ~ 80,000 records it starts slowing down pretty badly  
(I would too ...).


Here's hoping there is a really fast, pythonic way of doing this!

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


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


Re: [Tutor] Careful Dictionary Building

2007-12-29 Thread Paul McGuire
1. Don't name your dict 'dict' or your list 'list', as this then masks the
builtin dict and list types.
2. Your application is a textbook case for defaultdict:

from collections import defaultdict

recordDict = defaultdict(list)
for record in recordList:
recordDict[record[0]].append(record)

Voila!  No key checking, no keeping of separate key lists (wrong for many
other reasons, too), just let defaultdict do the work.  If the key does not
exist, then defaultdict will use the factory method specified in its
constructor (in this case, list) to initialize a new entry, and then the new
record is appended to the empty list.  If the key does exist, then you
retreive the list that's been built so far, and then the new record gets
appended.

-- Paul

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